{"record":{"id":"8650ace603b08719","repo":"langchain-ai/langchain","slug":"found-duplicate-subgraph-subgraph-this-like","errorCode":null,"errorMessage":"Found duplicate subgraph '{subgraph}' -- this likely means that you're reusing a subgraph node with the same name. Please adjust your graph to have subgraph nodes with unique names.","messagePattern":"Found duplicate subgraph '(.+?)' -- this likely means that you're reusing a subgraph node with the same name\\. Please adjust your graph to have subgraph nodes with unique names\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/runnables/graph_mermaid.py","lineNumber":180,"sourceCode":"        common_prefix = \":\".join(\n            src for src, tgt in zip(src_parts, tgt_parts, strict=False) if src == tgt\n        )\n        edge_groups.setdefault(common_prefix, []).append(edge)\n\n    seen_subgraphs = set()\n\n    def add_subgraph(edges: list[Edge], prefix: str) -> None:\n        nonlocal mermaid_graph\n        self_loop = len(edges) == 1 and edges[0].source == edges[0].target\n        if prefix and not self_loop:\n            subgraph = prefix.rsplit(\":\", maxsplit=1)[-1]\n            if subgraph in seen_subgraphs:\n                msg = (\n                    f\"Found duplicate subgraph '{subgraph}' -- this likely means that \"\n                    \"you're reusing a subgraph node with the same name. \"\n                    \"Please adjust your graph to have subgraph nodes with unique names.\"\n                )\n                raise ValueError(msg)\n\n            seen_subgraphs.add(subgraph)\n            mermaid_graph += f\"\\tsubgraph {subgraph}\\n\"\n\n            # Add nodes that belong to this subgraph\n            if with_styles and prefix in subgraph_nodes:\n                for key, node in subgraph_nodes[prefix].items():\n                    mermaid_graph += render_node(key, node)\n\n        for edge in edges:\n            source, target = edge.source, edge.target\n\n            # Add BR every wrap_label_n_words words\n            if edge.data is not None:\n                edge_data = edge.data\n                words = str(edge_data).split()  # Split the string into words\n                # Group words into chunks of wrap_label_n_words size\n                if len(words) > wrap_label_n_words:","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/runnables/graph_mermaid.py#L162-L198","documentation":"While generating Mermaid output, draw_mermaid tracks subgraph names it has already emitted. When two subgraph nodes share the same final name (prefix segment after the last ':'), Mermaid syntax would be ambiguous, so it raises this ValueError. It almost always means the same compiled subgraph was added as two nodes with the same name.","triggerScenarios":"A graph that reuses the same subgraph instance (or same-named subgraph nodes) in two places, then calling graph.draw_mermaid() on it. The subgraph name is the node id after the last ':' in the prefixed id.","commonSituations":"Building a state graph where the same compiled subgraph is wired into two nodes without renaming; copy-pasting node definitions; LangGraph-style multi-agent graphs reusing a tool subgraph.","solutions":["Give each subgraph node a unique name when adding it to the parent graph","If the same subgraph must appear twice, create two separately-named instances/wrappers","Check graph.nodes for ids sharing the same suffix after ':' before drawing"],"exampleFix":"# before\nparent.add_node(\"agent\", agent_subgraph)\nparent.add_node(\"agent\", agent_subgraph)  # duplicate\n# after\nparent.add_node(\"agent_1\", agent_subgraph)\nparent.add_node(\"agent_2\", agent_subgraph)","handlingStrategy":"validation","validationCode":"names = [nid.split(\":\")[-1] for nid in graph.nodes]\ndupes = {n for n in names if names.count(n) > 1}\nassert not dupes, f\"duplicate subgraph names: {dupes}\"","typeGuard":"def has_unique_subgraph_names(graph) -> bool:\n    names = [n.split(\":\")[-1] for n in graph.nodes]\n    return len(names) == len(set(names))","tryCatchPattern":"try:\n    graph.draw_mermaid()\nexcept ValueError as e:\n    if \"duplicate subgraph\" in str(e):\n        rename_offending_nodes(graph)","preventionTips":["Name every subgraph node uniquely at construction time","Never add the same compiled subgraph under one name twice; wrap with distinct names"],"tags":["graph","mermaid","subgraph","naming","valueerror"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}