How to Use Sub-Dependency in FastAPI: Simple Guide
In FastAPI, you can use
Depends inside another dependency function to create a sub-dependency. This allows you to compose dependencies by calling one dependency from another, making your code modular and reusable.Syntax
To use a sub-dependency in FastAPI, define a dependency function that itself uses Depends to call another dependency. Then, use Depends on the outer dependency in your path operation or another dependency.
- Sub-dependency function: A function that requires another dependency as a parameter using
Depends. - Outer dependency function: A function that depends on the sub-dependency.
- Path operation: Uses
Dependson the outer dependency to get all needed data.
python
from fastapi import Depends, FastAPI app = FastAPI() def sub_dependency(): return "sub-dependency value" def main_dependency(sub_dep: str = Depends(sub_dependency)): return f"main dependency got: {sub_dep}" @app.get("/") async def read_root(dep_value: str = Depends(main_dependency)): return {"message": dep_value}
Example
This example shows how a sub-dependency provides a value that the main dependency uses, and the path operation returns the combined result.
python
from fastapi import Depends, FastAPI app = FastAPI() def get_token(): # Imagine this gets a token from headers or elsewhere return "secure-token" def get_user(token: str = Depends(get_token)): # Use the token to get user info (mocked here) if token == "secure-token": return "user123" return "anonymous" @app.get("/profile") async def profile(user: str = Depends(get_user)): return {"user": user}
Output
{"user":"user123"}
Common Pitfalls
1. Forgetting to use Depends in the sub-dependency parameter: The sub-dependency must be wrapped in Depends to be injected properly.
2. Circular dependencies: Avoid dependencies that depend on each other directly or indirectly, as this causes errors.
3. Returning wrong types: Ensure your dependencies return the expected data type for the next dependency or path operation.
python
from fastapi import Depends, FastAPI app = FastAPI() # Wrong: missing Depends in parameter # def main_dep(sub_dep: str): # This will NOT work # return sub_dep # Correct: def sub_dep(): return "value" def main_dep(sub_dep: str = Depends(sub_dep)): return sub_dep @app.get("/test") async def test(dep: str = Depends(main_dep)): return {"dep": dep}
Quick Reference
- Use
Dependsto declare dependencies and sub-dependencies. - Sub-dependencies are just dependencies used inside other dependencies.
- Always wrap sub-dependency parameters with
Depends. - Avoid circular dependencies to prevent errors.
- Test your dependencies separately to ensure correct data flow.
Key Takeaways
Use Depends inside dependency functions to create sub-dependencies in FastAPI.
Always wrap sub-dependency parameters with Depends to enable injection.
Avoid circular dependencies to prevent runtime errors.
Sub-dependencies help keep your code modular and reusable.
Test dependencies independently to ensure they return expected values.