python
data_error
ai_generated
true
pydantic_core._pydantic_core.ValidationError: 配置模型验证错误:不允许额外的输入字段 user_id
pydantic_core._pydantic_core.ValidationError: 1 validation error for Config user_id Extra inputs are not permitted [type=extra_forbidden, input_value='abc123', input_type=str]
ID: python/pydantic-validation-error-extra-forbidden
80%修复率
87%置信度
0证据数
2024-06-20首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 2.x | active | — | — | — |
根因分析
模型的 model_config 设置了 extra='forbid',而输入数据包含未在模型中声明的字段。Pydantic v2 默认是 'ignore',但很多项目显式设置为 'forbid'。
English
The model_config has extra='forbid' set, and the input data contains a field not declared in the model. Pydantic v2 defaults to 'ignore' but many projects set 'forbid' explicitly.
解决方案
-
95% 成功率
from pydantic import BaseModel class Config(BaseModel): user_id: str model_config = {'extra': 'forbid'} -
90% 成功率
class Config(BaseModel): model_config = {'extra': 'ignore'}
无效尝试
常见但无效的做法:
-
50% 失败
Works for one schema but breaks when the same dict is validated against multiple models with different allowed fields.
-
65% 失败
Weakens validation across the entire app and may hide typos in field names.