Repository navigation
🐛 Fix explicit response_model ignored on streaming generator endpoints - #16384
Open
MayankSharma-2812 wants to merge 1 commit into
Open
MayankSharma-2812 wants to merge 1 commit into
MayankSharma-2812 wants to merge 1 commit into
Conversation
This comment was marked as spam.
This comment was marked as spam.
MayankSharma-2812
force-pushed
the
fix-streaming-explicit-response-model
branch
from
October 5, 2026 03:48
fa5380b to
311e187
Compare
MayankSharma-2812
force-pushed
the
fix-streaming-explicit-response-model
branch
from
October 5, 2026 03:57
311e187 to
e748591
Compare
Author
|
@hossiendehghan989 Great suggestion! Added regressions for The tests confirm that:
Both JSONL and SSE now have explicit coverage for this behavior. Pushed in the latest commit! |
This branch has not been deployed
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
Discussion: #16382
Description
When specifying an explicit
response_model(such asresponse_model=Item,response_model=Iterator[Item], orresponse_model=AsyncIterable[Item]) on generator streaming endpoints (JSONL and SSE), it was ignored during response serialization and generated an empty OpenAPIitemSchema: {}.Root Cause
In
_populate_api_route_state(),get_stream_item_type()was only called whenresponse_modelwas an instance ofDefaultPlaceholder. When an explicitresponse_modelwas provided:route.stream_item_typewas never populated, leavingstream_item_fieldasNone.route.response_fieldwas erroneously populated instead.stream_item_field. Because it wasNone, serialization fell back tojsonable_encoder(), bypassing model validation and field filters (response_model_include,response_model_exclude,response_model_exclude_none).itemSchema: {}becausestream_item_fieldwas absent.Fix
In
_populate_api_route_state():is_sse_streamoris_json_stream) with an explicitresponse_model.get_stream_item_type(response_model) or response_model.ServerSentEvent, assignroute.stream_item_type = stream_item.response_model = Nonesostream_item_fieldis created for per-item serialization, validation, and OpenAPI generation.Tests Added
Added
tests/test_stream_response_model.pycovering:response_model_exclude,response_model_exclude_none)response_model=AsyncIterable[Item]response_model=Iterator[Item]ServerSentEventdirectlyresponse_model=ServerSentEventapp.include_router()itemSchemafor JSONL andcontentSchemafor SSE)Checklist