Skip to content

🐛 Fix status_code being ignored for SSE and JSONL streaming endpoints#15937

Open
SAURABHSALVE wants to merge 1 commit into
fastapi:masterfrom
SAURABHSALVE:codex/fix-stream-status-code
Open

🐛 Fix status_code being ignored for SSE and JSONL streaming endpoints#15937
SAURABHSALVE wants to merge 1 commit into
fastapi:masterfrom
SAURABHSALVE:codex/fix-stream-status-code

Conversation

@SAURABHSALVE

Copy link
Copy Markdown

SSE and JSONL streaming endpoints ignore the status_code declared on the path operation and any status code set by a dependency on the Response parameter, always returning 200 while the generated OpenAPI documents the declared code. That makes the schema contradict the actual behavior.

@app.post(/events, response_class=EventSourceResponse, status_code=201)
async def events() -> AsyncIterable[str]:
    yield created
# returns 200, OpenAPI documents 201

Raw streaming responses (response_class=StreamingResponse) already handle both declared and dependency-set status codes via _build_response_args().

This PR applies the same _build_response_args() path to the SSE and JSONL branches. Default behavior is unchanged because the dependency solver's temporary response starts with status_code = None, so no status is injected unless the user declared or set one.

Adds tests/test_stream_status_code.py covering:

  • declared status codes for SSE and JSONL streams
  • dependency-set status codes for SSE and JSONL streams
  • raw streaming as the existing control case
  • default 200 behavior for both stream encodings
  • OpenAPI status-code consistency

@codspeed-hq

codspeed-hq Bot commented Jul 5, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing SAURABHSALVE:codex/fix-stream-status-code (8c2b6c8) with master (787e11b)1

Open in CodSpeed

Footnotes

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

@SAURABHSALVE SAURABHSALVE force-pushed the codex/fix-stream-status-code branch from d41bce7 to 8c2b6c8 Compare July 5, 2026 11:55
@SAURABHSALVE

Copy link
Copy Markdown
Author

Hi maintainers, all code/test checks are passing now. The only failing required check is Labels / check-labels because this PR has no required category label.
Could someone with triage permissions please add the bug label?
This PR fixes a runtime bug where SSE and JSONL streaming endpoints return 200 even when the path operation declares another status_code or a dependency sets Response.status_code, while OpenAPI documents the non-200 status.

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.

1 participant