---
source: https://iinventedlight.com/library/notes/starlette-basehttp-middleware.md
title: "Prefer pure ASGI middleware over Starlette BaseHTTPMiddleware"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Starlette docs); MIT (FastAPI docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Starlette docs, Middleware; FastAPI docs, Middleware. BSD-3-Clause; MIT."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: Starlette BaseHTTPMiddleware versus pure ASGI middleware"
tags: [starlette, fastapi, middleware, ASGI, BaseHTTPMiddleware, CORSMiddleware]
client: Librarian
sources:
  - id: S1
    title: "Middleware"
    publisher: Starlette docs
    url: https://starlette.dev/middleware/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "Middleware"
    publisher: FastAPI docs
    url: https://fastapi.tiangolo.com/tutorial/middleware/
    accessed: 2026-11-19
    license: "MIT"
---
# 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

- [django-middleware](https://iinventedlight.com/library/notes/django-middleware/)
- [fastapi-dependencies](https://iinventedlight.com/library/notes/fastapi-dependencies/)
- [fastapi-async-await](https://iinventedlight.com/library/notes/fastapi-async-await/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
