fastapi docs , Pydantic BaseModel, request example missing

20 Views Asked by At
from pydantic.v1 import *

self._router.add_api_route(
    "/v1/embeddings",
    self.create_embedding,
    methods=["POST"],
    dependencies=(
        [Security(self._auth_service, scopes=["models:read"])]
        if self.is_authenticated()
        else None
    ),
)

async def create_embedding(self, request: Request) -> Response:
    body = CreateEmbeddingRequest.parse_obj(await request.json())
    model_uid = body.model

    try:
        model = await (await self._get_supervisor_ref()).get_model(model_uid)
    except ValueError as ve:
        logger.error(str(ve), exc_info=True)
        await self._report_error_event(model_uid, str(ve))
        raise HTTPException(status_code=400, detail=str(ve))
    except Exception as e:
        logger.error(e, exc_info=True)
        await self._report_error_event(model_uid, str(e))
        raise HTTPException(status_code=500, detail=str(e))

    try:
        embedding = await model.create_embedding(body.input)
        return Response(embedding, media_type="application/json")
    except RuntimeError as re:
        logger.error(re, exc_info=True)
        await self._report_error_event(model_uid, str(re))
        self.handle_request_limit_error(re)
        raise HTTPException(status_code=400, detail=str(re))
    except Exception as e:
        logger.error(e, exc_info=True)
        await self._report_error_event(model_uid, str(e))
        raise HTTPException(status_code=500, detail=str(e))

class CreateEmbeddingRequest(BaseModel):
    model: str
    input: Union[str, List[str], List[int], List[List[int]]] = Field(
        description="The input to embed."
    )
    user: Optional[str] = None

    class Config:
        schema_extra = {
            "example": {
                "input": "The food was delicious and the waiter...",
            }
        }

I want to declare the request example in request body like that scheenshot. but the result of my code is like that scheenshot Who can help me, thanks in advance.

Operating System

Linux

Operating System Details

CentOS 7

FastAPI Version

0.109.2

Pydantic Version

2.6.4

Python Version

3.10.13

Additional Context

my Pydantic Version is V2,but my code import pydantic.v1

more details in: https://github.com/tiangolo/fastapi/discussions/11370

0

There are 0 best solutions below