Repository navigation
fix(fastapi): read the request id from the header it is echoed in - #117
Merged
Merged
Conversation
AlexeyShalaev
force-pushed
the
fix/request-id-header
branch
from
November 13, 2026 08:46
1a8b8c0 to
06f8eb6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CorrelationIdMiddlewareConfig.header_nameused to change only the header the request id was echoed in.ContextMiddlewarebuilt its default extractors with a literalx-request-id, and usedrequest_id_headeronly for the echo. Withheader_name="X-Correlation-ID", the example on the FastAPI page, a caller'sX-Correlation-ID: abcwas ignored and a new id came back under that header. A caller'sX-Request-ID: abcwas 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 stillx-request-id. An explicitcontext_headersmapping 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 incomingX-Request-IDis 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 afix.Tests: one test sends
X-Correlation-IDand checks that it binds and comes back, and another sendsX-Request-IDwith 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
CorrelationIdMiddlewareConfigandcontext_headersdocstrings, and the settings descriptions.Type of change
Checklist
make checkpasses locally (ruff+mypy)CHANGELOG.mdleft alone: Release Please generates it from the Conventional Commit subjectsdocs/agents.mdincludedmake test-unit: 813 passed, 14 skipped.make test-integration: 10 passed. The docs build reports no issues.Related issues
Closes #116