{"record":{"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/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/fastapi/encoders.py#L324-L360","documentation":"PydanticV1NotSupportedError raised inside jsonable_encoder (encoders.py:342). When FastAPI walks the response to serialize it, is_pydantic_v1_model_instance(obj) returns True for a legacy pydantic.v1.BaseModel instance, and FastAPI (which is pydantic-v2-only) refuses to encode it. The message names the offending model via {obj!r}.","triggerScenarios":"An endpoint returns (or a response_model produces) an instance of a pydantic.v1 model — e.g. a model imported from pydantic.v1, a third-party object built on v1, or code that still uses the v1 compatibility shim — and jsonable_encoder tries to serialize it.","commonSituations":"Migrating from FastAPI 0.99 (pydantic v1) to 0.100+/1.x (pydantic v2) without converting models; a dependency/library returning pydantic.v1 objects; mixing `from pydantic.v1 import BaseModel` leftovers; ORJSONResponse/custom encoder receiving a v1 model.","solutions":["Migrate the model to pydantic v2: change `from pydantic import BaseModel` (drop the .v1), and adapt validators/config to v2 (model_config, @field_validator).","If you must keep a v1 model, convert it before returning: return obj.dict() (v1) or pydantic_v1 parse to a v2 model.","Search the codebase for `pydantic.v1` imports and remove/replace them.","Ensure third-party objects returned are v2 models or plain dataclasses/dicts."],"exampleFix":"// before\nfrom pydantic.v1 import BaseModel\n\nclass Item(BaseModel):\n    name: str\n\n@app.get(\"/items/{i}\", response_model=Item)\ndef read(i: int):\n    return Item(name=\"x\")  # -> PydanticV1NotSupportedError\n\n// after\nfrom pydantic import BaseModel, ConfigDict\n\nclass Item(BaseModel):\n    model_config = ConfigDict()\n    name: str\n\n@app.get(\"/items/{i}\", response_model=Item)\ndef read(i: int):\n    return Item(name=\"x\")","handlingStrategy":"type-guard","validationCode":"# Reject pydantic v1 models before they reach jsonable_encoder\ntry:\n    from pydantic.v1 import BaseModel as V1Model\nexcept ImportError:\n    V1Model = None\n\ndef assert_v2(obj):\n    if V1Model is not None and isinstance(obj, V1Model):\n        raise TypeError(f\"pydantic v1 model not supported: {obj!r}\")\n    return obj","typeGuard":"def is_pydantic_v1(obj) -> bool:\n    try:\n        from pydantic.v1 import BaseModel as V1Model\n    except ImportError:\n        return False\n    return isinstance(obj, V1Model)","tryCatchPattern":"from fastapi.encoders import jsonable_encoder\ntry:\n    data = jsonable_encoder(obj)\nexcept Exception as e:\n    if \"pydantic.v1\" in str(e):\n        data = obj.dict()  # v1 fallback, then migrate\n    else:\n        raise","preventionTips":["Migrate all models to pydantic v2 and remove every `pydantic.v1` import.","Return pydantic v2 models or plain dicts/lists from endpoints.","Add a CI grep for `pydantic.v1` to block regressions.","Audit third-party return values to ensure they are v2 models."],"tags":["pydantic","pydantic-v2","encoding","migration","fastapi"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}