LangGraphLangGraph 1.2 · Python 3.10+
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
37 small wins to finish your pathNext lesson

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.

python
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START

class State(TypedDict):
    ticket: str
    reply: str
python
builder = 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.

Example
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.

The graph that printed, drawn out
nodes__start__, added for youcategorise, yourswrite_reply, yours__end__, added for youedges__start__ to categorisecategorise to write_replywrite_reply to __end__your graph

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.

Example
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.

Why this is here and not at the end
From the next lesson your graphs stop being straight lines. They branch, they loop, and they run two things at once. Printing the shape is the fastest way to check that the program you built is the program you meant to build.
Try it yourself
  • 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.