From 02493746e250b0b19d64c7f96f8bd096743b3166 Mon Sep 17 00:00:00 2001 From: g7azazlo Date: Wed, 3 Dec 2025 22:39:01 +0300 Subject: [PATCH] Refine response model inference logic in APIRoute - Updated the response model inference to handle cases where the model is None or not a subclass of BaseModel or a dataclass. - Enhanced the logic to infer the response model from the endpoint function's source code when necessary, improving schema generation accuracy. --- fastapi/routing.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/fastapi/routing.py b/fastapi/routing.py index 6485b1ad5..c4d9504c1 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -545,11 +545,16 @@ class APIRoute(routing.Route): response_model = None else: response_model = return_annotation - - if response_model is not None and not lenient_issubclass(response_model, BaseModel): - inferred = infer_response_model_from_ast(endpoint) - if inferred: - response_model = inferred + if ( + response_model is None + or ( + not lenient_issubclass(response_model, BaseModel) + and not dataclasses.is_dataclass(response_model) + ) + ): + inferred = infer_response_model_from_ast(endpoint) + if inferred: + response_model = inferred self.response_model = response_model self.summary = summary