Graphs are a fundamental data structure used to model relationships between entities. In Python, LangGraph provides an intuitive way to build stateful computation graphs, making it a powerful tool for workflows, AI pipelines, and complex state transitions.
In this blog post, we’ll explore the core concepts of Nodes, Edges, and State by implementing a simple StateGraph with LangGraph. We’ll also visualize the graph to understand its structure.
- Understanding the Core Concepts
Before diving into the code, let's break down the essential components:
🟢 Nodes (Vertices)
A node represents a point in the graph where computation or a transition occurs. It can be thought of as a function that modifies or processes data.
🔵 Edges (Connections)
An edge is a connection between nodes, defining how data flows through the graph. It determines the sequence of execution.
🟡 State (Data Structure)
State represents the information passed between nodes. In our example, we use Python’s TypedDict to define structured data.
- Defining the Graph Structure
We start by defining a TypedDict that represents the state of our system:
from typing import TypedDict
class InputState(TypedDict):
string_value: str
numeric_value: int
Here, InputState ensures that our state always contains a string_value (a string) and a numeric_value (an integer).
- Creating Nodes to Modify State
Each node processes the state. In this case, we define a simple function that returns the input without modification (for demonstration purposes).
def modify_state(input: InputState):
return input
Later, we can extend this function to modify the state dynamically.
- Constructing the Graph with LangGraph
Now, let's create a StateGraph that processes data through multiple nodes.
from langgraph.graph import END, START, StateGraph
graph = StateGraph(InputState)
graph.add_node("branch_a", modify_state)
graph.add_node("branch_b", modify_state)
graph.add_edge(START, "branch_a") # Connect START to first node
graph.add_edge("branch_a", "branch_b") # Connect first node to second node
graph.add_edge("branch_b", END) # Connect second node to END
runnable = graph.compile()
🔗 Breaking Down the Graph Structure
We create the graph using StateGraph(InputState), ensuring that it adheres to the InputState structure.
Nodes (branch_a, branch_b) are added—each node will process the state.
-
Edges define transitions:
`START → branch_a branch_a → branch_b branch_b → END`
Setting the entry point tells the graph where execution begins.
- Visualizing the Graph
To understand the structure better, we use Mermaid diagrams to generate a visual representation of the graph.
from IPython.display import Image, display
from langchain_core.runnables.graph import MermaidDrawMethod
display(
Image(
runnable.get_graph().draw_mermaid_png(
draw_method=MermaidDrawMethod.API,
)
)
)
This will produce a flowchart-like diagram showing the nodes and how they connect.
- Why Use LangGraph?
LangGraph makes it easy to:
Build workflows with structured state transitions.
Ensure type safety using TypedDict.
Visualize and debug complex processes.
Integrate with LangChain for AI-based applications.
Top comments (0)