From 4b0fca4cbdc6ba6555259dd6e4d2807da6568588 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Thu, 25 Dec 2025 12:05:51 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Error=20out=20when=20using?= =?UTF-8?q?=20pydantic.v1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/dependencies/utils.py | 11 ++++------- fastapi/exceptions.py | 6 ++++++ fastapi/routing.py | 18 +++++++----------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 39d0bd89c..2cebae618 100644 --- a/fastapi/dependencies/utils.py +++ b/fastapi/dependencies/utils.py @@ -1,7 +1,6 @@ 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 @@ -51,7 +50,7 @@ from fastapi.concurrency import ( contextmanager_in_threadpool, ) from fastapi.dependencies.models import Dependant -from fastapi.exceptions import DependencyScopeError +from fastapi.exceptions import DependencyScopeError, PydanticV1NotSupportedError from fastapi.logger import logger from fastapi.security.oauth2 import SecurityScopes from fastapi.types import DependencyCacheKey @@ -324,11 +323,9 @@ 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, + raise PydanticV1NotSupportedError( + "pydantic.v1 models are no longer supported by FastAPI." + f" Please update the param {param_name}: {param_details.type_annotation!r}." ) if isinstance( param_details.field.field_info, (params.Body, temp_pydantic_v1_params.Body) diff --git a/fastapi/exceptions.py b/fastapi/exceptions.py index 8e0c55902..a133cd793 100644 --- a/fastapi/exceptions.py +++ b/fastapi/exceptions.py @@ -231,3 +231,9 @@ class ResponseValidationError(ValidationException): ) -> None: super().__init__(errors, endpoint_ctx=endpoint_ctx) self.body = body + + +class PydanticV1NotSupportedError(FastAPIError): + """ + A pydantic.v1 model is used, which is no longer supported. + """ diff --git a/fastapi/routing.py b/fastapi/routing.py index 2770e3253..fc3fb33d4 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -2,7 +2,6 @@ import email.message import functools import inspect import json -import warnings from collections.abc import ( AsyncIterator, Awaitable, @@ -48,6 +47,7 @@ from fastapi.encoders import jsonable_encoder from fastapi.exceptions import ( EndpointContext, FastAPIError, + PydanticV1NotSupportedError, RequestValidationError, ResponseValidationError, WebSocketRequestValidationError, @@ -637,11 +637,9 @@ class APIRoute(routing.Route): ) 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, + raise PydanticV1NotSupportedError( + "pydantic.v1 models are no longer supported by FastAPI." + f" Please update the response model {self.response_model!r}." ) self.response_field = create_model_field( name=response_name, @@ -677,11 +675,9 @@ class APIRoute(routing.Route): ) 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, + raise PydanticV1NotSupportedError( + "pydantic.v1 models are no longer supported by FastAPI." + f" In responses={{}}, please update {model}." ) response_field = create_model_field( name=response_name, type_=model, mode="serialization"