Full answer
Scalar types (int, str, bool) in the signature become query parameters if they are not part of the path. A list[int] needs Query explicitly:
tags: Annotated[list[str] | None, Query(max_length=5)] = None # ?tags=a&tags=bThe URL repeats the key; FastAPI collects the values into a list and converts each to the item type (list[int] rejects ?id=x with 422). Comma-separated values (?tags=a,b) arrive as one string unless you split them yourself.
Common Query() arguments:
alias="item-query": the external name, needed for hyphenated names such asx-page-size.min_length,max_length,pattern: for strings. For lists,min_lengthandmax_lengthlimit the number of items.ge,gt,le,lt: numeric bounds, applied to each item for lists of numbers.description,title,examples: docs.deprecated=True: shown as deprecated in/docs, while still working.include_in_schema=False: hide internal parameters.validation_aliasandserialization_aliasgive finer control on models.
Required or optional is decided by the default: no default (or ...) is required; a default value or None makes it optional.
Booleans accept true, false, 1, 0, yes, no, on, off. For many related query parameters, group them in a Pydantic model with Annotated[FilterParams, Query()] (FastAPI 0.115+), which keeps signatures short.
Beware that query strings end up in logs and browser history, so never put secrets in them.
from typing import Annotated
from fastapi import FastAPI, Query
from fastapi.testclient import TestClient
app = FastAPI()
@app.get("/students")
def search(
tags: Annotated[list[str] | None, Query(max_length=3, description="Repeat the key for several tags")] = None,
ids: Annotated[list[int], Query()] = [],
page_size: Annotated[int, Query(alias="page-size", ge=1, le=50)] = 10,
q: Annotated[str | None, Query(min_length=2, pattern="^[a-z ]+$")] = None,
legacy: Annotated[str | None, Query(deprecated=True)] = None,
):
return {"tags": tags, "ids": ids, "page_size": page_size, "q": q}
c = TestClient(app)
print(c.get("/students?tags=a&tags=b&ids=1&ids=2&page-size=20&q=ravi").json())
print(c.get("/students?tags=a,b").json())
for url in ("/students?ids=x", "/students?page-size=500", "/students?q=A1", "/students?tags=1&tags=2&tags=3&tags=4"):
err = c.get(url).json()["detail"][0]
print(url, "->", err["loc"], err["type"])
params = {p["name"]: p for p in c.get("/openapi.json").json()["paths"]["/students"]["get"]["parameters"]}
print(params["legacy"].get("deprecated"), sorted(params))
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): {'tags': ['a', 'b'], 'ids': [1, 2], 'page_size': 20, 'q': 'ravi'} {'tags': ['a,b'], 'ids': [], 'page_size': 10, 'q': None} /students?ids=x -> ['query', 'ids', 0] int_parsing /students?page-size=500 -> ['query', 'page-size'] less_than_equal /students?q=A1 -> ['query', 'q'] string_pattern_mismatch /students?tags=1&tags=2&tags=3&tags=4 -> ['query', 'tags'] too_long True ['ids', 'legacy', 'page-size', 'q', 'tags']
Key points
- list[T] needs Query() so it is not read as a body
- Repeat the key for lists: ?tag=a&tag=b
- alias supports external names that are not valid identifiers
- deprecated=True marks parameters in the docs
- A default value makes a parameter optional
Common mistakes
- Declaring list[str] without Query() so it is treated as a body
- Sending comma-separated values and expecting a list
- Forgetting the alias for hyphenated parameter names
- Putting secrets in query strings
- Using a mutable default carelessly (use None or a factory)
Follow-ups
- How do you group many query parameters into a model?
- How do you make a repeated key required to have at least one value?
- What does include_in_schema=False do?
Tips
- Show repeated-key lists
- List 4 Query arguments
- Explain required vs default