This repository was archived by the owner on Sep 9, 2026. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 9, 2026. It is now read-only.
# Shaped Tensor are not compatible with FastAPI #1804
Copy link
Copy link
Open
Description
Initial Checks
- I have read and followed the docs and still think this is a bug
Description
NdArray['x'] is not compatible with fastAPI schema.
in a main.py file do:
from fastapi import FastAPI
from docarray.base_doc import DocArrayResponse, BaseDoc
from docarray.typing import NdArray
from pydantic import Field
class EmbeddingObject(BaseDoc):
object: str = 'embedding'
index: int = Field(description='The index of the embedding output, corresponding to the index in the list of inputs')
embedding: NdArray['x'] = Field(description='The embedding of the texts', default=[])
app = FastAPI()
@app.post("/", response_model=EmbeddingObject, response_class=DocArrayResponse)
async def create_item() -> EmbeddingObject:
## call my fancy model to generate the embeddings
return EmbeddingObject(embedding=[0.1, 0.2, 0.3], index=0)then do uvicorn main:app
and from your browser try to access localhost:8000/docs
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 407, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/fastapi/applications.py", line 289, in __call__
await super().__call__(scope, receive, send)
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/starlette/applications.py", line 122, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py", line 184, in __call__
raise exc
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
raise exc
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
await self.app(scope, receive, sender)
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 20, in __call__
raise e
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 17, in __call__
await self.app(scope, receive, send)
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/starlette/routing.py", line 718, in __call__
await route.handle(scope, receive, send)
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/starlette/routing.py", line 66, in app
response = await func(request)
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/fastapi/applications.py", line 244, in openapi
return JSONResponse(self.openapi())
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/fastapi/applications.py", line 217, in openapi
self.openapi_schema = get_openapi(
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/fastapi/openapi/utils.py", line 458, in get_openapi
field_mapping, definitions = get_definitions(
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/fastapi/_compat.py", line 458, in get_definitions
return {}, get_model_definitions(
File "/home/sami/.cache/pypoetry/virtualenvs/docarray-OI1ry5x2-py3.8/lib/python3.8/site-packages/fastapi/_compat.py", line 359, in get_model_definitions
m_schema, m_definitions, m_nested_models = model_process_schema(
File "pydantic/schema.py", line 582, in pydantic.schema.model_process_schema
File "pydantic/schema.py", line 623, in pydantic.schema.model_type_schema
File "pydantic/schema.py", line 251, in pydantic.schema.field_schema
File "pydantic/schema.py", line 319, in pydantic.schema.get_field_schema_validations
File "pydantic/schema.py", line 106, in pydantic.schema._apply_modify_schema
File "./docarray/typing/tensor/abstract_tensor.py", line 282, in __modify_schema__
reduce(mul, cls.__docarray_target_shape__, 1)
TypeError: '<=' not supported between instances of 'str' and 'int'
if I used NdArray instead of NdArray['x'] it is working tho.
Example Code
belowPython, DocArray & OS Version
0.38.1.dev1
Affected Components
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Activity
JoanFM commentedon Sep 26, 2023
what about NdArray[128]?
samsja commentedon Sep 26, 2023
this one is working