# 值错误：路径参数 'id' 的类型无效：应为 int，实际为 str

- **ID:** `python/starlette-route-param-type`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

Starlette 路由路径参数必须进行类型转换；如果 URL 提供字符串但处理程序期望 int，转换失败时会报错。

## 版本兼容性

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

## 解决方案

1. **Use int type hint in the route parameter** (95% 成功率)
   ```
   async def handler(request):
    id = request.path_params['id']
    # id is already an int
   ```
2. **Add a converter in the route pattern** (90% 成功率)
   ```
   routes = [Route('/items/{id:int}', handler)]
   ```

## 无效尝试

- **Using a string parameter and converting manually** — Starlette auto-converts based on type hints; manual conversion may cause mismatch. (70% 失败率)
- **Not specifying a type hint** — Without type hint, Starlette treats it as str. (80% 失败率)
