python type_error ai_generated true

AttributeError: 'Query' object has no attribute 'default'

ID: python/fastapi-attribute-error-no-attribute-query

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-01-09First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
0.95.x active — — —
0.100.x active — — —

Root Cause

Using `Query(default=...)` in a Pydantic v2 / FastAPI version where the parameter was renamed to `default` only in newer releases, or mixing `Query()` with `Annotated` incorrectly.

generic

中文

在 Pydantic v2 / FastAPI 版本中使用了已重命名的参数,或错误地将 `Query()` 与 `Annotated` 混用。

Workarounds

  1. 95% success
    Use the modern form: `q: Annotated[str | None, Query(max_length=50)] = None`
  2. 90% success
    Or the older positional style: `q: str = Query(None, max_length=50)` (no `default=` keyword).

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Downgrade breaks Pydantic v2 compatibility and other features.

  2. 90% fail

    Fragile across versions and breaks OpenAPI generation.