From 2d24b5e11b0a7539a746498f4e1c5982efd51085 Mon Sep 17 00:00:00 2001 From: Alex Colby Date: Fri, 28 Nov 2025 15:51:13 +0000 Subject: [PATCH] fix: formats missing `type_` argument in `FastAPIError` Previously the error messages raised when a complex type wasn't a pydantic `BaseModel` included "...that {type_} is..." instead of including the true argument / return type. This made debugging the use of complex, non-pydantic types difficult, especially in deeply nested dependencies. --- fastapi/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fastapi/utils.py b/fastapi/utils.py index 2e79ee6b1..18108bc3a 100644 --- a/fastapi/utils.py +++ b/fastapi/utils.py @@ -110,7 +110,7 @@ def create_model_field( try: return v1.ModelField(**v1_kwargs) # type: ignore[no-any-return] except RuntimeError: - raise fastapi.exceptions.FastAPIError(_invalid_args_message) from None + raise fastapi.exceptions.FastAPIError(_invalid_args_message.format(type_=type_)) from None elif PYDANTIC_V2: from ._compat import v2 @@ -121,7 +121,7 @@ def create_model_field( try: return v2.ModelField(**kwargs) # type: ignore[return-value,arg-type] except PydanticSchemaGenerationError: - raise fastapi.exceptions.FastAPIError(_invalid_args_message) from None + raise fastapi.exceptions.FastAPIError(_invalid_args_message.format(type_=type_) from None # Pydantic v2 is not installed, but it's not a Pydantic v1 ModelField, it could be # a Pydantic v1 type, like a constrained int from fastapi._compat import v1 @@ -129,7 +129,7 @@ def create_model_field( try: return v1.ModelField(**v1_kwargs) # type: ignore[no-any-return] except RuntimeError: - raise fastapi.exceptions.FastAPIError(_invalid_args_message) from None + raise fastapi.exceptions.FastAPIError(_invalid_args_message.format(type_=type_) from None def create_cloned_field(