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.
@@ -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,
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)
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
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
- GitHub issue: #14316
- Fix PR: https://github.com/fastapi/fastapi/pull/14301
- First fixed release: 0.128.4
- Reproduced locally: No (not executed)
- Last verified: 2026-02-08
- Confidence: 0.85
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.53
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. 🐛”
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
Failure Signature
-----------------
class ApplicationError(Exception): ...
def dependency_with_cleanup_error() -> Iterator[None]:
Error Message
Signature-only (no traceback captured)
Error Message
-------------
class ApplicationError(Exception): ...
def dependency_with_cleanup_error() -> Iterator[None]:
Minimal Reproduction
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
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.
When NOT to Use This Fix
- Do not use this fix if you rely on the previous cleanup behavior of dependencies.
Verify Fix
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
| Version | Status |
|---|---|
| 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.