A function that takes a dictionary
Before any of this involves LangGraph, it involves an ordinary Python function. Here is the whole shape of the thing.
def greet(state):
return {"greeting": "Hello!"}
print(greet({}))It takes one argument and gives back a dictionary. Run it and you get that dictionary. Nothing clever is happening yet.
Why is the argument called state? Because in a few lessons something will be passing one dictionary from function to function, and that dictionary is what we will call the state. Your function reads it and hands back the bits it wants to change. Getting used to the name now costs nothing.
greeting. It does not rebuild the whole dictionary. Every function you write from here on behaves like that.Now describe the dictionary
A dictionary can hold anything, which is convenient until six functions are writing to it and you have lost track of what is in there. So you write the shape down.
from typing_extensions import TypedDict
class State(TypedDict):
greeting: str
def greet(state):
return {"greeting": "Hello!"}
print(greet({}))The four new lines say: there is a dictionary, it has one key called greeting, and that key holds a string.
Nothing has changed about how the program runs, which is why the output is identical. You have only written down what you already knew.
TypedDict is plain Python, from the standard library. It exists to describe the shape of a dictionary and it does nothing at runtime. It is not a LangGraph idea and you can use it anywhere.- Change
"Hello!"to your own name and run it again. - Add a second key called
sendertoState, and return it fromgreetas well. - Return
{}instead and look at what prints.
Little by little, you're building something great.