Explain the internal role of the Depends class

If you treat Depends as FastAPI's core DI declaration primitive.
A strong answer notes it marks parameters for solver, enables recursive sub-dependencies and Annotated sharing, and feeds OpenAPI.
WHAT THIS TESTS: This question probes whether you understand Depends as the central declaration primitive in FastAPI's dependency injection system rather than viewing it as mere syntax sugar or an authentication helper. The interviewer wants to see that you recognize how the framework distinguishes between standard request parameters and dependencies that must be resolved by the solver. Senior candidates should demonstrate awareness of the declarative contract between path operation functions and the injection machinery.
A GOOD ANSWER COVERS: A strong response explains that Depends marks a parameter as a dependency to be resolved by FastAPI's injection system. The candidate should note that the solver identifies these declarations and then provides the needed objects to the path operation function. The answer should mention that the system supports recursive resolution through sub-dependencies, meaning one dependency can declare others. It should highlight that dependencies can be shared using Annotated, reducing repetition across endpoints. The candidate should also cover that the framework handles both synchronous and asynchronous callables, and that this integration feeds into OpenAPI documentation automatically. Mentioning that dependencies can be declared globally or on specific path operation decorators shows breadth.
COMMON WRONG ANSWERS: A red flag is describing Depends as a decorator or middleware layer, which confuses parameter injection with request interception. Another mistake is treating Depends as an authentication-only tool; while Security builds on it, Depends is a generic dependency primitive. Candidates err by suggesting that Depends instances store complex internal state or caching metadata without grounding this in the documented behavior. Overlooking sub-dependencies or claiming that dependencies are resolved in isolation rather than as a graph also signals shallow understanding.
LIKELY FOLLOW-UPS: The interviewer may ask how you would test dependencies using dependency overrides. They might probe the difference between Depends and Security, or ask when to use dependencies with yield versus regular callables. Another follow-up could explore how global dependencies interact with path-specific ones, or how dependency sharing via Annotated changes code organization.
ONE CONCRETE EXAMPLE: Suppose you have a database session dependency and a user retrieval dependency that needs that session. You declare the session with Depends in the user dependency's parameters, then declare the user dependency with Depends in the path operation. FastAPI resolves the session first, injects it into the user callable, and finally injects the user into the endpoint. Using Annotated, you define the shared dependency once and reuse it across multiple routes, keeping the declaration DRY and letting OpenAPI reflect the requirements.
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.