{
  "id": "python/pydantic-recursion-error-self-referencing",
  "signature": "pydantic.errors.PydanticUndefinedAnnotation: `Node` is not defined",
  "signature_zh": "pydantic.errors.PydanticUndefinedAnnotation: `Node` 未定义",
  "regex": "pydantic\\.errors\\.PydanticUndefinedAnnotation:\\ `Node`\\ is\\ not\\ defined",
  "domain": "python",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "A self-referencing model uses a forward reference that Pydantic cannot resolve because the class name is not yet in scope during schema building.",
  "root_cause_type": "generic",
  "root_cause_zh": "自引用模型使用了前向引用，但 Pydantic 在构建 schema 时类名尚未进入作用域，无法解析。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Loses recursive validation; nested children are not validated and errors propagate silently.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Postpones evaluation but Pydantic still needs model_rebuild() to resolve the reference at runtime.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Causes duplicate class definitions and Pydantic still cannot resolve the forward ref cleanly.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "from typing import Optional\nclass Node(BaseModel):\n    value: int\n    child: Optional[\"Node\"] = None\nNode.model_rebuild()",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "class Node(BaseModel):\n    model_config = ConfigDict(defer_build=True)\n    child: Optional[\"Node\"] = None",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-07-11",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}