python data_error ai_generated true

fastapi.exceptions.RequestValidationError: value is not a valid integer

ID: python/fastapi-query-param-validation-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-03-30First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

A query parameter expected to be an integer is provided as a non-numeric string or missing.

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. 70% fail

    Using int() conversion in the function body can raise ValueError instead of a proper validation error.

  2. 60% fail

    Setting a default value of 0 masks the error but allows invalid data.