python type_error ai_generated true

fastapi.exceptions.FastAPIError: 响应字段参数无效!提示:请检查<class 'models.User'>是否为有效的Pydantic字段类型

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

ID: python/fastapi-route-name-conflict-openapi

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

版本兼容性

版本状态引入弃用备注
0.100.x active — — —
0.110.x active — — —

根因分析

将非Pydantic类作为response_model传递,或在v2下使用了Pydantic v1的BaseModel。

English

Passing a non-Pydantic class as response_model, or a Pydantic v1 BaseModel under v2.

generic

解决方案

  1. 95% 成功率
    class UserOut(BaseModel):\n    id: int\n    name: str
  2. 85% 成功率
    class M(BaseModel):\n    model_config = ConfigDict(arbitrary_types_allowed=True)\n    obj: MyClass

无效尝试

常见但无效的做法:

  1. 70% 失败

    Removes OpenAPI schema; clients lose type info.

  2. 80% 失败

    Pydantic v2 uses __get_pydantic_core_schema__; v1 hook ignored.