# pydantic.errors.PydanticUndefinedAnnotation: `Node` 未定义

- **ID:** `python/pydantic-recursion-error-self-referencing`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

自引用模型使用了前向引用，但 Pydantic 在构建 schema 时类名尚未进入作用域，无法解析。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   from typing import Optional
class Node(BaseModel):
    value: int
    child: Optional["Node"] = None
Node.model_rebuild()
   ```
2. **** (90% 成功率)
   ```
   class Node(BaseModel):
    model_config = ConfigDict(defer_build=True)
    child: Optional["Node"] = None
   ```

## 无效尝试

- **** — Loses recursive validation; nested children are not validated and errors propagate silently. (70% 失败率)
- **** — Postpones evaluation but Pydantic still needs model_rebuild() to resolve the reference at runtime. (60% 失败率)
- **** — Causes duplicate class definitions and Pydantic still cannot resolve the forward ref cleanly. (90% 失败率)
