From a1a7e1f7b65c59f3e1fea97eb927c9a6b2cdd0a9 Mon Sep 17 00:00:00 2001 From: BillionClaw <267901332+BillionClaw@users.noreply.github.com> Date: Wed, 25 Mar 2026 05:45:23 +0800 Subject: [PATCH] fix(routing): thread stream_item_type through include_router() When a typed SSE or JSONL streaming endpoint is registered via a router, the OpenAPI schema loses the stream item type because include_router() re-creates routes via add_api_route() without passing stream_item_type. This fix threads stream_item_type through add_api_route() and include_router(), following the strict_content_type pattern. Fixes #15077 --- fastapi/routing.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/fastapi/routing.py b/fastapi/routing.py index 36acb6b89d..7b0454b768 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -840,11 +840,12 @@ class APIRoute(routing.Route): generate_unique_id_function: Callable[["APIRoute"], str] | DefaultPlaceholder = Default(generate_unique_id), strict_content_type: bool | DefaultPlaceholder = Default(True), + stream_item_type: type[Any] | None = None, ) -> None: self.path = path self.endpoint = endpoint - self.stream_item_type: Any | None = None - if isinstance(response_model, DefaultPlaceholder): + self.stream_item_type: Any | None = stream_item_type + if self.stream_item_type is None and isinstance(response_model, DefaultPlaceholder): return_annotation = get_typed_return_annotation(endpoint) if lenient_issubclass(return_annotation, Response): response_model = None @@ -1364,6 +1365,7 @@ class APIRouter(routing.Router): generate_unique_id_function: Callable[[APIRoute], str] | DefaultPlaceholder = Default(generate_unique_id), strict_content_type: bool | DefaultPlaceholder = Default(True), + stream_item_type: type[Any] | None = None, ) -> None: route_class = route_class_override or self.route_class responses = responses or {} @@ -1793,6 +1795,7 @@ class APIRouter(routing.Router): router.strict_content_type, self.strict_content_type, ), + stream_item_type=route.stream_item_type, ) elif isinstance(route, routing.Route): methods = list(route.methods or [])