← 返回首页
📝 Document custom headers for JSONL and SSE streams by Deadshot1831 · Pull Request #15953 · fastapi/fastapi · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

📝 Document custom headers for JSONL and SSE streams#15953

Open
Deadshot1831 wants to merge 1 commit into
fastapi:masterfrom
Deadshot1831:deadshot-sse-jsonl-tests-docs
Open

📝 Document custom headers for JSONL and SSE streams#15953
Deadshot1831 wants to merge 1 commit into
fastapi:masterfrom
Deadshot1831:deadshot-sse-jsonl-tests-docs

Conversation

Deadshot1831 commented Jul 7, 2026
edited
Loading

Copy link
Copy Markdown

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 Response parameter and mutate response.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 Response parameter 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:

  • Add a JSON Lines tutorial section and runnable docs example for custom stream headers.
  • Add an SSE tutorial section and runnable docs example for custom stream headers.
  • Add tutorial tests that verify the custom header is present, the response media type is still correct, the stream body is produced, and OpenAPI keeps the expected streaming schema.

Validation

python -m pytest tests/test_tutorial/test_stream_json_lines tests/test_tutorial/test_server_sent_events tests/test_sse.py tests/test_stream_bare_type.py tests/test_stream_json_validation_error.py -q 52 passed python -m ruff check docs_src/stream_json_lines/tutorial002_py310.py docs_src/server_sent_events/tutorial006_py310.py tests/test_tutorial/test_stream_json_lines/test_tutorial002.py tests/test_tutorial/test_server_sent_events/test_tutorial006.py All checks passed git diff --check

I did not run the full docs build locally because this venv does not have the full docs dependency group installed.

github-actions Bot added the docs Documentation about how to use FastAPI label Jul 7, 2026

codspeed-hq Bot commented Jul 7, 2026
edited
Loading

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 20 untouched benchmarks

Comparing Deadshot1831:deadshot-sse-jsonl-tests-docs (a9b2d87) with master (1e60e32)1

Footnotes

  1. No successful run was found on master (7cb06f3) during the generation of this report, so 1e60e32 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

github-actions Bot commented Jul 7, 2026
edited
Loading

Copy link
Copy Markdown
Contributor

Comment on lines +74 to +80
If you need to add custom headers to an SSE response, use a dependency that receives a `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] *}

The dependency runs before FastAPI creates the streaming response.

This is different from setting headers inside the generator body, which runs later, when the response is already streaming.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

I think this should be explained in a different way.

I would go with:

What do you think?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality Hide comment

Yes, that makes sense. I was focusing more on the final pattern, but starting from the existing Response Headers docs makes the flow clearer.
I’ll adjust the explanation so it first points to the usual Response parameter approach, then explains why setting headers inside a streaming generator is too late, and then introduces the dependency approach. The key reason that works is that dependencies run before FastAPI creates the streaming response, so the headers are set before streaming starts.

Deadshot1831 force-pushed the deadshot-sse-jsonl-tests-docs branch from 88d2e76 to a9b2d87 Compare July 7, 2026 19:03
github-actions Bot removed the waiting label Jul 7, 2026
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

Labels

docs Documentation about how to use FastAPI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Footer

© 2026 GitHub, Inc.