python install_error ai_generated true

pydantic.errors.PydanticUserError: `from_orm` is deprecated; use `model_validate` with `from_attributes=True`

ID: python/fastapi-pydantic-v2-orm-mode

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-03-30First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active — — —

Root Cause

Pydantic v1 API used with Pydantic v2 installed, typically after upgrading FastAPI.

generic

中文

在安装了Pydantic v2的情况下使用了Pydantic v1 API,通常是升级FastAPI后。

Workarounds

  1. 95% success
    class UserOut(BaseModel):\n    model_config = ConfigDict(from_attributes=True)\n    id: int
  2. 90% success
    UserOut.model_validate(user_obj)

Dead Ends

Common approaches that don't work:

  1. 70% fail

    FastAPI >=0.100 requires pydantic v2; other errors appear.

  2. 85% fail

    orm_mode is a v1 setting; ignored in v2.