{"record":{"id":"47c286d0937ab90d","repo":"stanford-oval/storm","slug":"insert-node-error-node-child-node-name-already","errorCode":null,"errorMessage":"Insert node error. Node {child_node_name} already exists under its parent node {self.name}.","messagePattern":"Insert node error\\. Node (.+?) already exists under its parent node (.+?)\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"knowledge_storm/dataclass.py","lineNumber":150,"sourceCode":"\n    def has_child(self, child_node_name: str):\n        \"\"\"\n        Check if the node has the child of given name.\n        \"\"\"\n        return child_node_name in [child.name for child in self.children]\n\n    def add_child(self, child_node_name: str, duplicate_handling: str = \"skip\"):\n        \"\"\"\n        Adds a child node to the current node.\n        duplicate_handling (str): How to handle duplicate nodes. Options are \"skip\", \"none\", and \"raise error\".\n        \"\"\"\n        if self.has_child(child_node_name):\n            if duplicate_handling == \"skip\":\n                for child in self.children:\n                    if child.name == child_node_name:\n                        return child\n            elif duplicate_handling == \"raise error\":\n                raise Exception(\n                    f\"Insert node error. Node {child_node_name} already exists under its parent node {self.name}.\"\n                )\n        child_node = KnowledgeNode(name=child_node_name, parent=self)\n        self.children.append(child_node)\n        return child_node\n\n    def get_parent(self):\n        \"\"\"\n        Returns the parent node of the current node.\n\n        Returns:\n            KnowledgeNode: The parent node of the current node.\n        \"\"\"\n        return self.parent\n\n    def get_children(self):\n        \"\"\"\n        Returns the children of the current node.","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/stanford-oval/storm/blob/fb951af7744dab086e34962e9bc6fe878e145f83/knowledge_storm/dataclass.py#L132-L168","documentation":"KnowledgeNode.add_child refuses to insert a child whose name already exists under the node when duplicate_handling is 'raise error' (the default strict mode); 'skip' instead returns the existing child.","triggerScenarios":"Calling add_child (directly or via insert_node/find_node_by_path) with a name that has_child() already reports, while duplicate_handling='raise error'.","commonSituations":"Inserting information whose computed node name collides with an existing node (case differences, trailing whitespace), or re-running insertion over an already-populated knowledge base without dedup.","solutions":["Pass duplicate_handling='skip' to reuse the existing node","Normalize names (strip/casefold) before inserting","Check node.has_child(name) first and reuse the existing child"],"exampleFix":"# before\nnode.add_child('History')  # raises if 'History' exists\n\n# after\nchild = node.add_child('History', duplicate_handling='skip')","handlingStrategy":"validation","validationCode":"if node.has_child(name):\n    child = next(c for c in node.children if c.name == name)\nelse:\n    child = node.add_child(name)","typeGuard":null,"tryCatchPattern":"try:\n    child = node.add_child(name)\nexcept Exception as e:\n    if 'already exists' not in str(e):\n        raise\n    child = node.add_child(name, duplicate_handling='skip')","preventionTips":["Use duplicate_handling='skip' for idempotent inserts","Normalize names before insertion","Check has_child before add_child"],"tags":["knowledge-tree","duplicate-node","insertion"],"backgroundTag":"duplicate-key-insertion","analyzedSha":"fb951af7744dab086e34962e9bc6fe878e145f83","analyzedAt":"2026-08-28T11:56:54.780Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}