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 →
What a node sees and what it returns
Every node is handed the same dictionary. Print it from inside and there is no mystery left about what a node knows.
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START
class State(TypedDict):
ticket: str
category: str
def categorise(state):
print("the node sees:", state)
return {"category": "billing"}
builder = StateGraph(State)
builder.add_node("categorise", categorise)
builder.add_edge(START, "categorise")
graph = builder.compile()
print("we get back: ", graph.invoke({"ticket": "charged twice", "category": ""}))Read the two lines
The first line was printed inside the node. It shows the dictionary exactly as it arrived, with the ticket you passed in and an empty category.
The second line is what came back out. The ticket is still there, untouched, and the category has been filled in.
So the node returned one key and got a two key dictionary back. LangGraph merged what you returned into what was already there. That merging is the only thing it did.
This is your program's memory
Anything a later node needs has to be in the state. If a node works something out and does not return it, it is gone the moment that function ends.
Worth remembering
This is the most common first bug. A node computes the right answer, forgets to return it, and the next node sees an empty string. Print the state when something is missing and you will find it in seconds.
Try it yourself
- Add a
customerkey and pass a name in when you invoke. - Return
{}from the node and look at the final dictionary. - Try returning a key that is not in
Stateand read the error.
Slow is fine. Stopping is the only problem.