# fastapi.exceptions.HTTPException: 422: 无法处理的实体

- **ID:** `python/fastapi-httpexception-422-unprocessable-entity`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

请求体格式正确但包含语义错误，例如无效的数据类型。

## 版本兼容性

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

## 解决方案

1. **Use Pydantic's conint or constr for validation** (90% 成功率)
   ```
   from pydantic import BaseModel, conint
class Item(BaseModel):
    age: conint(gt=0, lt=150)  # valid integer
   ```
2. **Add custom validators** (85% 成功率)
   ```
   from pydantic import validator
class Item(BaseModel):
    name: str
    @validator('name')
    def name_must_be_alpha(cls, v):
        if not v.isalpha():
            raise ValueError('must be alphabetic')
        return v
   ```

## 无效尝试

- **Sending numbers as strings without conversion** — Pydantic may not auto-convert if strict mode is on. (70% 失败率)
- **Missing required fields in nested models** — Validation fails at nested level. (80% 失败率)
