Merging this PR will not alter performance✅ 20 untouched benchmarks Comparing kagami-tsukimura:fix-sse-event-data-serialization-options (5a07921) with master (aeeb29f)1 Footnotes |
Sorry, something went wrong.
There was a problem hiding this comment.
This applies the response-model serialization options only when ServerSentEvent.data is a Pydantic model with model_dump_json. The fallback branch still calls jsonable_encoder(item.data) without include, exclude, by_alias, or the exclude flags, so dicts/dataclasses/lists of models sent as event data will continue ignoring the route's response serialization options. Please either pass the same options through jsonable_encoder in the fallback path or add a test that documents the limitation.
Sorry, something went wrong.
|
Good catch, thanks. I updated the fallback jsonable_encoder() path to pass the same response model serialization options as the Pydantic model_dump_json() path, and added regression tests for dict/list SSE event data. Ran the focused SSE tests, related response serialization tests, ruff, and git diff --check locally. |
Sorry, something went wrong.
Pull Request
🐛 Fix response model serialization options for SSE event data
Discussion: #15703
Description
This fixes the ServerSentEvent(data=...) serialization path for Pydantic models.
Previously, this path called model_dump_json() without forwarding the route's response model serialization options. Because of that, models used as SSE event data could ignore options such as response_model_by_alias, response_model_include, response_model_exclude, response_model_exclude_none, response_model_exclude_unset, and response_model_exclude_defaults.
This now passes those options to model_dump_json(), matching the behavior used by regular streamed response items.
I added regression tests for serialization aliases, response_model_by_alias=False, response_model_include, response_model_exclude, response_model_exclude_none, response_model_exclude_unset, and response_model_exclude_defaults.
I ran locally:
AI Disclaimer
I used Codex as a coding assistant while preparing this PR.
After identifying the missing by_alias handling and confirming it in the discussion, I used Codex to inspect the same ServerSentEvent(data=...) serialization path for other response model serialization options.
Codex helped me check for similar missing options, draft the focused regression tests, run local checks, and prepare this PR description. I reviewed the code changes, the test coverage, and the command results before opening the PR.
AI transcriptThe code-related AI assistance was:
User:
I identified that ServerSentEvent(data=...) does not respect by_alias when
the data is a Pydantic model, and confirmed it in the discussion. Before opening
a PR, check whether the same path misses any other response model serialization
options.
Codex:
I will inspect the SSE serialization path and compare it with the regular
streamed response model serialization path.
In fastapi/routing.py, plain streamed items use:
But ServerSentEvent(data=...) with a Pydantic model calls:
So the same path is not only missing by_alias; it is also missing the route's
include, exclude, exclude_unset, exclude_defaults, and exclude_none
serialization options.
User:
Update the fix so it handles the missing response model serialization options,
and add regression tests for the changed behavior.
Codex:
I updated the model_dump_json() call to pass the route serialization options:
I also added focused SSE regression tests for:
User:
Run the focused checks.
Codex:
The focused checks passed:
Checklist