python data_error ai_generated true

FastAPI 请求验证错误:路径参数 'item_id' 不是有效的整数

fastapi.exceptions.RequestValidationError: [{'loc': ['path', 'item_id'], 'msg': 'value is not a valid integer', 'type': 'type_error.integer'}]

ID: python/fastapi-path-param-type-error

其他格式: JSON · Markdown 中文 · English
80%修复率
87%置信度
0证据数
2025-04-18首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

路径参数标注为 int,但 URL 中提供了非整数值。

English

A path parameter annotated as int receives a non-integer value from the URL.

generic

解决方案

  1. 90% 成功率
    Ensure the client sends an integer in the URL, e.g., /items/123
  2. 100% 成功率
    Use a Pydantic model or path converter: @app.get('/items/{item_id}') with item_id: int

无效尝试

常见但无效的做法:

  1. 60% 失败

    This doesn't fix the client's request; they still need to send a valid integer.

  2. 50% 失败

    Loses automatic validation; you have to handle conversion errors yourself.