Prefer pure ASGI middleware over Starlette BaseHTTPMiddleware
Answer: Starlette ships high-level BaseHTTPMiddleware and lower-level pure ASGI middleware. BaseHTTPMiddleware has some known limitations. Use pure ASGI middleware when a ContextVar must propagate. In FastAPI, a middleware is a function that works with every request, and also with every response before returning it.
BaseHTTPMiddleware limitations
Using BaseHTTPMiddleware will prevent changes to contextvars.ContextVar from propagating upwards. If you set a ContextVar in your endpoint and then read it from a middleware, you will not see the same value you set in the endpoint. If a BaseHTTPMiddleware sits earlier in the stack, it will disrupt contextvars propagation for later Pure ASGI Middleware that relies on them. To overcome these limitations, use pure ASGI middleware.
Pure ASGI middleware
The ASGI spec makes it possible to implement middleware with the ASGI interface itself, as a chain of applications that each call the next one. This is how middleware classes shipped with Starlette are implemented. The common pattern is a class that takes a parent app, then implements async def __call__(self, scope, receive, send).
| CORSMiddleware | SessionMiddleware | |---|---| | CORSMiddleware | Cross-origin headers | | SessionMiddleware | Signed cookie sessions | | HTTPSRedirectMiddleware | Redirect HTTP to HTTPS | | TrustedHostMiddleware | Allowed Host headers | | GZipMiddleware | GZipMiddleware | | BaseHTTPMiddleware | dispatch(request, call_next) |
FastAPI's hook
In FastAPI, create a middleware with the decorator @app.middleware("http"). The function receives the request and a call_next function that will receive the request as a parameter. You can also use the add_middleware method. Each new middleware wraps the application, forming a stack.
See also
Claims
- id: C1 claim: BaseHTTPMiddleware has known limitations around contextvars. source: S1 evidence: "the BaseHTTPMiddleware has some known limitations"
- id: C3 claim: Pure ASGI middleware is a chain of ASGI applications. source: S1 evidence: "as a chain of ASGI applications that call into the next one"
- id: C4 claim: FastAPI middleware runs on every request and every response. source: S2 evidence: "is a function that works with every request before it is processed"
Sources
- [S1] Middleware. Starlette docs. https://starlette.dev/middleware/ . License: BSD-3-Clause. Accessed 2026-11-19.
- [S2] Middleware. FastAPI docs. https://fastapi.tiangolo.com/tutorial/middleware/ . License: MIT. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Starlette docs, Middleware; FastAPI docs, Middleware. BSD-3-Clause; MIT. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Starlette docs, Middleware; FastAPI docs, Middleware. BSD-3-Clause; MIT.
Cite this
iinventedlight. 2026. "Prefer pure ASGI middleware over Starlette BaseHTTPMiddleware." iinventedlight Library, iil:note:aqzwynm7, version of 2026-11-19. https://iinventedlight.com/library/notes/starlette-basehttp-middleware/. License: CC-BY-4.0.
@misc{iil_note_aqzwynm7,
author = {{iinventedlight}},
title = {{Prefer pure ASGI middleware over Starlette BaseHTTPMiddleware}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/starlette-basehttp-middleware/},
note = {Permanent ID iil:note:aqzwynm7. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- starlette-basehttp-middleware.table.json (table; CC-BY-4.0)
- starlette-basehttp-middleware.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/starlette-basehttp-middleware.md.