The Problem: Someone Must Run the Steps in Order
We have a state and some node functions. Now, who decides which one runs first, and what comes next?
The issue
We have three node functions and one state. They must run in the right order.
The first try
Call them one by one in plain Python: the editor, then the scriptwriter, then the translator.
Why it fails
It works for a straight line. But “stop if approved, otherwise go back” needs if-statements and loops that you must write and track yourself. And you cannot pause, save the state or draw the flow.
The fix
Describe the flow instead: list the nodes, and draw the arrows. LangGraph runs it for us.
add_node: Give Each Function a Name
A graph starts as an empty blueprint. We give it the state, and then we add each node with a name.
graph = StateGraph(pipelinestate) graph.add_node("editor", editor_node) graph.add_node("scriptwriter",scriptwriter_node) graph.add_node("translator",tranlator_node)
StateGraph(…)
A new, empty graph. It needs the state class, so it knows the keys.
add_node(name, function)
Adds one node: a name and the function that does the work.
The name
The arrows use the name, not the function. You will see this in the next section.
add_edge: Draw the Arrows
DefinitionSTART and END are two special names. The graph begins at START and stops at END.
An edge is one arrow from one node to the next.
graph.add_edge(START,"editor") graph.add_edge("editor","scriptwriter") graph.add_edge("scriptwriter","translator") graph.add_edge("translator",END)
add_edge(from, to)
One line, one arrow. When
editor finishes, scriptwriter runs next.The first edge
It starts at
START. It says which node runs first.The last edge
It goes to
END. It says where the graph stops.compile and invoke: Run the Graph
The blueprint cannot run yet. We compile it, and then we invoke it.
app = graph.compile() result = app.invoke({ "raw_input" :"AI agents are the future of tech. They can think, plan, and act on their own. LangGraph helps you build these agents with proper control and memory." })
compile()
Checks the blueprint and turns it into an app you can run.
invoke(…)
Give it the starting state. It runs every node in order and returns the final state, with all the keys filled.
A Runnable
The compiled app is a Runnable, like the chains you built. So
invoke, stream and batch all work.To see the graph: print
app.get_graph().draw_mermaid() and paste the text at mermaid.live. It draws the same picture as above.The code is from your
02_sequential.py. The Mermaid tip is from your teaching plan.