Skip to content

fix(fastapi): read the request id from the header it is echoed in - #117

Merged
AlexeyShalaev merged 1 commit into
masterfrom
fix/request-id-header
Nov 13, 2026
Merged

AlexeyShalaev merged 1 commit into
masterfrom
fix/request-id-header

Conversation

@AlexeyShalaev

@AlexeyShalaev AlexeyShalaev commented Nov 13, 2026 •

Copy link
Copy Markdown
Member

Summary

CorrelationIdMiddlewareConfig.header_name used to change only the header the request id was echoed in. ContextMiddleware built its default extractors with a literal x-request-id, and used request_id_header only for the echo. With header_name="X-Correlation-ID", the example on the FastAPI page, a caller's X-Correlation-ID: abc was ignored and a new id came back under that header. A caller's X-Request-ID: abc was kept, but it came back under the other header.

Now, when no header mapping is given, the request id is read from request_id_header, the header it is echoed in. The default config doesn't change, because both are still x-request-id. An explicit context_headers mapping from #115 stays as given, and the docs say to name the request id header in it when both are set.

Behaviour change for anyone who set header_name: an incoming X-Request-ID is no longer read, so the service mints a new id instead. That is what the option was documented to do, which is why this is a fix.

Tests: one test sends X-Correlation-ID and checks that it binds and comes back, and another sends X-Request-ID with a custom header and checks that a fresh id is minted and echoed. Both fail on #115's branch. The existing default-header tests are unchanged and pass.

Docs: the FastAPI page ("The request id" section), the agents page, the CorrelationIdMiddlewareConfig and context_headers docstrings, and the settings descriptions.

Type of change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Refactoring / internal

Checklist

  • Tests added or updated
  • make check passes locally (ruff + mypy)
  • CHANGELOG.md left alone: Release Please generates it from the Conventional Commit subjects
  • Documentation updated (if the public API changed), docs/agents.md included

make test-unit: 813 passed, 14 skipped. make test-integration: 10 passed. The docs build reports no issues.

Related issues

Closes #116

@AlexeyShalaev
AlexeyShalaev merged commit acc3254 into master Nov 13, 2026
7 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the fix/request-id-header branch November 13, 2026 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CorrelationIdMiddlewareConfig.header_name changes the echoed header but not the one the request id is read from

1 participant