python
type_error
ai_generated
true
fastapi.exceptions.FastAPIError: Invalid args for response field 'items'
ID: python/fastapi-query-param-missing-annotation
80%Fix Rate
84%Confidence
0Evidence
2024-03-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Defining a response model with a field that has no type annotation or an invalid one (e.g., `items = []` without `List[Item]`) causes FastAPI to fail when generating the response schema.
generic中文
在响应模型中定义没有类型注解或无效注解的字段(如`items = []`未指定`List[Item]`),FastAPI在生成响应模式时会失败。
Workarounds
-
95% success
Define the response model with proper type hints: `from typing import List; class ResponseModel(BaseModel): items: List[Item]`
-
90% success
Use `response_model=List[Item]` in the route decorator instead of a custom model.
Dead Ends
Common approaches that don't work:
-
80% fail
Adding a default value like `items: list = []` still fails if the list type is not specified; FastAPI needs `List[SomeModel]`.
-
70% fail
Using `items: Any` works but loses validation and may not be what you want.