python
data_error
ai_generated
true
fastapi.exceptions.ResponseValidationError: 2 validation errors for UserResponse id value is not a valid integer (type=type_error.integer)
ID: python/fastapi-response-validation-error
80%Fix Rate
88%Confidence
0Evidence
2024-06-07First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 0.100.x | active | — | — | — |
| 0.110.x | active | — | — | — |
Root Cause
The view returns data that does not conform to the declared response_model (wrong types, missing fields, or extra nested structure).
generic中文
视图返回的数据不符合声明的 response_model(类型错误、字段缺失或嵌套结构不匹配)。
Workarounds
-
95% success
Coerce in the view: `return UserResponse(id=int(user['id']), name=str(user['name']))` or use `.model_validate(user, from_attributes=True)`.
-
90% success
Set `model_config = ConfigDict(from_attributes=True)` on the Pydantic model and return ORM objects directly.
Dead Ends
Common approaches that don't work:
-
85% fail
Still logs ResponseValidationError; clients get opaque failures.
-
75% fail
Loses OpenAPI schema and silently returns wrong shapes.