# fastapi.exceptions.ResponseValidationError: 响应验证错误，返回数据不匹配响应模型。

- **ID:** `python/fastapi-response-validation-error`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

视图返回的数据不符合声明的 response_model（类型错误、字段缺失或嵌套结构不匹配）。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 0.100.x | active | — | — |
| 0.110.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Coerce in the view: `return UserResponse(id=int(user['id']), name=str(user['name']))` or use `.model_validate(user, from_attributes=True)`.
   ```
2. **** (90% 成功率)
   ```
   Set `model_config = ConfigDict(from_attributes=True)` on the Pydantic model and return ORM objects directly.
   ```

## 无效尝试

- **** — Still logs ResponseValidationError; clients get opaque failures. (85% 失败率)
- **** — Loses OpenAPI schema and silently returns wrong shapes. (75% 失败率)
