From a83cd00c501daabf489d66c6d4d8b945e26db130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 5 Oct 2025 17:46:52 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20util=20to=20check=20if=20anno?= =?UTF-8?q?tation=20is=20Pydantic=20v1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/_compat/shared.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/fastapi/_compat/shared.py b/fastapi/_compat/shared.py index 56e4ce46c..4b55ca25b 100644 --- a/fastapi/_compat/shared.py +++ b/fastapi/_compat/shared.py @@ -183,3 +183,18 @@ def is_uploadfile_sequence_annotation(annotation: Any) -> bool: is_uploadfile_or_nonable_uploadfile_annotation(sub_annotation) for sub_annotation in get_args(annotation) ) + + +def annotation_is_pydantic_v1(annotation: Any) -> bool: + if lenient_issubclass(annotation, v1.BaseModel): + return True + origin = get_origin(annotation) + if origin is Union or origin is UnionType: + for arg in get_args(annotation): + if lenient_issubclass(arg, v1.BaseModel): + return True + if field_annotation_is_sequence(annotation): + for sub_annotation in get_args(annotation): + if annotation_is_pydantic_v1(sub_annotation): + return True + return False