The Fix
Upgrade to version 0.19.0 or later.
Based on closed Kludex/starlette issue #1433 · PR/commit linked
Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.
@@ -52,6 +52,9 @@ async def body_stream() -> typing.AsyncGenerator[bytes, None]:
yield message.get("body", b"")
+ if app_exc is not None:
+ raise app_exc
+
import httpx
import pytest
from fastapi import FastAPI, APIRouter
from starlette.middleware import Middleware
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Route
class SomeError(Exception):
pass
class SomeMiddleware(BaseHTTPMiddleware):
async def dispatch(
self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
return await call_next(request)
# Drop (or use not BaseHTTPMiddleware based) middleware and test works fine
app = FastAPI(middleware=[Middleware(SomeMiddleware), ])
async def simple_route(request: Request):
raise SomeError
another_router = APIRouter(
routes=[Route('/simple-route/', simple_route, methods=['GET'])]
)
sub_app = FastAPI()
sub_app.include_router(another_router)
app.router.mount(f'/api', sub_app)
@pytest.mark.asyncio
async def test_simple_route():
async with httpx.AsyncClient(app=app) as client:
with pytest.raises(SomeError):
await client.get("http://testserver/api/simple-route/")
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\nUpgrade to version 0.19.0 or later.\nWhen NOT to use: Do not use this fix if your application relies on the previous behavior of suppressing exceptions.\n\n
Why This Fix Works in Production
- Trigger: Raising Exceptions in sub-applications routes
- Mechanism: BaseHTTPMiddleware does not correctly raise exceptions from StreamingResponse in sub-applications
- Why the fix works: Prevent BaseHTTPMiddleware from hiding errors of StreamingResponse, ensuring exceptions are raised correctly in sub-applications. (first fixed release: 0.19.0).
- If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.
Why This Breaks in Prod
- Shows up under Python 3.9.9 in real deployments (not just unit tests).
- BaseHTTPMiddleware does not correctly raise exceptions from StreamingResponse in sub-applications
- Production symptom (often without a traceback): Raising Exceptions in sub-applications routes
Proof / Evidence
- GitHub issue: #1433
- Fix PR: https://github.com/kludex/starlette/pull/1459
- First fixed release: 0.19.0
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.85
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.43
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“### Checklist - [X] The bug is reproducible against the latest release or master. - [X] There are no similar issues or pull requests to fix it yet. ### Describe the bug Let's start with this PR: #1262 It's about preventing raise anyio.Excep”
Failure Signature (Search String)
- Raising Exceptions in sub-applications routes
- It's about preventing raise `anyio.ExceptionGroup` in views under a `BaseHTTPMiddleware`. PR resolve that problem with nonlocal variable that stores our exception. But in the case
Copy-friendly signature
Failure Signature
-----------------
Raising Exceptions in sub-applications routes
It's about preventing raise `anyio.ExceptionGroup` in views under a `BaseHTTPMiddleware`. PR resolve that problem with nonlocal variable that stores our exception. But in the case of sub-applications, it does not work.
Error Message
Signature-only (no traceback captured)
Error Message
-------------
Raising Exceptions in sub-applications routes
It's about preventing raise `anyio.ExceptionGroup` in views under a `BaseHTTPMiddleware`. PR resolve that problem with nonlocal variable that stores our exception. But in the case of sub-applications, it does not work.
Minimal Reproduction
import httpx
import pytest
from fastapi import FastAPI, APIRouter
from starlette.middleware import Middleware
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Route
class SomeError(Exception):
pass
class SomeMiddleware(BaseHTTPMiddleware):
async def dispatch(
self, request: Request, call_next: RequestResponseEndpoint
) -> Response:
return await call_next(request)
# Drop (or use not BaseHTTPMiddleware based) middleware and test works fine
app = FastAPI(middleware=[Middleware(SomeMiddleware), ])
async def simple_route(request: Request):
raise SomeError
another_router = APIRouter(
routes=[Route('/simple-route/', simple_route, methods=['GET'])]
)
sub_app = FastAPI()
sub_app.include_router(another_router)
app.router.mount(f'/api', sub_app)
@pytest.mark.asyncio
async def test_simple_route():
async with httpx.AsyncClient(app=app) as client:
with pytest.raises(SomeError):
await client.get("http://testserver/api/simple-route/")
Environment
- Python: 3.9.9
What Broke
Exceptions raised in sub-application routes are not propagated, leading to silent failures.
Why It Broke
BaseHTTPMiddleware does not correctly raise exceptions from StreamingResponse in sub-applications
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
Upgrade to version 0.19.0 or later.
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Fix reference: https://github.com/kludex/starlette/pull/1459
First fixed release: 0.19.0
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not use this fix if your application relies on the previous behavior of suppressing exceptions.
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.19.0 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.