Skip to main content

request-id-helper

Context-local request IDs for Python logging and async applications.

Requirements

  • Python 3.11–3.15

Installation

uv add request-id-helper
pip install request-id-helper

Logging and scoped request IDs

Configure a formatter that includes %(request_id)s, then decorate each request entry point. The generated ID is available for the complete call and is restored when the call finishes, raises, or is cancelled.

import logging

from request_id_helper import RequestIdFormatter, request_id_ctx, set_request_id

handler = logging.StreamHandler()
handler.setFormatter(RequestIdFormatter("%(levelname)s [%(request_id)s] %(message)s"))
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.INFO)


@set_request_id()
def handle_request() -> None:
    logger.info("handling request")
    assert request_id_ctx.get()

set_request_id() supports async def functions as well. Each decorated call receives an independent ID, including concurrent asyncio tasks. Nested decorated calls temporarily replace the outer ID and restore it when they return.

Existing logging dictionaries

Use init_logger() with an existing logging.config.dictConfig dictionary to install request-ID record enrichment. It composes with an already configured log-record factory and never overwrites a record that already has request_id.

from request_id_helper import init_logger

init_logger(
    {
        "version": 1,
        "disable_existing_loggers": False,
        "formatters": {
            "default": {"format": "%(levelname)s [%(request_id)s] %(message)s"}
        },
    }
)

RequestIdFormatter is an alternative for individual handlers. It uses N/A when a record has no request ID.

Direct context propagation

Read the current value when making an outbound call:

from request_id_helper import request_id_ctx

headers = {"x-request-id": request_id_ctx.get()}

Workers can restore a propagated value with request_id_ctx.set(serialized_request_id). Call request_id_ctx.reset() when the direct binding is no longer needed.

Examples

uv run python examples/basic_usage.py
uv run python examples/async_propagation.py

Development

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

License

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

Reporting a security vulnerability

See the security policy.

Metadata

Release files for request-id-helper 1.0.3

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

Source distribution (sdist)

Source distribution for request-id-helper 1.0.3
File Size Uploaded Provenance Actions
request_id_helper-1.0.3.tar.gz 50.1 kB With provenance Details for request_id_helper-1.0.3.tar.gz

Built distribution (wheel)

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

Total release size: 55.5 kB

Release files / request_id_helper-1.0.3.tar.gz

Download URL request_id_helper-1.0.3.tar.gz
Size 50.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2dea6b6ffa876420f56b02122aa5f68b1a2f129b2fd9e4e7b483d66b1a54e9e6
BLAKE2b-256 checksum
How to use checksums
84b108b33aef37351c5d168ce1dc09481440be19d89e7e462083701f04ceebaa
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 / request_id_helper-1.0.3-py3-none-any.whl

Download URL request_id_helper-1.0.3-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0e89ab6ab69200a058cc1ec87410e8a262e11200afbb14d9f2bbbb49ba4a062b
BLAKE2b-256 checksum
How to use checksums
4ec6846077ca32845ada6a1d0cedcfbb656d67ecfc6322285b1a13d789dceeaa
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 request-id-helper 1.0.3

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 request_id_helper-1.0.3.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