Home / Function/ draw_mermaid() — langchain Function Reference

draw_mermaid() — langchain Function Reference

Architecture documentation for the draw_mermaid() function in graph_mermaid.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  6f5bf1ec_e14e_f271_08a5_0bc96c21c52a["draw_mermaid()"]
  c545ef44_a033_15ff_e016_18af37277247["graph_mermaid.py"]
  6f5bf1ec_e14e_f271_08a5_0bc96c21c52a -->|defined in| c545ef44_a033_15ff_e016_18af37277247
  776b523a_707b_6be9_ae9a_137ca8570849["_to_safe_id()"]
  6f5bf1ec_e14e_f271_08a5_0bc96c21c52a -->|calls| 776b523a_707b_6be9_ae9a_137ca8570849
  a89253e8_f0f1_f69d_15be_a8a5c825767f["_generate_mermaid_graph_styles()"]
  6f5bf1ec_e14e_f271_08a5_0bc96c21c52a -->|calls| a89253e8_f0f1_f69d_15be_a8a5c825767f
  style 6f5bf1ec_e14e_f271_08a5_0bc96c21c52a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/runnables/graph_mermaid.py lines 45–252

def draw_mermaid(
    nodes: dict[str, Node],
    edges: list[Edge],
    *,
    first_node: str | None = None,
    last_node: str | None = None,
    with_styles: bool = True,
    curve_style: CurveStyle = CurveStyle.LINEAR,
    node_styles: NodeStyles | None = None,
    wrap_label_n_words: int = 9,
    frontmatter_config: dict[str, Any] | None = None,
) -> str:
    """Draws a Mermaid graph using the provided graph data.

    Args:
        nodes: List of node ids.
        edges: List of edges, object with a source, target and data.
        first_node: Id of the first node.
        last_node: Id of the last node.
        with_styles: Whether to include styles in the graph.
        curve_style: Curve style for the edges.
        node_styles: Node colors for different types.
        wrap_label_n_words: Words to wrap the edge labels.
        frontmatter_config: Mermaid frontmatter config.
            Can be used to customize theme and styles. Will be converted to YAML and
            added to the beginning of the mermaid graph.

            See more here: https://mermaid.js.org/config/configuration.html.

            Example config:

            ```python
            {
                "config": {
                    "theme": "neutral",
                    "look": "handDrawn",
                    "themeVariables": {"primaryColor": "#e2e2e2"},
                }
            }
            ```

    Returns:
        Mermaid graph syntax.

    """
    # Initialize Mermaid graph configuration
    original_frontmatter_config = frontmatter_config or {}
    original_flowchart_config = original_frontmatter_config.get("config", {}).get(
        "flowchart", {}
    )
    frontmatter_config = {
        **original_frontmatter_config,
        "config": {
            **original_frontmatter_config.get("config", {}),
            "flowchart": {**original_flowchart_config, "curve": curve_style.value},
        },
    }

    mermaid_graph = (
        (
            "---\n"
            + yaml.dump(frontmatter_config, default_flow_style=False)
            + "---\ngraph TD;\n"
        )
        if with_styles
        else "graph TD;\n"
    )
    # Group nodes by subgraph
    subgraph_nodes: dict[str, dict[str, Node]] = {}
    regular_nodes: dict[str, Node] = {}

    for key, node in nodes.items():
        if ":" in key:
            # For nodes with colons, add them only to their deepest subgraph level
            prefix = ":".join(key.split(":")[:-1])
            subgraph_nodes.setdefault(prefix, {})[key] = node
        else:
            regular_nodes[key] = node

    # Node formatting templates
    default_class_label = "default"

Domain

Subdomains

Frequently Asked Questions

What does draw_mermaid() do?
draw_mermaid() is a function in the langchain codebase, defined in libs/core/langchain_core/runnables/graph_mermaid.py.
Where is draw_mermaid() defined?
draw_mermaid() is defined in libs/core/langchain_core/runnables/graph_mermaid.py at line 45.
What does draw_mermaid() call?
draw_mermaid() calls 2 function(s): _generate_mermaid_graph_styles, _to_safe_id.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free