tezvyn:

How would you implement a dependency requiring multi-source parameters?

AI-drafted, machine-checkedSource: fastapi.tiangolo.comadvanced
How would you implement a dependency requiring multi-source parameters?

Tests if you know FastAPI resolves dependency params like endpoint params. Great answers annotate each parameter with its source inside the dependency so FastAPI injects them independently. Red flag: manually parsing Request or merging values in the endpoint.

WHAT THIS TESTS: This question probes whether you understand that FastAPI's dependency injection system uses the same parameter resolution machinery for dependencies as it does for path operation functions. The interviewer wants to know if you recognize that a dependency callable is not a black box that receives a raw Request, but rather a function whose signature is introspected by FastAPI to map each parameter to the correct part of the HTTP request.

A GOOD ANSWER COVERS: First, explain that you write a normal Python function where each parameter is annotated with its source, such as Path for route variables, Header for headers, Query for query strings, or Cookie for cookies. Second, state that FastAPI inspects this signature automatically and resolves every parameter independently, exactly as it would for an endpoint function. Third, note that you then declare this function as a dependency using Depends in the path operation or in another dependency, creating a sub-dependency chain if needed. Fourth, mention that because resolution is annotation-driven, you can mix sources freely in a single dependency without touching the Request object directly.

COMMON WRONG ANSWERS: A major red flag is saying you would inject the raw Request object and manually extract headers or path parameters from its scope or headers dict. Another is suggesting you must gather the values in the path operation function and then pass them into a regular helper function; this misses the point of the DI system entirely. A third error is claiming that dependencies can only read from one source type or that you need a special class or decorator to enable multi-source resolution.

LIKELY FOLLOW-UPS: The interviewer may ask how class-based dependencies work, in which case the answer is that FastAPI resolves the __init__ parameters the same way. They might ask about dependency caching across a single request, or how to handle validation errors inside dependencies. They could also ask what happens when a dependency parameter name clashes with a path operation parameter name, testing whether you understand that scopes are independent.

ONE CONCRETE EXAMPLE: Imagine an endpoint GET /items/{item_id} that needs a dependency requiring the item_id from the path and an X-Api-Version from the header. You define async def get_item_context(item_id: int = Path(...), api_version: str = Header(..., alias="X-Api-Version")): and return a context object. In the endpoint, you declare async def read_item(context: ItemContext = Depends(get_item_context)). FastAPI sees the dependency, inspects get_item_context, sees Path on item_id and Header on api_version, resolves both from the incoming request, and injects them automatically.

Source: fastapi.tiangolo.com

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.