Skip to content

🐛 Fix field aliases ignored for ServerSentEvent data in SSE streams - #16144

Closed
SAURABHSALVE wants to merge 1 commit into
fastapi:masterfrom
SAURABHSALVE:fix/sse-event-data-by-alias
Closed

SAURABHSALVE wants to merge 1 commit into
fastapi:masterfrom
SAURABHSALVE:fix/sse-event-data-by-alias

Conversation

@SAURABHSALVE

Copy link
Copy Markdown
Contributor

Problem

ServerSentEvent(data=...) serializes Pydantic models via model_dump_json(),
whose by_alias default is False. Every other serialization path in FastAPI
defaults to by_alias=True:

  • jsonable_encoder() — by_alias: bool = True
  • ModelField.serialize_json() — by_alias: bool = True
  • response_model_by_alias on all route decorators — True

This makes ServerSentEvent the only path that drops aliases, so the same model
serializes differently depending on how it is yielded:

class Item(BaseModel):
    item_name: str = Field(serialization_alias="itemName")

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

@app.get("/b", response_class=EventSourceResponse)
async def b():
    yield ServerSentEvent(data=Item(item_name="x"))  # data: {"item_name":"x"}

`ServerSentEvent(data=...)` serialized Pydantic models with
`model_dump_json()`, whose `by_alias` default is `False`. Every other
serialization path in FastAPI defaults to `by_alias=True`, so the same
model produced different JSON depending on whether it was yielded
directly or wrapped in a `ServerSentEvent`.

Thread `response_model_by_alias` through both branches of the
`ServerSentEvent` data serializer so it matches the plain-item path and
honors an explicit `response_model_by_alias=False`.
@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-data-by-alias (29c847d) 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