{{ message }}
🐛 Fix response model serialization options for SSE event data#15708
Open
kagami-tsukimura wants to merge 3 commits into
Open
🐛 Fix response model serialization options for SSE event data#15708kagami-tsukimura wants to merge 3 commits into
kagami-tsukimura wants to merge 3 commits into
Conversation
avinashkamat48
left a comment
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.
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

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 asresponse_model_by_alias,response_model_include,response_model_exclude,response_model_exclude_none,response_model_exclude_unset, andresponse_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, andresponse_model_exclude_defaults.I ran locally:
AI Disclaimer
I used Codex as a coding assistant while preparing this PR.
After identifying the missing
by_aliashandling and confirming it in the discussion, I used Codex to inspect the sameServerSentEvent(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 transcript
The code-related AI assistance was:
User:
I identified that
ServerSentEvent(data=...)does not respectby_aliaswhenthe 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'sinclude,exclude,exclude_unset,exclude_defaults, andexclude_noneserialization 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:
response_model_by_alias=Falseresponse_model_includeresponse_model_excluderesponse_model_exclude_noneresponse_model_exclude_unsetresponse_model_exclude_defaultsUser:
Run the focused checks.
Codex:
The focused checks passed:
Checklist