Skip to content

🐛 Fix exclude_unset/exclude_defaults/exclude_none not propagated to ServerSentEvent data - #16145

Closed
SAURABHSALVE wants to merge 2 commits into
fastapi:masterfrom
SAURABHSALVE:fix/sse-event-exclude-params
Closed

SAURABHSALVE wants to merge 2 commits into
fastapi:masterfrom
SAURABHSALVE:fix/sse-event-exclude-params

Conversation

@SAURABHSALVE

Copy link
Copy Markdown
Contributor

Problem

ServerSentEvent(data=...) serializes Pydantic models via model_dump_json()
without passing exclude_unset, exclude_defaults, or exclude_none. Every
other serialization path in FastAPI threads these parameters through:

  • ModelField.serialize_json() — used for plain (non-wrapped) stream items
  • serialize_response() — used for regular (non-streaming) responses

This makes ServerSentEvent data the only path that ignores these route-level
settings, so the same model serializes differently depending on how it is
yielded:

class Item(BaseModel):
    name: str
    tag: str = "default-tag"

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

@app.get("/b", response_class=EventSourceResponse, response_model_exclude_defaults=True)
async def b():
    yield ServerSentEvent(data=Item(name="x"))  # data: {"name":"x","tag":"default-tag"}

@codspeed

codspeed Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing SAURABHSALVE:fix/sse-event-exclude-params (2a8b40f) 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