8000 FastAPI integration with DocList · Issue #1449 · docarray/docarray · GitHub
[go: up one dir, main page]

Skip to content

FastAPI integration with DocList #1449

@samsja

Description

@samsja

Context

As of today to use FastAPI with DocList we need to cast the DocList into a python list manually like this

from typing import List

import numpy as np
from fastapi import FastAPI
from httpx import AsyncClient

from docarray import DocList
from docarray.base_doc import DocArrayResponse
from docarray.documents import TextDoc

# Create a docarray
docs = DocList[TextDoc]([TextDoc(text='first'), TextDoc(text='second')])

app = FastAPI()


# Always use our custom response class (needed to dump tensors)
@app.post("/doc/", response_class=DocArrayResponse)
async def create_embeddings(docs: List[TextDoc]) -> List[TextDoc]:
    # The docs FastAPI will receive will be treated as List[TextDoc]
    # so you need to cast it to DocList
    docs = DocList[TextDoc].construct(docs)

    # Embed docs
    for doc in docs:
        doc.embedding = np.zeros((3, 224, 224))

    # Return your DocList as a list
    return list(docs)

Objective

The objective is to be able to directly use DocList in the type hint

@app.post("/doc/", response_class=DocArrayResponse)
async def create_embeddings(docs: DocList[TextDoc]) -> DocList[TextDoc]:
    assert isinstance(docs, DocList)

How to do it

The most straightforward way is to:

  • Make DocList a real python list (alternatively we could extend isinstance to return true for list but it is a bit hacky).
  • Using post root validator from pydantic to init the list into a DocList

Metadata

Metadata

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

    0