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

Your first graph

Same function, same output. The difference is that this time you do not call it. LangGraph does.

Right now Python has no idea your function is special. To let something else run it for you, you first have to describe your program, and then run the description.

Example
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START

class State(TypedDict):
    greeting: str

def greet(state):
    return {"greeting": "Hello!"}

builder = StateGraph(State)
builder.add_node("greet", greet)
builder.add_edge(START, "greet")
graph = builder.compile()

print(graph.invoke({"greeting": ""}))

The same answer as the last lesson. What changed is who ran greet. You did not call it this time.

The five new lines, one at a time

LineWhat it does
StateGraph(State)Start describing a program whose dictionary has the shape State.
add_node("greet", greet)Add a step. It has a name you choose and a function that runs.
add_edge(START, "greet")Connect the beginning of the run to that step.
compile()Turn the description into something runnable. Nothing has run yet.
invoke({...})Run it, starting from this dictionary. You get the finished dictionary back.

Three new words arrive with those lines, and they are the only vocabulary you need for a while. A node is a step, which is a function with a name. An edge is a connection between steps. A graph is the whole description once it is compiled.

That is the pattern for every LangGraph program you will ever write. Describe, compile, run.

Two things you might be wondering

Why does a node need a name as well as a function? The name is how you refer to that step later, when you connect it to others. Keeping the name separate from the function is what lets you wire steps together by name instead of by variable.

Where does it stop? You never said. A node with nothing after it ends the run. Once your program has more than one way to finish, you will start saying so explicitly, and there is a word for that which arrives in lesson 6.

Try it yourself
  • Rename the node from greet to hello. You will have to change the edge too, or it will complain.
  • Remove the add_edge line and read the error you get.
  • Call greet({}) yourself on the last line instead of using graph.invoke, and confirm you get the same dictionary.

You understood something today that you didn't yesterday.