starlette-context
Middleware for Starlette that allows you to store and access the context data of a request. Can be used with logging so logs automatically use request headers such as x-request-id or x-correlation-id.
What it is and what it does
starlette-context is middleware for the Starlette web framework that manages request-scoped context—data that belongs to a single HTTP request and should be accessible throughout its lifecycle. It wraps Starlette's request handling to provide a thread-safe or async-safe context store, populated by configurable plugins that extract headers (like x-request-id or x-correlation-id) and make them available to your route handlers, logging, and other request-processing code without passing them explicitly through function parameters.
The package ships with built-in plugins for common patterns like request ID and correlation ID extraction, and you can write custom plugins to populate context with application-specific data. This is particularly useful for distributed tracing, structured logging, and audit trails where you want request identifiers to flow automatically through logs and downstream services. It integrates cleanly with Starlette's middleware stack and works with FastAPI applications built on Starlette.
Use it for:
- Automatic request ID injection into logs so all log entries for a request share a common identifier without manual threading.
- Distributed tracing: propagate correlation IDs through microservices by extracting them from incoming headers and making them available to outbound calls.
- Audit logging: store user ID or tenant ID in context during request processing and access it in database operations or business logic without parameter passing.
- Multi-tenant applications: extract and store tenant context from request headers to enforce isolation in downstream database queries or API calls.
- Request metadata enrichment: populate context with parsed authentication tokens, feature flags, or client metadata for use across handlers.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Middleware for Starlette that stores and accesses request context data, enabling automatic injection of request headers like x-request-id or x-correlation-id into logs and application state.
Yes. The package is actively maintained, has no known vulnerabilities, low install friction, and solves a real problem in web applications—making request-scoped data available throughout the request lifecycle without manual parameter threading. It is well-suited for any Starlette or FastAPI application that needs structured logging, distributed tracing, or request-scoped state management.
Install
starlette-context on PyPI
pip
pip install starlette-contextuv
uv add starlette-contextpoetry
poetry add starlette-contextInstalling starlette-context
Before you install
Low friction: pure Python wheel with a single runtime dependency on starlette>=0.27.0. Actively maintained with a recent release and no known vulnerabilities.
License in practice
MIT license permits commercial and private use with minimal restrictions; you may use, modify, and distribute this package freely provided you include the license notice.
Quickstart
pip install starlette-context
from starlette_context import context, plugins
from starlette_context.middleware import ContextMiddleware
from starlette.applications import Starlette
from starlette.middleware import Middleware
middleware = [
Middleware(
ContextMiddleware,
plugins=(plugins.RequestIdPlugin(), plugins.CorrelationIdPlugin())
)
]
app = Starlette(middleware=middleware)
context["key"] = "value" # Access in request handlers
Requires Python 3.10 or later and starlette>=0.27.0.
Verify before relying
- Whether the package supports async context isolation across concurrent requests without thread-local or context-var conflicts.
- Performance overhead of middleware on high-throughput applications.
- Compatibility with other context-aware middleware or frameworks that also manage request state.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — starlette |
| Maintenance | actively maintained — 167 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 2,367,211/month — #3,102 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: starlette_context-0.5.1-py3-none-any.whl
Keywords: fastapi, middleware, starlette
Tags
More Internet packages
Botocore provides low-level, data-driven access…
permissive · top 100 on PyPI
aiobotocoreProvides an async client for AWS services using…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
filelockProvides a platform-independent file locking…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
googleapis-common-protosProvides common Protocol Buffer message…
permissive · top 100 on PyPI
asgi-correlation-idASGI middleware that reads or generates…
permissive · top 5,000 on PyPI
starlette-csrfStarlette middleware that protects web…
permissive · top 15,000 on PyPI
SecwebSecweb applies security headers (CSP, HSTS,…
copyleft · top 15,000 on PyPI
starlette-compressMiddleware for Starlette and FastAPI that…
permissive · top 5,000 on PyPI
starsessionsProvides session management middleware for…
permissive · top 15,000 on PyPI
starletteStarlette is a lightweight ASGI framework for…
permissive · top 100 on PyPI
starlette-cramjamMiddleware for Starlette that compresses HTTP…
permissive · top 15,000 on PyPI
fastapi-eventsFastapi-events is an event dispatching and…
permissive · top 15,000 on PyPI
flask-request-id-headerFlask middleware that automatically adds or…
unclear · top 15,000 on PyPI
opentelemetry-instrumentation-asgiProvides ASGI middleware for OpenTelemetry that…
permissive · top 1,000 on PyPI