python data_error ai_generated true

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

Also available as: JSON · Markdown · 中文
80%Fix Rate
87%Confidence
0Evidence
2025-04-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 60% fail

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

  2. 50% fail

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