tiangolo/fastapi · error · PydanticV1NotSupportedError
pydantic.v1 models are no longer supported by FastAPI. Pleas
Error message
pydantic.v1 models are no longer supported by FastAPI. Please update the model {obj!r}. What it means
PydanticV1NotSupportedError raised at encoders.py:342 by jsonable_encoder when it encounters a pydantic.v1 model instance. FastAPI dropped support for pydantic.v1 models; the encoder checks is_pydantic_v1_model_instance(obj) as a fallback (after lists, registered types, custom encoders) and raises with the offending object's repr. It fires whenever a response model, response_model, or manual jsonable_encoder call receives a v1 BaseModel.
Source
Thrown at fastapi/encoders.py:342
include=include,
exclude=exclude,
by_alias=by_alias,
exclude_unset=exclude_unset,
exclude_defaults=exclude_defaults,
exclude_none=exclude_none,
custom_encoder=custom_encoder,
sqlalchemy_safe=sqlalchemy_safe,
)
)
return encoded_list
if type(obj) in ENCODERS_BY_TYPE:
return ENCODERS_BY_TYPE[type(obj)](obj)
for encoder, classes_tuple in encoders_by_class_tuples.items():
if isinstance(obj, classes_tuple):
return encoder(obj)
if is_pydantic_v1_model_instance(obj):
raise PydanticV1NotSupportedError(
"pydantic.v1 models are no longer supported by FastAPI."
f" Please update the model {obj!r}."
)
try:
data = dict(obj)
except Exception as e:
errors: list[Exception] = []
errors.append(e)
try:
data = vars(obj)
except Exception as e:
errors.append(e)
raise ValueError(errors) from e
return jsonable_encoder(
data,
include=include,
exclude=exclude,
by_alias=by_alias,View on GitHub (pinned to 42a41db11f)
Solutions
- Re-define the model inheriting from pydantic.BaseModel (v2) instead of pydantic.v1.BaseModel.
- If a third-party returns v1 models, convert: MyV2Model.model_validate(v1_obj.dict()) before returning.
- Run python -c 'import pydantic; print(pydantic.VERSION)' and grep the codebase for 'pydantic.v1' / 'from pydantic.v1' to find leftover v1 usage.
- Pin/upgrade the third-party library to a version that emits v2 models.
Example fix
# before
from pydantic.v1 import BaseModel
class Item(BaseModel):
name: str
# after
from pydantic import BaseModel
class Item(BaseModel):
name: str Defensive patterns
Strategy: type-guard
Validate before calling
from fastapi._compat import is_pydantic_v1_model_instance
def ensure_v2(obj):
if is_pydantic_v1_model_instance(obj):
raise TypeError(f'{obj!r} is a pydantic.v1 model — migrate to pydantic.BaseModel')
return obj Type guard
import typing
from pydantic import BaseModel
def is_pydantic_v2_model(v: typing.Any) -> typing.TypeGuard[BaseModel]:
return isinstance(v, BaseModel) Try / catch
from fastapi.exceptions import PydanticV1NotSupportedError
from fastapi.encoders import jsonable_encoder
try:
jsonable_encoder(obj)
except PydanticV1NotSupportedError:
obj = MyV2Model.model_validate(obj.dict()) # convert v1 -> v2
jsonable_encoder(obj) Prevention
- Migrate all models from pydantic.v1.BaseModel to pydantic.BaseModel (v2).
- Grep the codebase for 'pydantic.v1' / 'from pydantic.v1' and remove.
- When integrating third-party libs that return v1 models, convert with model_validate before returning.
- Add a CI check that asserts no model inherits from pydantic.v1.BaseModel.
When it happens
Trigger: Returning a pydantic.v1.BaseModel subclass instance from a path operation; passing a v1 model to jsonable_encoder(); a nested field containing a v1 model that gets traversed during encoding; using a third-party library that still hands back v1 models.
Common situations: Migrating from FastAPI 0.99- to 0.100+ and forgetting to convert models from pydantic.v1.BaseModel to pydantic.BaseModel; a dependency (e.g. older ORM/SDK) returning v1 models; copy-pasted v1 model definitions still inheriting from pydantic.v1.
Related errors
- e.errors(include_url=False)
- {"errors": exc.errors(), "body": body.decode()}
- {"errors": exc.errors(), "body": body.decode()}
- Invalid ID format, it must start with "isbn-" or "imdb-"
- Expected UploadFile, received: {type(__input_value)}
AI-assisted analysis of tiangolo/fastapi@42a41db11f (2026-08-04).
Data as JSON: /data/errors/bc673065736fdc29.json.
Report an issue: GitHub.