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
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).
解决方案
-
95% 成功率
class Out(BaseModel): name: str @app.get('/x', response_model=Out) def x(): ... -
85% 成功率
from typing import Any @app.get('/x') def x() -> Any: ...
无效尝试
常见但无效的做法:
-
70% 失败
Loses schema and docs; underlying type issue remains.
-
85% 失败
Bypasses OpenAPI generation but not validation.