Skip to content

🐛 Fix response_model_include/exclude ignored for ServerSentEvent data - #16147

Closed
Shriprasad-P wants to merge 1 commit into
fastapi:masterfrom
Shriprasad-P:fix/sse-response-model-include-exclude
Closed

Shriprasad-P wants to merge 1 commit into
fastapi:masterfrom
Shriprasad-P:fix/sse-response-model-include-exclude

Conversation

@Shriprasad-P

Copy link
Copy Markdown

Problem

ServerSentEvent(data=...) serializes payloads via model_dump_json() /
jsonable_encoder() without forwarding route-level
response_model_include or response_model_exclude.

Plain stream items already pass these options through
ModelField.serialize_json(), so the same model can serialize differently
depending on how it is yielded:

class Item(BaseModel):
    name: str
    description: str | None = None

@app.get("/a", response_class=EventSourceResponse, response_model_include={"name"})
async def a() -> AsyncIterable[Item]:
    yield Item(name="x", description="hidden")
    # data: {"name":"x"}

@app.get("/b", response_class=EventSourceResponse, response_model_include={"name"})
async def b():
    yield ServerSentEvent(data=Item(name="x", description="hidden"))
    # data: {"name":"x","description":"hidden"}  ← include ignored

Fix

Forward include=response_model_include and exclude=response_model_exclude
in the explicit ServerSentEvent branch for both:

  • Pydantic models → model_dump_json(...)
  • non-model data → jsonable_encoder(...)

raw_data, strings, event metadata (event / id / retry / comment),
and non-SSE response serialization are unchanged.

This is intentionally scoped to include/exclude only, so it can coexist with:

Tests

Added regressions in tests/test_sse.py covering:

  • response_model_include on ServerSentEvent(data=model)
  • response_model_exclude on ServerSentEvent(data=model)
  • include/exclude for dict payloads via jsonable_encoder
  • plain yield vs ServerSentEvent-wrapped parity
  • event meta / raw_data / string data unchanged

The new tests fail on current master and pass with this change.

Checks

uv run --no-sync pytest -q tests/test_sse.py
uv run --no-sync ruff check fastapi/routing.py tests/test_sse.py
uv run --no-sync ruff format fastapi/routing.py tests/test_sse.py --check
uv run --no-sync mypy fastapi/routing.py
uv run --no-sync ty check fastapi/routing.py

Checklist

  • The changes fail on the main branch and pass on this PR
  • I added tests covering the bug
  • Coverage / focused checks pass locally
  • Docs update not needed (behavior aligns existing response_model_* options)

ServerSentEvent(data=...) skipped route-level include/exclude when
serializing models and dicts, unlike plain streamed items.
@codspeed

codspeed Bot commented Aug 5, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing Shriprasad-P:fix/sse-response-model-include-exclude (dcd212c) with master (84fc386)1

Open in CodSpeed

Footnotes

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

@YuriiMotov

Copy link
Copy Markdown
Member

Thanks for your interest and efforts, but let's keep it closed until maintainers can review the discussion #15703 and decide how to handle it.

We are trying to keep things clean and ask to only open PRs when it's explicitly requested by maintainers.
See https://tiangolo.com/open-source/contributing/ and PR description template:

Please start with a GitHub Discussion.

Once a team member asks you to open a PR, create it and link the discussion here.

Thanks for understanding!

@YuriiMotov YuriiMotov closed this Aug 5, 2026
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.

2 participants