python type_error ai_generated true

fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'typing.Any'> is a valid Pydantic field type.

ID: python/fastapi-openapi-schema-generation-failed

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-09-30First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.100+ active — — —

Root Cause

A route used a return annotation or response_model of Any or an unsupported type, which Pydantic cannot build a schema for.

generic

中文

路由使用了 Any 或不支持类型的返回注解或 response_model,Pydantic 无法为其构建 schema。

Workarounds

  1. 95% success
    class ItemOut(BaseModel):
        id: int
        name: str
    @app.get('/items', response_model=list[ItemOut])
    async def items(): ...
  2. 85% success
    @app.get('/raw', response_class=JSONResponse)
    async def raw() -> dict: ...

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Disables OpenAPI schema; clients lose typed documentation.

  2. 80% fail

    Pydantic still rejects Any in response models.