python
type_error
ai_generated
true
AttributeError: 'Query' object has no attribute 'default'
ID: python/fastapi-attribute-error-no-attribute-query
80%Fix Rate
85%Confidence
0Evidence
2025-01-09First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 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
-
95% success
Use the modern form: `q: Annotated[str | None, Query(max_length=50)] = None`
-
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:
-
80% fail
Downgrade breaks Pydantic v2 compatibility and other features.
-
90% fail
Fragile across versions and breaks OpenAPI generation.