The Problem: Steps Need to Share Data
Our graph has many steps. A step in the middle often needs something that an earlier step made.
The issue
We turn a raw text into a Hinglish video script in three steps: clean it up, write the script, translate it.
The first try
Pass the result from one function to the next by hand.
Why it fails
With three steps it works. With branches, loops and parallel steps, you pass many things around and lose track. A step in the middle may need data from two steps ago.
The fix
Keep everything in one shared place: the state. Every step reads from it and writes to it.
What Is State?
DefinitionThe state is the shared data of the graph. It is a dictionary with fixed keys. Every step can read all of it, and every step can write to it.
Your sequential graph has four keys. Watch them fill up, step by step.
class pipelinestate(TypedDict): raw_input : str edited_text : str script_text :str final_output:str
| When | raw_input | edited_text | script_text | final_output |
|---|---|---|---|---|
| Start | your text | – | – | – |
| After the editor | your text | clean text | – | – |
| After the scriptwriter | your text | clean text | video script | – |
| After the translator | your text | clean text | video script | Hinglish text |
The bold cell is the one that step just wrote. Nothing is ever passed by hand.
You start the graph with only one key. In your code:
app.invoke({"raw_input": "..."}). The other keys stay empty until a node fills them.A Node Returns Only What It Changes
A node is a normal Python function. It receives the whole state, but it returns only the keys it wants to change.
def editor_node(state:pipelinestate): # ... build the prompt and call the model ... return {"edited_text":response.content.strip()}
It receives
the whole state, so it can read
raw_input or any other key.It returns
a small dictionary with only the key it changed:
edited_text.LangGraph does the rest
It puts the new value into the state. All the other keys stay as they were.
The first and last lines are from your
02_sequential.py. The comment stands for the prompt and the model call.