python data_error ai_generated true

fastapi.exceptions.RequestValidationError: 1 validation error for Request query -> q field required (type=value_error.missing)

ID: python/fastapi-422-missing-query-param

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

A function parameter declared without a default is treated as a required query parameter and the client omitted it.

generic

中文

未声明默认值的函数参数被视为必填查询参数,而客户端省略了它。

Workarounds

  1. 95% success
    from fastapi import Query
    @app.get('/search')
    async def search(q: str = Query(default='')):
        ...
  2. 92% success
    async def search(q: str | None = None):
        ...

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Still required; Query(...) means required.

  2. 70% fail

    Hides the contract mismatch from the client.