Seeing your graph
Your graph can describe itself. From here on, when a program does something you did not expect, you can look at its shape instead of guessing.
Take the two node graph from the last lesson, cut down to the parts that matter here.
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START
class State(TypedDict):
ticket: str
reply: strbuilder = StateGraph(State)
builder.add_node("categorise", lambda state: {"ticket": state["ticket"]})
builder.add_node("write_reply", lambda state: {"reply": "We will refund it."})
builder.add_edge(START, "categorise")
builder.add_edge("categorise", "write_reply")Ask it what it is
Every compiled graph has a get_graph() method. The simplest thing to do with it is ask for the nodes and the edges.
shape = builder.compile().get_graph()
print("nodes:", list(shape.nodes))
print("edges:", [(e.source, e.target) for e in shape.edges])You wrote two edges. There are three. LangGraph added the last one because nothing comes after write_reply, which is exactly what lesson 2 said would happen. Now you can see it rather than take it on trust.
A picture instead of a list
The same method can hand you a diagram as text, in a format called mermaid. Paste it into any mermaid viewer and you get a picture.
print(builder.compile().get_graph().draw_mermaid())It is only text, so it costs nothing and needs no extra packages. In a notebook there is draw_mermaid_png(), which draws it directly, though that one needs a drawing library installed.
- Remove the
add_edge(START, ...)line, compile, and look at the edges again. - Add a third node with no edges and find it in the node list.
- Paste the mermaid text into a mermaid viewer and look at the picture.
Every expert started right here.