Skip to content

Improve fastapi-docarray compatibility #1324

@jupyterjazz

Description

@jupyterjazz

Improve the way we send/receive DocArray objects with FastAPI.

Context

Currently, DocArray objects are received as lists, so we have to construct an actual DocArray inside the function, and in case of returning, we have to convert it into a list ourselves (done in #1320)

@app.post("/doc/", response_class=DocArrayResponse)
async def func(fastapi_docs: List[ImageDoc]) -> List[ImageDoc]:
    # The docs FastAPI will receive will be treated as List[ImageDoc]
    # so you need to cast it to DocArray
    docarray_docs = DocArray[ImageDoc].construct(fastapi_docs)
    ...
    # In case you want to return a DocArray, return it as a list
    return list(docarray_docs)

What we want

The goal is to receive/return DocArray objects without any additional operations from the developer side.

@app.post("/doc/", response_class=DocArrayResponse)
async def func(docs: DocArray[ImageDoc]) -> DocArray[ImageDoc]:
    return docs

Ways to do it

I've investigated couple of approaches, and here are the most plausible ones and the main problem I had with them:

  1. DocArray inherits from List/Sequence (draft feat: make docarray fastapi-compatible #1316)
    FastAPI still received DocArray as a List. If we fix that, this is the best way to go
  2. DocArray inherits from Pydantic's GenericModel (draft feat: docarray extends generic pydantic model #1304)
    GenericModel does type-checking during runtime which introduces lots of circular imports
  3. DocArray inherits from Pydantic's BaseModel (draft feat: docarray extends pydantic basemodel #1303)
    BaseModel introduced couple of functions that were in conflict with our existing ones

In general, extending pydantic's classes was a bit of a headache, the first option is much cleaner.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions