Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions libs/core/langchain_core/runnables/graph_mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,24 @@ def add_subgraph(edges: list[Edge], prefix: str) -> None:
# Add empty subgraphs (subgraphs with no internal edges)
if with_styles:
for prefix, subgraph_node in subgraph_nodes.items():
if ":" not in prefix and prefix not in seen_subgraphs:
mermaid_graph += f"\tsubgraph {prefix}\n"
parts = prefix.split(":")
if all(part in seen_subgraphs for part in parts):
continue

# Add nodes that belong to this subgraph
for key, node in subgraph_node.items():
mermaid_graph += render_node(key, node)
# Open subgraph blocks for each nesting level
opened = []
for part in parts:
if part not in seen_subgraphs:
mermaid_graph += f"\tsubgraph {part}\n"
seen_subgraphs.add(part)
opened.append(part)

# Add nodes that belong to this subgraph
for key, node in subgraph_node.items():
mermaid_graph += render_node(key, node)

for _ in opened:
mermaid_graph += "\tend\n"
seen_subgraphs.add(prefix)

# Add custom styles for nodes
if with_styles:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,29 @@

'''
# ---
# name: test_nested_empty_subgraph_mermaid[mermaid]
'''
---
config:
flowchart:
curve: linear
---
graph TD;
__start__([<p>__start__</p>]):::first
__end__([<p>__end__</p>]):::last
__start__ --> parent\3achild\3agrandchild;
parent\3achild\3agrandchild --> __end__;
subgraph parent
subgraph child
parent\3achild\3agrandchild(grandchild)
end
end
classDef default fill:#f2f0ff,line-height:1.2
classDef first fill-opacity:0
classDef last fill:#bfb6fc

'''
# ---
# name: test_parallel_subgraph_mermaid[mermaid]
'''
---
Expand Down
33 changes: 33 additions & 0 deletions libs/core/tests/unit_tests/runnables/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,39 @@ def test_single_node_subgraph_mermaid(snapshot: SnapshotAssertion) -> None:
assert graph.draw_mermaid() == snapshot(name="mermaid")


def test_nested_empty_subgraph_mermaid(snapshot: SnapshotAssertion) -> None:
"""Test 3+ level nested subgraphs with no internal edges render correctly."""
empty_data = BaseModel
nodes = {
"__start__": Node(
id="__start__", name="__start__", data=empty_data, metadata=None
),
"parent:child:grandchild": Node(
id="parent:child:grandchild",
name="grandchild",
data=empty_data,
metadata=None,
),
"__end__": Node(id="__end__", name="__end__", data=empty_data, metadata=None),
}
edges = [
Edge(
source="__start__",
target="parent:child:grandchild",
data=None,
conditional=False,
),
Edge(
source="parent:child:grandchild",
target="__end__",
data=None,
conditional=False,
),
]
graph = Graph(nodes, edges)
assert graph.draw_mermaid() == snapshot(name="mermaid")


def test_runnable_get_graph_with_invalid_input_type() -> None:
"""Test that error isn't raised when getting graph with invalid input type."""

Expand Down
Loading