# FastAPI请求验证错误：422 无法处理的实体

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

## 根因

请求体或查询参数与预期的Pydantic模型架构不匹配，例如缺少必填字段或类型错误。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Define a custom exception handler: @app.exception_handler(RequestValidationError)\nasync def validation_exception_handler(request, exc):\n    return JSONResponse(status_code=422, content={"detail": exc.errors()})
   ```
2. **** (85% 成功率)
   ```
   Use Pydantic's Field(..., description='...') to document requirements and ensure clients send correct data.
   ```

## 无效尝试

- **** — Catching the exception globally and returning a generic 400 hides the specific field issues, making debugging harder. (60% 失败率)
- **** — Disabling validation with model_config = {'extra': 'allow'} can allow invalid data through, causing downstream errors. (80% 失败率)
