AI Hints
Home›LangGraph Tutorial›Running and Drawing Graphs
LangGraph · Lesson 9 of 120

Drawing the Graph

See its shape.

⏱ 2 min read▶ Video at 8:32🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

As graphs grow, it gets hard to picture them from code. LangGraph can describe any graph as a diagram, in a text format called Mermaid, which many tools, including GitHub, can draw.

Drawing the Graph
From the video: Drawing the Graph
  • Graphs grow
  • Hard to picture
  • Draw a diagram

A Mermaid diagram

Get graph returns a description of the graph. Draw mermaid turns it into Mermaid text. We print only the arrow lines, to keep it short. Run it. Each line is one edge, from start, through our three steps, to the end.

draw.pypython
from typing import TypedDict
from langgraph.graph import StateGraph, START, END

class State(TypedDict):
    text: str

def step(name):
    return lambda state: {"text": state["text"] + " -> " + name}

builder = StateGraph(State)
for name in ["check", "price", "pack"]:
    builder.add_node(name, step(name))
builder.add_edge(START, "check"); builder.add_edge("check", "price")
builder.add_edge("price", "pack"); builder.add_edge("pack", END)

mermaid = builder.compile().get_graph().draw_mermaid()
print("\n".join(line.strip() for line in mermaid.splitlines() if "-->" in line))
Output
__start__ --> check;
check --> price;
price --> pack;
pack --> __end__;

Ways to see a graph

Paste Mermaid text into a markdown file on GitHub, or the Mermaid live editor, to see the picture. Draw mermaid PNG makes an image directly. And in a Jupyter notebook, the graph can display itself.

keyvalue
Mermaid textdraw_mermaid()
Imagedraw_mermaid_png()
GitHubrenders Mermaid
Notebooksdisplay inline

Summary

Get graph and draw mermaid show the shape of any graph. Use them to check your edges. Next, building a pipeline of several steps.

Key takeaways
  • get_graph()
  • draw_mermaid()
  • Check your edges
Next up: A Pipeline — nodes in a row