📝 Document custom headers for JSONL and SSE streams - #15953
Open
Deadshot1831 wants to merge 1 commit into
Open
Conversation
Contributor
📝 Docs previewLast commit a9b2d87 at: https://b1ecfcaf.fastapitiangolo.pages.dev Modified Pages |
YuriiMotov
reviewed
Jul 7, 2026
Deadshot1831
force-pushed
the
deadshot-sse-jsonl-tests-docs
branch
from
July 7, 2026 19:03
88d2e76 to
a9b2d87
Compare
Author
|
a9b2d87 |
YuriiMotov
approved these changes
Jul 14, 2026
YuriiMotov
left a comment
Member
There was a problem hiding this comment.
@Deadshot1831, thanks!
Your version is technically precise, but it seems a bit hard to read, so I prepared an alternative version that might be simpler for newcomers.
Please, take a look or we can just leave it for Sebastian to decide.
Comment on lines
+76
to
+82
| With an SSE stream, the *path operation function* is a generator. Calling it creates the generator, but the code inside the generator body doesn't run until the response is already streaming. So setting headers inside the generator body is too late for headers that need to be sent with the response. | ||
|
|
||
| Instead, use a dependency that receives the `Response` parameter and sets the headers before the stream starts: | ||
|
|
||
| {* ../../docs_src/server_sent_events/tutorial006_py310.py ln[1:33] hl[3:4,22:23,26:30] *} | ||
|
|
||
| Dependencies run before FastAPI creates the streaming response, so FastAPI can copy those headers into the final SSE response. |
Member
There was a problem hiding this comment.
Suggested change
| With an SSE stream, the *path operation function* is a generator. Calling it creates the generator, but the code inside the generator body doesn't run until the response is already streaming. So setting headers inside the generator body is too late for headers that need to be sent with the response. | |
| Instead, use a dependency that receives the `Response` parameter and sets the headers before the stream starts: | |
| {* ../../docs_src/server_sent_events/tutorial006_py310.py ln[1:33] hl[3:4,22:23,26:30] *} | |
| Dependencies run before FastAPI creates the streaming response, so FastAPI can copy those headers into the final SSE response. | |
| With an SSE stream, the *path operation function* is a generator. FastAPI starts the response before the body of that generator is executed. Because the response headers have already been sent, modifying the `Response` object inside the endpoint function has no effect. | |
| Instead, use a dependency that receives the `Response` parameter and sets the headers before the response starts: | |
| {* ../../docs_src/server_sent_events/tutorial006_py310.py ln[1:33] hl[3:4,22:23,26:30] *} | |
| Dependencies are resolved before FastAPI starts sending the response, so headers they set on the `Response` object are included in the outgoing response. |
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.
Pull Request
Discussion: N/A - docs-only clarification with tutorial test coverage.
Description
This PR documents how to add custom response headers to JSON Lines and SSE streaming endpoints.
FastAPI already documents the common pattern in Response Headers: use a
Responseparameter and mutateresponse.headers. This PR adds the streaming-specific caveat. For generator endpoints, setting a header inside the generator body is too late because FastAPI has already prepared the streaming response before the generator starts yielding data.The examples use a dependency with a
Responseparameter to set the header before FastAPI creates the streaming response. Dependencies are resolved before the response is prepared, so the header is available at the correct point.Changes:
Validation
I did not run the full docs build locally because this venv does not have the full docs dependency group installed.