Sequential Workflows with LangGraph #
A sequential workflow is a linear architectural pattern where tasks are executed in a specific, one-after-another order. In LangGraph, these workflows are represented as a directed graph where control flows from a start point, through a series of nodes, to an end point without branching or parallel paths.
1. Prerequisites and Environment Setup #
To build a sequential workflow with LangGraph, you need LangGraph, LangChain, and an LLM provider. In this example, we will use Ollama with the Gemma 3 1B model, so you can run the LLM locally.
You can use different LLM providers such as OpenAI, Google Gemini, Anthropic, Ollama, and others. Just change the LLM configuration, model name, and required API or environment setup according to the provider you choose.
Install the required packages #
pip install langgraph langchain langchain-ollama
Make sure Ollama is installed and that the Gemma 3 1B model is available.
ollama pull gemma3:1b
2. The Five-Step Implementation Framework #
A basic LangGraph workflow can be understood through five fundamental steps:
- Define the State
- Create the Graph
- Create and Add Nodes
- Add Edges
- Compile and Execute
Step 1: Define the State #
The State is the shared data structure that travels
through the workflow. In Python, we can define it using
TypedDict.
from typing import TypedDict
class State(TypedDict):
topic: str
explanation: str
example: str
summary: str
Here, the state contains four pieces of information:
topic– The topic provided by the user.explanation– Explanation generated by the LLM.example– Example generated from the explanation.summary– Final summary generated by the LLM.
Step 2: Create the Graph #
We create a StateGraph using our state definition.
from langgraph.graph import StateGraph, START, END
graph = StateGraph(State)
Step 3: Create and Add Nodes #
A node is a Python function that performs a specific task. Each node receives the current state and returns an update to it.
In our example, we have three nodes:
- Explain – Explains the topic.
- Example – Generates a real-world example.
- Summary – Creates a final summary.
Step 4: Add Edges #
Edges determine the order in which nodes execute.
graph.add_edge(START, "explain")
graph.add_edge("explain", "example")
graph.add_edge("example", "summary")
graph.add_edge("summary", END)
Step 5: Compile and Execute #
After defining the graph, we compile it and execute it using
invoke().
app = graph.compile()
result = app.invoke({
"topic": "Machine Learning"
})
3. Practical Example: Sequential AI Workflow #
Now let’s build a complete sequential workflow using LangGraph + Ollama + Gemma 3.
Complete Code #
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
from langchain_ollama import ChatOllama
# 1. Create LLM
llm = ChatOllama(model="gemma3:1b")
# 2. Define State
class State(TypedDict):
topic: str
explanation: str
example: str
summary: str
# 3. Node 1 - Explain topic
def explain_topic(state: State):
topic = state["topic"]
res = llm.invoke(
f"Explain {topic} in very simple language."
)
return {
"explanation": res.content
}
# 4. Node 2 - Create example
def example(state: State):
explanation = state["explanation"]
res = llm.invoke(
f"Give a simple real-world example of this explanation:\n{explanation}"
)
return {
"example": res.content
}
# 5. Node 3 - Create summary
def summary(state: State):
explanation = state["explanation"]
example_text = state["example"]
res = llm.invoke(
f"""
Create a short summary using:
Explanation:
{explanation}
Example:
{example_text}
"""
)
return {
"summary": res.content
}
# 6. Create Graph
graph = StateGraph(State)
# 7. Add Nodes
graph.add_node("explain", explain_topic)
graph.add_node("example", example)
graph.add_node("summary", summary)
# 8. Connect Nodes
graph.add_edge(START, "explain")
graph.add_edge("explain", "example")
graph.add_edge("example", "summary")
graph.add_edge("summary", END)
# 9. Compile
app = graph.compile()
# 10. Run
result = app.invoke({
"topic": "Machine Learning"
})
# 11. Print result
print(result["summary"])
4. How This Workflow Works #
When we execute the workflow, the input first enters the Explain node.
Input #
topic = "Machine Learning"
Explain Node #
The LLM explains Machine Learning in simple language and stores
the result in explanation.
Example Node #
The node reads state["explanation"] and asks the LLM
to generate a real-world example.
Summary Node #
The node reads both the explanation and example and generates the final summary.
Final Output #
The final summary is available using
result["summary"].
5. Understanding State Flow #
The most important concept in this example is how the state changes as it moves through each node.
{
"topic": "Machine Learning"
}
{
"topic": "Machine Learning",
"explanation": "..."
}
{
"topic": "Machine Learning",
"explanation": "...",
"example": "..."
}
{
"topic": "Machine Learning",
"explanation": "...",
"example": "...",
"summary": "..."
}
6. Graph Structure #
The final LangGraph workflow is a simple linear sequence:
7. Key Advantages of Sequential Workflows #
- Simple Flow: Tasks execute in a predictable order.
- Shared State: Every node can access information stored in the state.
- LLM Integration: Each node can use an LLM for a different task.
- Modular Design: Each task is separated into its own node.
- Easy to Extend: Additional nodes can be added to the workflow.
8. Important LangGraph Concepts #
Stores the data flowing through the workflow.
A Python function that performs a task.
Defines how control moves from one node to another.
Defines where the workflow begins.
Defines where the workflow finishes.
Provides AI capabilities inside the nodes.
Q.1 What is the primary reason the instructor suggests using LangGraph even when LangChain already exists as a framework?
LangGraph is only used for visualizing graphs and does not execute AI logic.
LangGraph is designed to handle complex circular and non-linear workflows that LangChain's linear chains struggle with.
LangGraph completely replaces LangChain components like Chat Models and Prompt Templates.
LangGraph is a compiled C++ version of LangChain.
Explanation
LangGraph extends LangChain by supporting graph-based workflows with loops, branching, state management, and complex execution paths that are difficult to implement using simple sequential chains.
Q.2 When defining a state for a LangGraph workflow using TypedDict, what is the primary purpose of this dictionary?
To define the order in which nodes execute.
To act as a shared schema that stores data passed between and updated by nodes.
To store only the final output for the user.
To store API keys and environment variables.
Explanation
A TypedDict defines the structure of the graph’s shared state. Each node reads from and updates this state as execution progresses, allowing information to flow throughout the workflow.
Q.3 Which Python library is commonly used to define strongly typed shared state in LangGraph examples?
collections
typing.TypedDict
json
dataclasses
Explanation
LangGraph commonly uses TypedDict from Python’s typing module to define the schema of the shared state, making workflows more structured and type-safe.
Q.4 Technically, what is a Node in the context of LangGraph implementation?
A specialized JSON configuration file.
A prompt template sent directly to an LLM.
A connection between two AI models.
A Python function that accepts the current state and returns an updated version of that state.
Explanation
A node is simply a Python function that performs a task using the current state and returns updated state information for subsequent nodes.
Q.5 Which specific step is required before a graph can be executed using the .invoke() method?
Every node must call an LLM.
The graph must be compiled using the .compile() method.
The graph must be uploaded to LangChain Cloud.
The graph must be converted into YAML.
Explanation
After defining all nodes and edges, the graph must be compiled. Compilation validates the workflow and produces an executable graph object that supports methods such as invoke().
Q.6 In a sequential workflow, what is the significance of the START and END constants imported from langgraph.graph?
They store API keys for the workflow.
They are virtual nodes that define where execution begins and ends.
They automatically clear memory after execution.
They represent the physical servers executing the graph.
Explanation
START and END are special virtual nodes used to specify the entry point and exit point of the graph, defining the overall execution flow.
Q.7 Why does the instructor choose Jupyter Notebooks (IPYNB) for these LangGraph tutorials instead of standard Python files?
LangGraph only works inside notebooks.
Python files cannot use .env files.
Notebooks allow the compiled graph structure to be visualized using display utilities.
Jupyter automatically fixes syntax errors.
Explanation
Jupyter Notebooks make it easy to visualize the compiled graph using Mermaid diagrams and display tools, helping developers understand the workflow architecture interactively.
Q.8 In the Prompt Chaining example for blog generation, what is the role of the 'Create Outline' node?
It publishes the completed blog.
It checks grammar before publishing.
It searches the web for articles.
It generates a structured outline from the topic for the next node to expand.
Explanation
The Create Outline node transforms the input topic into a structured outline, providing the next node with an organised plan for generating the complete blog.
Q.9 What is a distinct advantage of using LangGraph for prompt chaining compared to standard LangChain Chains regarding state visibility?
All intermediate outputs remain available in the shared state after execution.
Prompt templates are no longer required.
Simple LLM calls always require less code.
LLM token costs are automatically reduced.
Explanation
LangGraph preserves every intermediate value in the shared state, allowing later nodes, debugging tools, and developers to inspect outputs such as outlines, summaries, or retrieved documents.
Q.10 If you wanted to add a 'Score' to your blog post in the suggested homework, which part of the code must be modified first?
The .env file.
The START node.
The .compile() method.
The BlogState TypedDict definition.
Explanation
Since the shared state schema is defined inside the BlogState TypedDict, any new information such as a score must first be added there before nodes can read or update it.