get_graph().draw_mermaid(): text graph representation
Why this matters
When your agent graphs grow beyond 3-4 nodes, mental debugging fails. <code>draw_mermaid()</code> lets you inspect the actual topology, edge routing, and conditional branching you built: catching logic errors before they reach production.
Explanation
What it is: get_graph().draw_mermaid() is a method on a compiled LangGraph StateGraph that returns a Mermaid syntax string representing your graph's nodes, edges, and control flow. How it works: When you call .compile() on a StateGraph, it produces a runnable graph object. That object has a get_graph() method which returns the underlying graph structure. Calling draw_mermaid() on that structure walks the node list and edge definitions, then formats them as valid Mermaid markdown: which renders as a flowchart in tools like GitHub, Obsidian, or mermaid.live. When to use it: Use this when onboarding a teammate to your agent architecture, when debugging unexpected node routing, or when documenting what you actually built (not what you thought you built).
Analogy
It's like inspecting the blueprint of a building after construction: the <code>draw_mermaid()</code> output shows what was actually connected, not what was in your head.
Code
from langgraph.graph import StateGraph, START, END
from typing_extensions import TypedDict
class State(TypedDict):
value: int
def add_one(state: State) -> State:
return {"value": state["value"] + 1}
def double(state: State) -> State:
return {"value": state["value"] * 2}
def is_greater_than_five(state: State) -> str:
if state["value"] > 5:
return "yes"
return "no"
graph = StateGraph(State)
graph.add_node("add", add_one)
graph.add_node("double", double)
graph.add_node("check", is_greater_than_five)
graph.add_edge(START, "add")
graph.add_edge("add", "double")
graph.add_conditional_edges("double", is_greater_than_five, {"yes": END, "no": "add"})
compiled = graph.compile()
mermaid_str = compiled.get_graph().draw_mermaid()
print(mermaid_str) graph TD
__start0[__start]
__end0[__end]
add[add]
double[double]
check[check]
__start0 --> add
add --> double
double --> check
check -->|yes| __end0
check -->|no| add What just happened?
The code defined a three-node graph with conditional routing. When <code>get_graph().draw_mermaid()</code> was called, it inspected the compiled graph's internal node and edge structures, then formatted them as a Mermaid flowchart string. That string shows START routing to the 'add' node, 'add' routing to 'double', 'double' routing to a conditional 'check' node, and 'check' branching to either END (if 'yes') or back to 'add' (if 'no').
Common gotcha
Developers call draw_mermaid() on the StateGraph object directly instead of on the compiled graph. You must call .compile() first, then .get_graph().draw_mermaid() on the result. Calling it on the uncomplied StateGraph will raise an AttributeError.
Error recovery
AttributeError: 'StateGraph' object has no attribute 'get_graph'TypeError: draw_mermaid() takes 1 positional argument but 2 were givenExperienced dev note
The diagram string is cheap to generate but expensive to parse mentally. Copy the output into mermaid.live immediately: don't try to read the raw string. Also: if your diagram has more than 8 nodes, your graph is likely too monolithic. That's a sign to split it into sub-graphs or rethink your node boundaries, not a reason to avoid draw_mermaid().
Check your understanding
If you add a new conditional edge to your graph but draw_mermaid() doesn't show it, what is the most likely cause?
Show answer hint
The answer involves the difference between modifying the StateGraph object versus working with the compiled graph. A correct answer identifies that the diagram was generated from a stale compiled graph that was created before the new edge was added.