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

Two steps in a row

Add a second node and an edge between them. Now the first node's work is there for the second one to use.

Where the run goes
START. The run begins.Step 1 of 4

A state with room for both

Three keys now. The ticket that came in, the category the first node works out, and the reply the second one writes.

python
from typing_extensions import TypedDict

class State(TypedDict):
    ticket: str
    category: str
    reply: str

The two nodes

python
def categorise(state):
    return {"category": "billing"}

The second one reads what the first one wrote. This line is the whole point of the lesson.

python
def write_reply(state):
    return {"reply": f"Sending this to our {state['category']} team."}

That reads the category out of the state. It is empty when the run starts, and has a value here only because categorise returned one a moment earlier.

Wiring them in order

python
from langgraph.graph import StateGraph, START

builder = StateGraph(State)
builder.add_node("categorise", categorise)
builder.add_node("write_reply", write_reply)
builder.add_edge(START, "categorise")
builder.add_edge("categorise", "write_reply")

The second edge is the new part. It is the only thing that says which of the two nodes runs first.

Run it

Example
graph = builder.compile()

print(graph.invoke({"ticket": "charged twice", "category": "", "reply": ""})["reply"])

The word billing in that sentence was not written by write_reply. It came out of the state, where categorise put it a moment earlier. That is the whole reason the state exists.

The order you add nodes does not matter
Try swapping the two add_node lines. The output does not change. Nodes are only registered by name, and the edges decide the order.
Try it yourself
  • Swap the two add_node lines and confirm the output is identical.
  • Swap the two add_edge lines instead and read the error.
  • Add a third node that puts the reply in capitals, and wire it in after write_reply.

This is what real progress feels like.