python config_error ai_generated true

pydantic.errors.PydanticUserError:Pydantic V2 已弃用 `Config` 和 `fields`。请改用 `model_config` 和 `Field`。参见 https://docs.pydantic.dev/2.0/migration/

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

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-01-30首次发现

版本兼容性

版本状态引入弃用备注
2.x active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

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

  2. 60% 失败

    Blocks upgrades of FastAPI and other deps that require v2.

  3. 70% 失败

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