Skip to content
tezvyn:

How do you declare a function as a dependency, and why?

Source: fastapi.tiangolo.comEasyHow cards are made

How do you declare a function as a dependency, and why?

Tests FastAPI dependency injection basics. Answer: create a function, import Depends, and add it to path operation parameters so FastAPI injects it. Purpose: routes declare what they need instead of hard-coding shared logic.

What's really being asked

Whether you understand FastAPI's dependency injection system at a mechanical level. The interviewer wants to know if you can separate reusable logic from path operations and let the framework wire them together rather than hard-coding shared behavior inside every route. It also checks that you know the Depends import and that you grasp the inversion-of-control pattern.

The full answer

First, create a simple function that returns or validates something; this is the dependency, also called the dependable. The function can be sync or async because FastAPI handles both forms automatically. Second, import Depends from fastapi. Third, declare the dependency in the path operation parameters by assigning the parameter default to Depends(your_function) or by using Annotated with Depends for better type checking and sharing across endpoints. Fourth, explain the primary purpose: dependency injection means your path operation declares what it requires to work, and FastAPI takes care of doing whatever is needed to provide those dependencies. This keeps route handlers focused on HTTP concerns and pushes shared logic like authentication, database sessions, or query validation into reusable components that are easy to test and share.

The mistakes people make

Calling the dependency function manually inside the path operation body instead of letting FastAPI inject it. Describing dependencies as just imported helper functions that you invoke yourself. Conflating dependencies with middleware, which runs on every request globally rather than being declared per route or per router. Thinking dependencies are only for authentication or security. Forgetting that the dependency itself can use Depends to form sub-dependencies. Not mentioning that the system is integrated with OpenAPI and appears in the automatic documentation.

What usually comes next

How do sub-dependencies work when one Depends call uses another? Can a dependency be async and what are the performance implications? How do you share the same dependency across multiple routes without repeating the declaration? What is the difference between dependencies declared on the path operation function parameters versus the path operation decorator? How do dependencies with yield work for setup and cleanup, such as database sessions? When should you use a class instead of a function as a dependency?

A concrete example

Imagine a function named common_parameters that accepts skip and limit query parameters and returns a dictionary. You import Depends from fastapi. In your path operation, you write something like async def read_items(commons: dict = Depends(common_parameters)). When a client hits the endpoint, FastAPI recognizes the dependency, calls common_parameters with the incoming query values, and passes the result into read_items as the commons argument. The route itself does not need to know how the parameters were parsed or validated. If another endpoint needs the same behavior, you reuse the same function and FastAPI resolves it independently for each request.

Interview question

What is the main reason to declare a FastAPI dependency in a path operation parameter?

  • a.To let FastAPI automatically inject reusable logic so routes stay focused on HTTP concerns.Correct
  • b.To register global middleware that runs on every request before reaching the path operation.
  • c.To manually invoke a helper function inside the route body whenever shared logic is needed.
  • d.To enforce authentication exclusively because Depends is intended only for security-related checks.
Why?

The correct answer is C because dependency injection allows FastAPI to wire reusable components into routes, keeping handlers clean. A is tempting because it describes calling helpers, but that misses the inversion-of-control pattern where the framework injects the dependency rather than the route calling it manually.

Just read this? Test yourself on what you have been reading.

Read the original → fastapi.tiangolo.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.

See open roles