# pydantic_core._pydantic_core.ValidationError: 不允许额外的输入字段

- **ID:** `python/pydantic-v2-extra-input-forbidden`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在 Pydantic v2 中，默认 model_config 不再允许额外字段。输入字典中任何未声明的键都会触发 extra_forbidden 错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 2.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Declare the field explicitly on the model:

class User(BaseModel):
    name: str
    role: str = 'user'
   ```
2. **** (90% 成功率)
   ```
   If you intentionally want to accept unknown keys:

class User(BaseModel):
    model_config = ConfigDict(extra='ignore')
    name: str
   ```
3. **** (85% 成功率)
   ```
   For dynamic keys, use a dict field:

class User(BaseModel):
    name: str
    metadata: dict[str, Any] = {}
   ```

## 无效尝试

- **** — The extra data is silently discarded, so downstream code that relies on it raises KeyError or AttributeError later. (70% 失败率)
- **** — Fields become untyped Any attributes; validation is skipped and IDE/type-checker support is lost, causing subtle runtime bugs. (55% 失败率)
- **** — Loses v2 performance, Rust core, and breaks other dependencies that require v2. (80% 失败率)
