{"id":"f1c2c44a6cfbe32f","repo":"tiangolo/fastapi","slug":"pydantic-v1-models-are-no-longer-supported-by-fast-f1c2c4","errorCode":null,"errorMessage":"pydantic.v1 models are no longer supported by FastAPI. Please update the response model {type_!r}.","messagePattern":"pydantic\\.v1 models are no longer supported by FastAPI\\. Please update the response model (.+?)\\.","errorType":"exception","errorClass":"PydanticV1NotSupportedError","httpStatus":null,"severity":"error","filePath":"fastapi/utils.py","lineNumber":67,"sourceCode":"    \"check that {type_} is a valid Pydantic field type. \"\n    \"If you are using a return type annotation that is not a valid Pydantic \"\n    \"field (e.g. Union[Response, dict, None]) you can disable generating the \"\n    \"response model from the type annotation with the path operation decorator \"\n    \"parameter response_model=None. Read more: \"\n    \"https://fastapi.tiangolo.com/tutorial/response-model/\"\n)\n\n\ndef create_model_field(\n    name: str,\n    type_: Any,\n    default: Any | None = Undefined,\n    field_info: FieldInfo | None = None,\n    alias: str | None = None,\n    mode: Literal[\"validation\", \"serialization\"] = \"validation\",\n) -> ModelField:\n    if annotation_is_pydantic_v1(type_):\n        raise PydanticV1NotSupportedError(\n            \"pydantic.v1 models are no longer supported by FastAPI.\"\n            f\" Please update the response model {type_!r}.\"\n        )\n    field_info = field_info or FieldInfo(annotation=type_, default=default, alias=alias)\n    try:\n        return v2.ModelField(mode=mode, name=name, field_info=field_info)\n    except PydanticSchemaGenerationError:\n        raise fastapi.exceptions.FastAPIError(\n            _invalid_args_message.format(type_=type_)\n        ) from None\n\n\ndef generate_operation_id_for_path(\n    *, name: str, path: str, method: str\n) -> str:  # pragma: nocover\n    warnings.warn(\n        message=\"fastapi.utils.generate_operation_id_for_path() was deprecated, \"\n        \"it is not used internally, and will be removed soon\",","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/fastapi/utils.py#L49-L85","documentation":"`create_model_field` (utils.py:66-70) raises `PydanticV1NotSupportedError` (a `FastAPIError` subclass, exceptions.py:246) when `annotation_is_pydantic_v1(type_)` is true for a field annotation. This version of FastAPI only supports pydantic v2, so any `pydantic.v1.BaseModel` subclass used as a response_model, request body, query dependency, or embedded field type triggers it at app/route construction.","triggerScenarios":"Using `from pydantic.v1 import BaseModel` and annotating a path operation or `response_model=` with it; a third-party library that still ships v1 models and exposes them as types; embedding a v1 model inside a v2 model field; importing `BaseModel` from the wrong (compat) location after an upgrade.","commonSituations":"Upgrading FastAPI/pydantic without migrating models; mixing `pydantic.v1` compat shims left over from a 1.x->2.x migration; a dependency (SDK, ORM plugin) returning v1 models that you annotate as `response_model`.","solutions":["Migrate the model to pydantic v2: `from pydantic import BaseModel` and update validators/`Config` to v2 idioms.","If you cannot migrate, disable response model generation for that endpoint with `response_model=None`.","Replace the v1 model annotation with a plain `dict` or a v2 model, or return a `Response` directly.","Upgrade or replace the third-party package that still exposes `pydantic.v1` models."],"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_item(i: int): ...\n\n# after\nfrom pydantic import BaseModel\n\nclass Item(BaseModel):\n    name: str\n\n@app.get(\"/items/{i}\", response_model=Item)\ndef read_item(i: int): ...","handlingStrategy":"validation","validationCode":"import pydantic\n\ndef assert_pydantic_v2_model(model: type) -> None:\n    \"\"\"Reject pydantic.v1 models used as FastAPI field/response types.\"\"\"\n    if hasattr(pydantic, \"v1\") and issubclass(model, pydantic.v1.BaseModel):\n        raise TypeError(\n            f\"{model!r} is a pydantic.v1 model, no longer supported. Migrate to pydantic v2.\"\n        )\n\n# usage, e.g. in a startup scan of declared response models\nfor m in (Item, Order, User):\n    assert_pydantic_v2_model(m)","typeGuard":"import pydantic\n\ndef is_pydantic_v2_model(obj: object) -> bool:\n    return (\n        isinstance(obj, type)\n        and issubclass(obj, pydantic.BaseModel)\n        and not (hasattr(pydantic, \"v1\") and issubclass(obj, pydantic.v1.BaseModel))\n    )","tryCatchPattern":"# Construction-time failure: catch at bootstrap so the service logs a clear cause.\nfrom fastapi.exceptions import PydanticV1NotSupportedError\n\ntry:\n    app.include_router(legacy_router)  # router exposes a v1 response_model\nexcept PydanticV1NotSupportedError as exc:\n    raise SystemExit(\n        f\"Startup aborted: {exc}. Migrate the model to pydantic v2 or set response_model=None.\"\n    ) from exc","preventionTips":["After upgrading pydantic, grep the codebase for `pydantic.v1` and `from pydantic.v1` and migrate every hit.","Import `BaseModel` only from the top-level `pydantic` package.","For endpoints you cannot migrate immediately, set `response_model=None` or return a plain `dict`/`Response`.","Audit third-party SDKs that expose model types before annotating endpoints with them."],"tags":["pydantic","migration","response-model","version-compat"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}