Skip to content

The agent engineering platform.

Remove logic to exclude intermediate nodes from rendering time (#20459)

Ángel Igareta60c7a17781c8
diff --git a/libs/core/langchain_core/runnables/graph.py b/libs/core/langchain_core/runnables/graph.py
index 8c11cd8fc7..969780446e 100644
--- a/libs/core/langchain_core/runnables/graph.py
+++ b/libs/core/langchain_core/runnables/graph.py
@@ -163,7 +163,6 @@ class Graph:
nodes: Dict[str, Node] = field(default_factory=dict)
edges: List[Edge] = field(default_factory=list)
- branches: Optional[Dict[str, List[Branch]]] = field(default_factory=dict)
def to_json(self, *, with_schemas: bool = False) -> Dict[str, List[Dict[str, Any]]]:
"""Convert the graph to a JSON-serializable format."""
@@ -354,7 +353,6 @@ class Graph:
return draw_mermaid(
nodes=nodes,
edges=self.edges,
- branches=self.branches,
first_node_label=first_label,
last_node_label=last_label,
curve_style=curve_style,
diff --git a/libs/core/langchain_core/runnables/graph_mermaid.py b/libs/core/langchain_core/runnables/graph_mermaid.py
index 591d3e68ff..6912f2bcc2 100644
--- a/libs/core/langchain_core/runnables/graph_mermaid.py
+++ b/libs/core/langchain_core/runnables/graph_mermaid.py
@@ -4,7 +4,6 @@ from dataclasses import asdict
from typing import Dict, List, Optional, Tuple
from langchain_core.runnables.graph import (
- Branch,
CurveStyle,
Edge,
MermaidDrawMethod,
@@ -15,7 +14,6 @@ from langchain_core.runnables.graph import (
def draw_mermaid(
nodes: Dict[str, str],
edges: List[Edge],
- branches: Optional[Dict[str, List[Branch]]] = None,
first_node_label: Optional[str] = None,
last_node_label: Optional[str] = None,
curve_style: CurveStyle = CurveStyle.LINEAR,
@@ -28,8 +26,6 @@ def draw_mermaid(
nodes (dict[str, str]): List of node ids
edges (List[Edge]): List of edges, object with source,
target and data.
- branches (defaultdict[str, list[Branch]]): Branches for the graph (
- in case of langgraph) to remove intermediate condition nodes.
curve_style (CurveStyle, optional): Curve style for the edges.
node_colors (NodeColors, optional): Node colors for different types.
wrap_label_n_words (int, optional): Words to wrap the edge labels.
@@ -51,30 +47,8 @@ def draw_mermaid(
if last_node_label is not None:
format_dict[last_node_label] = "{0}[{0}]:::endclass"
- # Filter out nodes that were created due to conditional edges
- # Remove combinations where node name is the same as a branch + condition
- mapping_intermediate_node_pure_node = {}
- if branches is not None:
- for agent, agent_branches in branches.items():
- for branch in agent_branches:
- condition_name = branch.condition.__name__
- intermediate_node_label = f"{agent}_{condition_name}"
- if intermediate_node_label in nodes:
- mapping_intermediate_node_pure_node[intermediate_node_label] = agent
-
- # Not intermediate nodes
- pure_nodes = {
- id: value
- for id, value in nodes.items()
- if value not in mapping_intermediate_node_pure_node.keys()
- }
-
- # Add __end__ node if it is in any of the edges.target
- if any("__end__" in edge.target for edge in edges):
- pure_nodes["__end__"] = "__end__"
-
# Add nodes to the graph
- for node in pure_nodes.values():
+ for node in nodes.values():
node_label = format_dict.get(node, format_dict[default_class_label]).format(
_escape_node_label(node)
)
@@ -82,9 +56,7 @@ def draw_mermaid(
# Add edges to the graph
for edge in edges:
- adjusted_edge = _adjust_mermaid_edge(
- edge, nodes, mapping_intermediate_node_pure_node
- )
+ adjusted_edge = _adjust_mermaid_edge(edge=edge, nodes=nodes)
if (
adjusted_edge is None
): # Ignore if it is connection between source and intermediate node
@@ -125,20 +97,11 @@ def _escape_node_label(node_label: str) -> str:
def _adjust_mermaid_edge(
edge: Edge,
nodes: Dict[str, str],
- mapping_intermediate_node_pure_node: Dict[str, str],
) -> Optional[Tuple[str, str]]:
"""Adjusts Mermaid edge to map conditional nodes to pure nodes."""
source_node_label = nodes.get(edge.source, edge.source)
target_node_label = nodes.get(edge.target, edge.target)
- # Remove nodes between source node to intermediate node
- if target_node_label in mapping_intermediate_node_pure_node.keys():
- return None
-
- # Replace intermediate nodes by source nodes
- if source_node_label in mapping_intermediate_node_pure_node.keys():
- source_node_label = mapping_intermediate_node_pure_node[source_node_label]
-
return source_node_label, target_node_label