Skip to content
This repository was archived by the owner on Sep 9, 2026. It is now read-only.
This repository was archived by the owner on Sep 9, 2026. It is now read-only.

FastAPI integration with DocList #1449

Closed

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

Activity

  1. converted this from a draft issue on Apr 25, 2023
  2. changed the title [-]Make DocList a real List[/-] [+]FastAPI integration with DocList[/+] on Apr 25, 2023
  3. moved this from Todo/Bug to Todo/Features in DocArray backlogon Apr 25, 2023
  4. JoanFM commented on Apr 27, 2023

    @JoanFM
    Member

    @jupyterjazz I think we can already test that after merging #1457

  5. reopened this on Apr 27, 2023
  6. moved this from Todo/Features to Done in DocArray backlogon Apr 27, 2023
  7. moved this from Done to Todo/Bug in DocArray backlogon Apr 28, 2023
  8. removed their assignment
    on May 9, 2023
  9. JohannesMessner commented on May 9, 2023

    @JohannesMessner
    Member

    I looked into this, sending a taking a DocList as input works, returning one as an output does not. I am working on this now.

    Update: Actually both sending and returning work, only the OpenAPI schema is messed up, see #1521

    I would consider this issue to be resolved once:

  10. moved this from Todo/Bug to In Progress in DocArray backlogon May 9, 2023
  11. JoanFM commented on May 17, 2023

    @JoanFM
    Member

    I will close this issue as duplicated of #1521

  12. moved this from In Progress to Done in DocArray backlogon May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions