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.
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.
from typing_extensions import TypedDict
class State(TypedDict):
ticket: str
category: str
reply: strThe two nodes
def categorise(state):
return {"category": "billing"}The second one reads what the first one wrote. This line is the whole point of the lesson.
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
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
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.
add_node lines. The output does not change. Nodes are only registered by name, and the edges decide the order.- Swap the two
add_nodelines and confirm the output is identical. - Swap the two
add_edgelines 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.