Skip to main content

starlette-request-id

Request-ID context and response propagation middleware for Starlette applications.

Requirements

  • Python 3.11+ (CI validates 3.11, 3.12, 3.13, 3.14, and 3.15)
  • Starlette >1.2.0 and <2.0.0

Install

uv add starlette-request-id
pip install starlette-request-id

Basic usage

from starlette.applications import Starlette

from starlette_request_id import RequestIdMiddleware

app = Starlette()
app.add_middleware(RequestIdMiddleware)

RequestIdMiddleware reads x-request-id, creates a UUID4 ID if it is absent, exposes it through request_id_ctx, and writes the active value to the response header. See examples/basic_usage.py for logging and outbound-header propagation.

Examples

Run the standalone example:

uv run --with uvicorn examples/basic_usage.py

Run the dedicated Uvicorn example:

uv run --with 'uvicorn[standard]' uvicorn examples.uvicorn_runner:app --reload

Run the Gunicorn example with Uvicorn workers:

uv run --with gunicorn --with uvicorn gunicorn -c examples/gunicorn_conf.py examples.gunicorn_runner:app

Then send a request with a chosen ID:

curl -H 'x-request-id: example-id' http://127.0.0.1:8000/

Starlette 1.3 uses app.add_route(path, endpoint) to register handlers; the examples follow that API.

Configuration

app.add_middleware(
    RequestIdMiddleware,
    id_header="x-correlation-id",
    get_default_id_func=lambda: "generated-id",
)

A supplied value is preserved exactly; the generator is called only when the header is absent.

Logging

from starlette_request_id import init_logger, request_id_ctx

These exports are the shared APIs from request-id-helper, so applications that import request_id_ctx directly from request_id_helper observe the same request value.

Error responses and middleware order

The middleware adds the header to response starts emitted by the application and its inner exception handlers. It does not render exceptions. If an outer error middleware constructs a response after an exception escapes RequestIdMiddleware, put that error middleware inside RequestIdMiddleware or make it write the request-ID header itself.

Migrating from 1.x

2.0.0 drops Python 3.8–3.10 and replaces the internal BaseHTTPMiddleware implementation with pure ASGI middleware. Existing imports of RequestIdMiddleware, REQUEST_ID_HEADER, request_id_ctx, init_logger, and LogExtraFactory continue to work. Update deployment environments to Python 3.11+, use Starlette's add_route() API, and retain request-id-helper as the shared logging/context provider.

Development

uv sync --all-groups
make lint
make test
make build

License

starlette-request-id is distributed under the Apache License 2.0.

Metadata

Release files for starlette-request-id 2.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for starlette-request-id 2.1.2
File Size Uploaded Provenance Actions
starlette_request_id-2.1.2.tar.gz 45.6 kB With provenance Details for starlette_request_id-2.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for starlette-request-id 2.1.2
File Interpreter ABI Platform Actions
starlette_request_id-2.1.2-py3-none-any.whl Python 3 none any Details for starlette_request_id-2.1.2-py3-none-any.whl

Total release size: 50.4 kB

Release files / starlette_request_id-2.1.2.tar.gz

Download URL starlette_request_id-2.1.2.tar.gz
Size 45.6 kB
Tags Source
SHA-256 checksum
How to use checksums
6ef11e25745ababec1ed3efc713cb82325d27cf1c026173b0edf1230ee276a7b
BLAKE2b-256 checksum
How to use checksums
5cc15dc4e187a55b9d395c794e64ad93fb4bfca98b87f74b9ed7864e3f968c81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Release files / starlette_request_id-2.1.2-py3-none-any.whl

Download URL starlette_request_id-2.1.2-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
82464dbac6fbb57d481924c14e26bd12e6a41b04208707816353e30ee851d1c4
BLAKE2b-256 checksum
How to use checksums
0a2010a017f6b3b6d989ea1a5920ba32798ef0c7055babe0ab8f9eb54c03e6c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Security for starlette-request-id 2.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for starlette_request_id-2.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page