-
Notifications
You must be signed in to change notification settings - Fork 234
Closed
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
Done