{"id":"bc673065736fdc29","repo":"tiangolo/fastapi","slug":"pydantic-v1-models-are-no-longer-supported-by-fast","errorCode":null,"errorMessage":"pydantic.v1 models are no longer supported by FastAPI. Please update the model {obj!r}.","messagePattern":"pydantic\\.v1 models are no longer supported by FastAPI\\. Please update the model (.+?)\\.","errorType":"exception","errorClass":"PydanticV1NotSupportedError","httpStatus":null,"severity":"error","filePath":"fastapi/encoders.py","lineNumber":342,"sourceCode":"                    include=include,\n                    exclude=exclude,\n                    by_alias=by_alias,\n                    exclude_unset=exclude_unset,\n                    exclude_defaults=exclude_defaults,\n                    exclude_none=exclude_none,\n                    custom_encoder=custom_encoder,\n                    sqlalchemy_safe=sqlalchemy_safe,\n                )\n            )\n        return encoded_list\n\n    if type(obj) in ENCODERS_BY_TYPE:\n        return ENCODERS_BY_TYPE[type(obj)](obj)\n    for encoder, classes_tuple in encoders_by_class_tuples.items():\n        if isinstance(obj, classes_tuple):\n            return encoder(obj)\n    if is_pydantic_v1_model_instance(obj):\n        raise PydanticV1NotSupportedError(\n            \"pydantic.v1 models are no longer supported by FastAPI.\"\n            f\" Please update the model {obj!r}.\"\n        )\n    try:\n        data = dict(obj)\n    except Exception as e:\n        errors: list[Exception] = []\n        errors.append(e)\n        try:\n            data = vars(obj)\n        except Exception as e:\n            errors.append(e)\n            raise ValueError(errors) from e\n    return jsonable_encoder(\n        data,\n        include=include,\n        exclude=exclude,\n        by_alias=by_alias,","sourceCodeStart":324,"sourceCodeEnd":360,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/fastapi/encoders.py#L324-L360","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nfrom pydantic.v1 import BaseModel\nclass Item(BaseModel):\n    name: str\n# after\nfrom pydantic import BaseModel\nclass Item(BaseModel):\n    name: str","handlingStrategy":"type-guard","validationCode":"from fastapi._compat import is_pydantic_v1_model_instance\ndef ensure_v2(obj):\n    if is_pydantic_v1_model_instance(obj):\n        raise TypeError(f'{obj!r} is a pydantic.v1 model — migrate to pydantic.BaseModel')\n    return obj","typeGuard":"import typing\nfrom pydantic import BaseModel\ndef is_pydantic_v2_model(v: typing.Any) -> typing.TypeGuard[BaseModel]:\n    return isinstance(v, BaseModel)","tryCatchPattern":"from fastapi.exceptions import PydanticV1NotSupportedError\nfrom fastapi.encoders import jsonable_encoder\ntry:\n    jsonable_encoder(obj)\nexcept PydanticV1NotSupportedError:\n    obj = MyV2Model.model_validate(obj.dict())  # convert v1 -> v2\n    jsonable_encoder(obj)","preventionTips":["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."],"tags":["pydantic","pydantic-v2","migration","encoders","fastapi"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}