python config_error ai_generated true

pydantic.errors.PydanticUserError: `Config` and `fields` are deprecated in Pydantic V2. Use `model_config` and `Field` instead. See https://docs.pydantic.dev/2.0/migration/

ID: python/pydantic-config-and-fields-deprecated

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active

Root Cause

A v1-style inner class Config or fields dict was used in a model under Pydantic v2.

generic

中文

在 Pydantic v2 的模型中使用了 v1 风格的内嵌 Config 类或 fields 字典。

Workarounds

  1. 90% success
    from pydantic import BaseModel, ConfigDict
    class M(BaseModel):
        model_config = ConfigDict(orm_mode=True)  # was class Config: orm_mode = True
        x: int
  2. 85% success
    pip install bump-pydantic
    bump-pydantic ./src

Dead Ends

Common approaches that don't work:

  1. 80% fail

    No such switch exists; pydantic 2.x always rejects v1 config.

  2. 60% fail

    Blocks upgrades of FastAPI and other deps that require v2.

  3. 70% fail

    The error is raised at class definition time, not import time, and cannot be caught usefully.