# FastAPI请求验证错误：值不是有效的整数

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

## 根因

期望为整数的查询参数被提供为非数字字符串或缺失。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Use type hints: def read_item(item_id: int = Query(...)):
   ```
2. **** (90% 成功率)
   ```
   Catch RequestValidationError and return a custom message.
   ```

## 无效尝试

- **** — Using int() conversion in the function body can raise ValueError instead of a proper validation error. (70% 失败率)
- **** — Setting a default value of 0 masks the error but allows invalid data. (60% 失败率)
