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.

Metadata

Release files for request-id-helper 1.0.1

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.1
File Size Uploaded
request_id_helper-1.0.1.tar.gz 49.1 kB Details

Built distribution (wheel)

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

Total release size: 54.4 kB

Release files / request_id_helper-1.0.1.tar.gz

Download URL request_id_helper-1.0.1.tar.gz
Size 49.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3e5e0cf5b1de5a762f2ebbb941247f65f5eee42d85830cd201319e43942a628d
BLAKE2b-256 checksum
How to use checksums
fcb8d487430a827d47df2fd3684408e0f5ee4446005c82ed0fa2f368582a1eb3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.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 Sep 27, 2026.

Transparency log

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

Download URL request_id_helper-1.0.1-py3-none-any.whl
Size 5.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2d042e87a97a1c1211cf35f1c547f64c93ee71e3a140ac680ccab312ea0b8366
BLAKE2b-256 checksum
How to use checksums
2c788a1f700cf819c79a4f0b9c64eca3e0222dc2d5cba8db5e8a5c1f398dd580
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.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 Sep 27, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

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