How does FastAPI distinguish required optional and default query parameters
Tests whether you know FastAPI infers query parameter optionality from Python signature defaults. Answer: no default means required, Optional[T] = None means optional, T = value sets a default, with all three in one signature.
WHAT THIS TESTS: This question tests whether you understand that FastAPI derives query parameter requirement rules from Python function signature defaults rather than from type hints alone. It reveals whether you know that FastAPI introspects the parameter default at runtime. Senior candidates should demonstrate that they grasp the distinction between type hints for OpenAPI documentation and default values for actual requirement logic.
A GOOD ANSWER COVERS: First, explain the mechanism: FastAPI looks at whether the function parameter has a default value in the Python signature. If there is no default, the parameter is required. Second, describe the optional case: the parameter uses a type hint that allows None, such as Optional[str], AND it has a default value of None, like q: Optional[str] = None. This tells FastAPI the query can be omitted. Third, describe the default value case: the parameter has a non-None default, like limit: int = 10, which makes it non-required and supplies a fallback. Fourth, provide a single function signature that shows all three cases side by side, such as async def read_items(needy: str, q: Optional[str] = None, limit: int = 10). Fifth, note that the type hint affects schema generation and validation, but the requirement behavior is driven by the presence or absence of the default.
COMMON WRONG ANSWERS: A major red flag is stating that Optional[str] alone makes a query parameter optional. In FastAPI, if you write q: Optional[str] without a default, FastAPI treats it as required because Python sees no default in the signature. Another red flag is confusing query parameter rules with Pydantic model field rules, where field declarations follow different patterns. Candidates also err by saying FastAPI uses special decorators or Query classes to set defaults, when the core behavior is native Python. Mentioning Query() is fine for validation, but it is not the primary mechanism for basic requirement.
LIKELY FOLLOW-UPS: An interviewer might ask how to make a query parameter required even when using Query() for validation, which you can do with Query(..., min_length=3). They might also ask what happens if a client omits a required query parameter, which returns a 422 Unprocessable Entity with a detailed JSON error. Another follow-up is how to declare a list query parameter, such as q: list[str] = [], or how required parameters interact with OpenAPI documentation generation.
ONE CONCRETE EXAMPLE: A clear endpoint signature demonstrating all three cases is: async def read_items(needy: str, q: Optional[str] = None, limit: int = 10). Here, needy is required and must be provided in the URL. The q parameter is optional and can be omitted; when omitted it becomes None. The limit parameter is optional but defaults to 10 if the client does not supply it. In the OpenAPI schema, needy will be marked as required, q as optional string, and limit as optional integer with default 10.
Read the original → fastapi.tiangolo.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.