python config_error ai_generated true

pydantic.errors.ConfigError: 'extra' 必须是字符串或字典

pydantic.errors.ConfigError: 'extra' must be a string or dict

ID: python/pydantic-config-extra-forbid

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

版本兼容性

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

根因分析

在 Pydantic v2 中,'extra' 配置选项只接受特定字符串值('allow'、'ignore'、'forbid')或字典,不接受布尔值或任意字符串。

English

In Pydantic v2, the 'extra' config option only accepts specific string values ('allow', 'ignore', 'forbid') or a dict, not a boolean or arbitrary string.

generic

解决方案

  1. 90% 成功率
    Set extra='forbid' or 'allow' in model config: class Model(BaseModel): model_config = ConfigDict(extra='forbid')
  2. 85% 成功率
    Use a dict to customize extra fields: model_config = ConfigDict(extra={'field': 'value'})

无效尝试

常见但无效的做法:

  1. 80% 失败

    Boolean values are not valid for 'extra' in v2; it requires string or dict, leading to ConfigError.

  2. 70% 失败

    'extra' does not accept a list; it must be a string or dict, so this raises ConfigError.