From 17d9be4d3ebed8b3a86683ef49b74a718268d909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 21 Dec 2025 16:23:01 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8A=20Add=20warnings=20when=20using=20?= =?UTF-8?q?Pydantic=20v1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/dependencies/utils.py | 8 ++++++++ fastapi/routing.py | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 0ba93524c..39d0bd89c 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -1,6 +1,7 @@ import dataclasses import inspect import sys +import warnings from collections.abc import Coroutine, Mapping, Sequence from contextlib import AsyncExitStack, contextmanager from copy import copy, deepcopy @@ -322,6 +323,13 @@ def get_dependant( ) continue assert param_details.field is not None + if isinstance(param_details.field, may_v1.ModelField): + warnings.warn( + "pydantic.v1 is deprecated and will soon stop being supported by FastAPI." + f" Please update the param {param_name}: {param_details.type_annotation!r}.", + category=DeprecationWarning, + stacklevel=5, + ) if isinstance( param_details.field.field_info, (params.Body, temp_pydantic_v1_params.Body) ): diff --git a/fastapi/routing.py b/fastapi/routing.py index a1f2e44bb..2770e3253 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -2,6 +2,7 @@ import email.message import functools import inspect import json +import warnings from collections.abc import ( AsyncIterator, Awaitable, @@ -28,6 +29,7 @@ from fastapi._compat import ( _get_model_config, _model_dump, _normalize_errors, + annotation_is_pydantic_v1, lenient_issubclass, may_v1, ) @@ -634,6 +636,13 @@ class APIRoute(routing.Route): f"Status code {status_code} must not have a response body" ) response_name = "Response_" + self.unique_id + if annotation_is_pydantic_v1(self.response_model): + warnings.warn( + "pydantic.v1 is deprecated and will soon stop being supported by FastAPI." + f" Please update the response model {self.response_model!r}.", + category=DeprecationWarning, + stacklevel=4, + ) self.response_field = create_model_field( name=response_name, type_=self.response_model, @@ -667,6 +676,13 @@ class APIRoute(routing.Route): f"Status code {additional_status_code} must not have a response body" ) response_name = f"Response_{additional_status_code}_{self.unique_id}" + if annotation_is_pydantic_v1(model): + warnings.warn( + "pydantic.v1 is deprecated and will soon stop being supported by FastAPI." + f" In responses={{}}, please update {model}.", + category=DeprecationWarning, + stacklevel=4, + ) response_field = create_model_field( name=response_name, type_=model, mode="serialization" )