Under The Hood
backend fastapi FastAPI 0.100+

FastAPI Dependency Injection: Depends() and yield Context Cleanup

Last updated
Prerequisites:
Python Generators (`yield`)
FastAPI route handler basics
  • fastapi
  • dependency-injection
  • depends
  • yield
  • cleanup
  • database-session

Read at your depth

The practical view

FastAPI uses `Depends()` to inject shared dependencies into route handlers: `def get_db(): db = Session(); try: yield db; finally: db.close()`. In your endpoint: `@app.get('/users') def list_users(db: Session = Depends(get_db)): ...`. The code before `yield` runs before the route handler, and the code after `yield` (or inside `finally`) is guaranteed to run after the response is sent, ensuring safe resource cleanup.

The same idea in other frameworks

fastapi equivalent
`db: Session = Depends(get_db)` with `yield db` in generator.

Legacy vs modern

Manual Try/Finally DB Connection vs FastAPI yield Dependency

Manual session management in every route leaks connections on unhandled errors; `Depends()` with `yield` guarantees clean teardown.

before → after
Manual Session Management
db = Session(); try: ... finally: db.close(); // ❌ Boilerplate in every route handler
FastAPI Depends() with yield
def get_db(): db = SessionLocal(); try: yield db; finally: db.close(); // ✅ NEW: Auto-cleanup

Interview gotchas

Context

Interviewers test your knowledge of dependency memoization within a single request lifecycle.

The mechanical answer

`use_cache=True` ensures that if a dependency function (e.g. `get_current_user` or `get_db`) is referenced multiple times across sub-dependencies within the same request, **FastAPI executes the dependency function only once** and caches its result for the duration of that specific request. If you need a dependency to execute a fresh calculation every time it is injected, set `Depends(my_func, use_cache=False)`.

Trap

The cache is request-scoped; it never persists or leaks data across different HTTP requests.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.