Full answer
Older FastAPI code puts the metadata in the default value:
def list_books(page: int = Query(1, ge=1)): ...The current style uses typing.Annotated:
def list_books(page: Annotated[int, Query(ge=1)] = 1): ...Why it is better:
- The default is a real default. In the old style, the default value is a
Queryobject. Callinglist_books()from a test or a script givespageaQueryinstance, not 1. With Annotated,list_books()getspage=1. - Type checkers and editors understand it.
Annotated[int, ...]is justintto mypy and Pyright; the extra items are metadata that other tools ignore. - Reuse. You can name a parameter declaration once:
PageParam = Annotated[int, Query(ge=1, le=500)]
CurrentUser = Annotated[User, Depends(get_current_user)]
def list_books(page: PageParam = 1, user: CurrentUser): ...This removes copy-paste across dozens of endpoints and keeps rules consistent.
4. Required parameters are clearer. Without a default, it is required. No need for the old Query(...) with an Ellipsis.
The same pattern applies to Path, Body, Header, Cookie, Form, File and Depends.
Pydantic uses Annotated the same way for Field constraints, so one style covers models and endpoints.
Rules: put the default after =, not inside Query() when using Annotated (FastAPI raises an error if you do both). Parameters without defaults must come before those with defaults, as in any Python function, so reusable dependency aliases without defaults go first.
from typing import Annotated
from fastapi import Depends, FastAPI, Query
from fastapi.testclient import TestClient
PageParam = Annotated[int, Query(ge=1, le=500)]
def get_current_user() -> str:
return "librarian"
CurrentUser = Annotated[str, Depends(get_current_user)]
app = FastAPI()
@app.get("/old-style")
def old_style(page: int = Query(1, ge=1)):
return {"page": page}
@app.get("/books")
def list_books(user: CurrentUser, page: PageParam = 1):
return {"page": page, "user": user}
c = TestClient(app)
print(c.get("/books?page=3").json(), c.get("/books?page=0").status_code)
# Calling the functions directly, as a test or script might
print("old style default is:", type(old_style()["page"]).__name__)
print("Annotated default is:", list_books(user="tester")["page"])
Standalone script. Run it with `python file.py`; it calls the app through TestClient, so no server is needed. Output (ran twice with identical results; Python 3.13.5, FastAPI 0.165.1, Pydantic 2.14.4, Starlette 1.10.0, SQLAlchemy 2.1.9, httpx 0.29.0): {'page': 3, 'user': 'librarian'} 422 old style default is: Query Annotated default is: 1
Key points
- Annotated separates type, metadata and default
- Functions stay callable with real defaults
- Type checkers see the true type
- Aliases like CurrentUser remove repetition
- Recommended since FastAPI 0.95
Common mistakes
- Putting the default inside Query() while also using Annotated
- Calling old-style endpoint functions directly and getting Query objects as values
- Copy-pasting the same Query constraints across endpoints instead of an alias
- Ordering parameters with defaults before parameters without defaults
Follow-ups
- How would you share a pagination dependency across all list endpoints?
- What does a type checker see for Annotated[int, Query()]?
- How do you migrate a large codebase from the old style?
Tips
- Lead with the 'functions stay normal Python' benefit
- Show a reusable alias
- Mention the version where it became recommended