{
  "id": "python/pydantic-recursion-error-self-referential",
  "signature": "pydantic_core._pydantic_core.ValidationError: 1 validation error for Node\nchildren.0.children.0\n  RecursionError: maximum recursion depth exceeded while calling a Python object",
  "signature_zh": "pydantic_core._pydantic_core.ValidationError: 1 个验证错误，递归深度超过 Python 最大限制",
  "regex": "pydantic_core\\._pydantic_core\\.ValidationError:\\ 1\\ validation\\ error\\ for\\ Node\\\nchildren\\.0\\.children\\.0\\\n\\ \\ RecursionError:\\ maximum\\ recursion\\ depth\\ exceeded\\ while\\ calling\\ a\\ Python\\ object",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A self-referential Pydantic model (tree/graph) was given a cyclic data structure, causing infinite recursion during validation. Pydantic has no built-in cycle detection for plain nested models.",
  "root_cause_type": "generic",
  "root_cause_zh": "自引用 Pydantic 模型（树/图）接收到循环数据结构，导致验证时无限递归。Pydantic 对普通嵌套模型没有内置的循环检测。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This only defers the crash; a truly cyclic structure will still exhaust memory and eventually segfault the interpreter.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "arbitrary_types_allowed permits non-Pydantic types, it does not detect or prevent cyclic references.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.85,
      "how": "def break_cycles(obj, seen=None):\n    seen = seen or set()\n    if id(obj) in seen:\n        return None\n    seen.add(id(obj))\n    if isinstance(obj, dict):\n        return {k: break_cycles(v, seen) for k, v in obj.items()}\n    if isinstance(obj, list):\n        return [break_cycles(v, seen) for v in obj]\n    return obj\n\nNode.model_validate(break_cycles(raw))",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.7,
      "how": "from pydantic import BaseModel, field_validator\n\nclass Node(BaseModel):\n    name: str\n    children: list['Node'] = []\n\n    @field_validator('children')\n    @classmethod\n    def limit_depth(cls, v):\n        if len(v) > 100:\n            raise ValueError('tree too deep')\n        return v",
      "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.82,
  "fix_success_rate": 0.8,
  "resolvable": "partial",
  "first_seen": "2025-01-19",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}