python
data_error
ai_generated
partial
pydantic_core._pydantic_core.ValidationError: 1 validation error for Node children.0.parent Recursion error - cyclic reference detected [type=recursion_loop]
ID: python/pydantic-recursion-loop-detected
80%Fix Rate
83%Confidence
0Evidence
2024-08-09First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 2.x | active | — | — | — |
Root Cause
The input data contains a cyclic reference (e.g. an object referencing itself), which Pydantic detects while validating a recursive model.
generic中文
输入数据包含循环引用(例如对象引用自身),Pydantic 在验证递归模型时检测到该循环。
Workarounds
-
90% success
def strip_parent(node): node.pop('parent', None) for c in node.get('children', []): strip_parent(c) return node Node.model_validate(strip_parent(data)) -
80% success
from pydantic import BaseModel class Node(BaseModel): name: str children: list['Node'] = [] Node.model_rebuild() # avoid modeling parent back-references
Dead Ends
Common approaches that don't work:
-
85% fail
The cycle is in the data itself, not stack depth. It will still be detected and may crash the interpreter.
-
70% fail
Loses the schema definition and breaks legitimate nested validation for non-cyclic inputs.