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 workflows in LangGraph, you must install the core library and its dependencies, typically within a virtual environment.
- langgraph: The primary orchestration framework.
- langchain: Required for LLM components like chat models and prompt templates.
- langchain-openai: To interface specifically with OpenAI models.
- python-dotenv: For managing environment variables (API keys).
- Jupyter Notebook: Recommended for development because it allows for easy graph visualization.
2. The Five-Step Implementation Framework #
Every LangGraph workflow, regardless of complexity, follows these five fundamental steps:
Step 1: Define the State #
The State is a shared dictionary that tracks information throughout the workflow. You define it using a TypedDict, which allows you to specify the data types for each key.
from typing import TypedDict
class MyWorkflowState(TypedDict):
input_data: str
intermediate_result: str
final_output: str
Step 2: Create the Graph Object #
Initialize the graph using the StateGraph class, passing in your state definition.
from langgraph.graph import StateGraph
builder = StateGraph(MyWorkflowState)
Step 3: Define and Add Nodes #
Nodes are the functional units of your graph. Each node is a standard Python function that receives the current state as input and returns a partial update to that state.
def my_node_function(state: MyWorkflowState):
# Logic goes here
return {"intermediate_result": "processed_data"}
builder.add_node("node_name", my_node_function)
Step 4: Add Edges #
Edges define the control flow. Use START and END constants to mark the boundaries of your workflow.
from langgraph.graph import START, END
builder.add_edge(START, "node_name")
builder.add_edge("node_name", END)
Step 5: Compile and Execute #
Compile the graph into a runnable workflow. You then execute it by calling .invoke() with an initial state.
workflow = builder.compile()
result = workflow.invoke({"input_data": "hello"})
3. Practical Example: AI Blog Generator (Prompt Chaining) #
This example demonstrates Prompt Chaining, where the output of one LLM call (an outline) serves as the input for the next (the final blog).
The Goal: Topic → Generate Outline → Generate Blog Content.
State Definition
The state persists the topic, the generated outline, and the final content.
class BlogState(TypedDict):
title: str
outline: str
content: str
Node Logic
- Create Outline Node: Takes the title, prompts the LLM, and saves the outline to the state.
- Create Blog Node: Takes the title and the outline from the state to generate the full blog.
Graph Assembly
builder = StateGraph(BlogState)
# Add Nodes
builder.add_node("create_outline", create_outline_fn)
builder.add_node("create_blog", create_blog_fn)
# Define Sequential Edges
builder.add_edge(START, "create_outline")
builder.add_edge("create_outline", "create_blog")
builder.add_edge("create_blog", END)
# Compile
workflow = builder.compile()
4. Key Advantages of LangGraph Workflows #
- State Persistence: Unlike traditional LangChain chains, the entire state is accessible at the end. In the blog example, the final output contains the title, the outline, and the blog content, whereas a simple chain might only return the final content.
- Visual Debugging: You can visually print and verify your graph structure in Jupyter to ensure nodes are correctly connected before execution.
- Granular Updates: Each node only needs to return the specific keys it updated; LangGraph handles merging these updates into the global state.
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.