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

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.

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

Notice what the function does not do
It returns only 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.

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

Where TypedDict comes from
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.
Try it yourself
  • Change "Hello!" to your own name and run it again.
  • Add a second key called sender to State, and return it from greet as well.
  • Return {} instead and look at what prints.

Little by little, you're building something great.