Jump to solution
Verify

The Fix

pip install fastapi==0.128.4

Based on closed fastapi/fastapi issue #14316 · PR/commit linked

Production note: This usually shows up under retries/timeouts. Treat it as a side-effect risk until you can verify behavior with a canary + real traffic.

Jump to Verify Open PR/Commit
@@ -129,7 +129,10 @@ def get_parameterless_sub_dependant(*, depends: params.Depends, path: str) -> De use_security_scopes.extend(depends.scopes) return get_dependant( - path=path, call=depends.dependency, security_scopes=use_security_scopes + path=path, + call=depends.dependency,
repro.py
from types import NoneType from typing import Iterator, Annotated from fastapi import APIRouter from fastapi import Depends from fastapi import FastAPI app = FastAPI() class ApplicationError(Exception): ... def dependency_with_cleanup_error() -> Iterator[None]: yield raise ApplicationError() router = APIRouter( dependencies=[Depends(dependency_with_cleanup_error, scope="function")] ) @router.get("/broken") def broken(): # Will return 200, and then fail in the cleanup. return {"status": "ok"} @router.get("/works") def endpoint( v: Annotated[NoneType, Depends(dependency_with_cleanup_error, scope="function")], ): # Will correctly return 500 return {"status": "ok"} app.include_router(router)
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Upgrade to fixed release\npip install fastapi==0.128.4\nWhen NOT to use: Do not use this fix if you rely on the previous cleanup behavior of dependencies.\n\n

Why This Fix Works in Production

  • Trigger: `Depends(func, scope="function")` with `yield` dependency exits after the request is sent instead of after the function
  • Mechanism: Function-scoped dependencies were not cleaned up correctly after the response was sent
  • Why the fix works: Enables proper support for `scope="function"` on `APIRoute` dependencies by passing the dependency’s scope through to `get_dependant`. (first fixed release: 0.128.4).

Why This Breaks in Prod

  • Function-scoped dependencies were not cleaned up correctly after the response was sent
  • Production symptom (often without a traceback): class ApplicationError(Exception): ...

Proof / Evidence

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“This was solved by https://github.com/fastapi/fastapi/pull/14301, released in FastAPI 0.121.1. 🐛”
@tiangolo · 2025-11-08 · confirmation · source

Failure Signature (Search String)

  • `Depends(func, scope="function")` with `yield` dependency exits after the request is sent instead of after the function
Copy-friendly signature
signature.txt
Failure Signature ----------------- class ApplicationError(Exception): ... def dependency_with_cleanup_error() -> Iterator[None]:

Error Message

Signature-only (no traceback captured)
error.txt
Error Message ------------- class ApplicationError(Exception): ... def dependency_with_cleanup_error() -> Iterator[None]:

Minimal Reproduction

repro.py
from types import NoneType from typing import Iterator, Annotated from fastapi import APIRouter from fastapi import Depends from fastapi import FastAPI app = FastAPI() class ApplicationError(Exception): ... def dependency_with_cleanup_error() -> Iterator[None]: yield raise ApplicationError() router = APIRouter( dependencies=[Depends(dependency_with_cleanup_error, scope="function")] ) @router.get("/broken") def broken(): # Will return 200, and then fail in the cleanup. return {"status": "ok"} @router.get("/works") def endpoint( v: Annotated[NoneType, Depends(dependency_with_cleanup_error, scope="function")], ): # Will correctly return 500 return {"status": "ok"} app.include_router(router)

What Broke

Users experienced unexpected behavior with function-scoped dependencies in FastAPI routes.

Why It Broke

Function-scoped dependencies were not cleaned up correctly after the response was sent

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

pip install fastapi==0.128.4

When NOT to use: Do not use this fix if you rely on the previous cleanup behavior of dependencies.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

Fix reference: https://github.com/fastapi/fastapi/pull/14301

First fixed release: 0.128.4

Last verified: 2026-02-08. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • Do not use this fix if you rely on the previous cleanup behavior of dependencies.

Verify Fix

verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Capture the exact failing error string in logs and tests so you can reproduce via a minimal script.
  • Pin production dependencies and upgrade only with a reproducible test that hits the failing path.

Version Compatibility Table

VersionStatus
0.128.4 Fixed

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.