# fastapi.exceptions.RequestValidationError: Model 的 1 个校验错误
body -> age
  不是有效的整数 (type=type_error.integer)

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

## 根因

Pydantic 的 int 字段从 JSON 请求体接收到非数字字符串或带小数的浮点数。

## 版本兼容性

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

## 解决方案

1. **** (92% 成功率)
   ```
   requests.post(url, json={'age': int(age_str)})
   ```
2. **** (90% 成功率)
   ```
   from pydantic import field_validator
@field_validator('age', mode='before')
def parse_age(cls, v):
    return int(v)
   ```

## 无效尝试

- **** — Downstream arithmetic fails with TypeError. (80% 失败率)
- **** — Hides bad data and corrupts records. (85% 失败率)
