Skip to main content

request-id-helper

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

Requirements

  • Python 3.11–3.14

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.

Release files for request-id-helper 1.0.0

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.0
File Size Uploaded
request_id_helper-1.0.0.tar.gz 52.6 kB Details

Built distribution (wheel)

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

Total release size: 61.3 kB

Release files / request_id_helper-1.0.0.tar.gz

Download URL request_id_helper-1.0.0.tar.gz
Size 52.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d318584912a991a034fbe8127893adc67405e356ebf476deb12a725ecd0fad7d
BLAKE2b-256 checksum
How to use checksums
cf02bddedd9a88e93367a9dbe7d86e4b9666f7c8e97268d5634d399a7a1794b0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

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 Jul 20, 2026.

Transparency log

Release files / request_id_helper-1.0.0-py3-none-any.whl

Download URL request_id_helper-1.0.0-py3-none-any.whl
Size 8.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8505ca5506eab654a2778c5956cd745c13d743d98963e8b7b5a8cfc87bbade7e
BLAKE2b-256 checksum
How to use checksums
1c7837d71bcf483a26ded4d61cfe0abb79eaf4e25b1eee72d907d47c13cb73ab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.14

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 Jul 20, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.2.0

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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