python type_error ai_generated true

fastapi.exceptions.FastAPIError: 响应字段参数无效!提示:检查 typing.Union[str, NoneType] 是否为有效的 Pydantic 字段类型。

fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that typing.Union[str, NoneType] is a valid Pydantic field type.

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

其他格式: JSON · Markdown 中文 · English
80%修复率
83%置信度
0证据数
2024-07-15首次发现

版本兼容性

版本状态引入弃用备注
0.100+ active — — —

根因分析

返回注解使用了 FastAPI 无法转换为 Pydantic 字段的结构(如无校验的自定义类或不受支持的泛型)。

English

A return annotation used a construct FastAPI cannot convert to a Pydantic field (e.g., a custom class without validation, or an unsupported generic).

generic

解决方案

  1. 95% 成功率
    class Out(BaseModel):
        name: str
    @app.get('/x', response_model=Out)
    def x(): ...
  2. 85% 成功率
    from typing import Any
    @app.get('/x')
    def x() -> Any: ...

无效尝试

常见但无效的做法:

  1. 70% 失败

    Loses schema and docs; underlying type issue remains.

  2. 85% 失败

    Bypasses OpenAPI generation but not validation.