Skip to content

Make DocumentArray compatible with FastAPI #1229

@samsja

Description

@samsja

Context

Atm FastAPI is working with BaseDocument. But we need to make it work for DocumentArray.

this is currently working

    class Mmdoc(BaseDocument):
        img: ImageDoc
        text: TextDoc
        title: str

    input_doc = Mmdoc(
        img=ImageDoc(tensor=np.zeros((3, 224, 224))), text=TextDoc(), title='hello'
    )

    app = FastAPI()

    @app.post("/doc/", response_model=Mmdoc, response_class=DocumentResponse)
    async def create_item(doc: Mmdoc):
        return doc

but we need to extend it to

    class Mmdoc(BaseDocument):
        img: ImageDoc
        text: TextDoc
        title: str

    da = DocumentArray[Mmdoc]([Mmdoc(
        img=ImageDoc(tensor=np.zeros((3, 224, 224))), text=TextDoc(), title='hello'
    )])

    app = FastAPI()

    @app.post("/doc/", response_model=DocumentArray[Mmdoc], response_class=DocumentArrayResponse)
    async def create_item(da: DocumentArray[Mmdoc]):
        return doc

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions