skillfed

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.

starlette-context v0.5.1 2.4M downloads/30d#3,102 on PyPI611
Permissive license MIT Active released

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-context

uv

uv add starlette-context

poetry

poetry add starlette-context

Installing 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

Development Status :: 4 - BetaIntended Audience :: DevelopersIntended Audience :: Information TechnologyIntended Audience :: System AdministratorsLicense :: OSI Approved :: MIT LicenseProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Topic :: Internet

Tags

starlette request context middlewarerequest id correlation id loggingstarlette context storagerequest header injection middlewarefastapi request contextstarlette middleware pluginsrequest tracing headers
request-contextdistributed-tracinglogging-integration

More Internet packages