Introduction to Sequential Graphs
Okay, welcome to your third graph. What are we going to do this time? Well, enough processing multiple values and everything. Let’s actually get the graph more complicated. That’s why we’re going to be building a sequential graph. All it all that basically means is we’re going to be creating and handling multiple nodes that can sequentially process and update different parts of the state.
Step 1: Imports and Type Dictionary
Cool. So now we’re about to code up the third graph. And we’re making quite fast progress. Well done on that. Again, the imports are the same. State graph and type dictionary. Perfect.
from typing_extensions import TypedDict
from langgraph.graph import StateGraph
Step 2: Agent State (Typed Dictionary)
And like we’ve done in the previous two graphs, we’re going to be coding the the state schema or the agent state first. Let’s have class agent state. And again, it needs to be in the form of a typed dictionary, right? And in this case, let’s have the three attributes as all strings because we’ve already we already know how to handle multiple data types, right? Let’s keep it simple. Name string, age string, and final string.
class AgentState(TypedDict):
name: str
age: str
final: str
Step 3: Node Functions (Actions)
Okay. Now, here’s what we’re going to build. Now, we’re about to build our two node functions, which are again the actions. Okay. Again you simply write first well I’ll name it first node in this case and like I mentioned before we pass in the state and we return the updated state. This is the first node of our sequence. Okay. And what do we want to do in this specific node? Well, I really just want to manipulate the final part. So, let’s say something like state final is equal to state or let’s have an f string f state name. Let’s say something like hi that. And we’ll just return the state. Perfect.
def first_node(state: AgentState) -> AgentState:
"""This is the first node of our sequence."""
state["final"] = "hi " + state["name"]
return state
And now again we create a new node and state agent state. Return that. Perfect. And I’m just going to copy this doc string and just change it. This is the second nerf. Perfect. Okay. To speed things up. And in this case I also want to have state final is equal to you are state age years old. Again quite a simple example easy to follow. That’s why I’ve kept it as quite a basic graph.
def second_node(state: AgentState) -> AgentState:
"""This is the second node."""
state["final"] = "you are " + state["age"] + " years old"
return state
There is one logical error which I’ve put deliberately here. I want you to try to identify it.
Step 4: Fixing the Logical Error
Okay. So the logical error in this case is the that once we’ve built our graph and everything what would have happened is we would have said hi to whoever we pass in let’s say Charlie or something. So, hi Charlie. And we store that in the final attribute in the state, which is what we want. But here’s where things get like start to be well logically incorrect.
def second_node(state: AgentState) -> AgentState:
"""This is the second node."""
state["final"] = state["final"] + ", you are " + state["age"] + " years old"
return state
Step 5: Building the Sequential Graph and Adding Edges
Okay. Now let’s get to the fun part. How do we actually build this graph? And really it’s quite similar to the previous two graphs except there is one new thing which you’re about to learn. Like always we use state graph to start the framework. Agent state and let’s store it in graph. Again I could have had this name the width variable into anything. I’ve just kept it graph because it makes intuitive sense.
graph = StateGraph(AgentState)
graph.add_node("first_node", first_node)
graph.add_node("second_node", second_node)
graph.set_entry_point("first_node")
graph.add_edge("first_node", "second_node")
graph.set_finish_point("second_node")
app = graph.compile()
Step 6: Invoking the Sequential Graph
So now that we’ve built that, let’s again invoke this. I’ve got this code ready here. Let’s invoke it. Let’s pass the parameter as Charlie and let’s pass the age as 20. Cool. Print result.
result = app.invoke({
"name": "Charlie",
"age": "20",
"final": ""
})
print(result)
print(result["final"])
Exercise: Building a Sequential Graph with Three Nodes
Awesome. So now we will move on to the exercise for this third graph. And what I want you to do is really build on top of what we just covered. Instead of two nodes, I want you to build three nodes. Again, in a sequence, don’t need to go too fancy yet. We will again three nodes in a sequence. And we will have you will need to accept the user’s name, their age, and a list of their skills.
Conclusion
In this article, we have learned how to build a sequential graph using the LangGraph library. We have created a graph with two nodes and added edges between them. We have also learned how to invoke the graph and print the result.
FAQs
Q: What is a sequential graph?
A: A sequential graph is a graph where nodes are connected in a sequence, and each node processes and updates the state before passing it to the next node.
Q: How do we add edges between nodes in a sequential graph?
A: We add edges between nodes using the add_edge method, specifying the start and end nodes.
Q: How do we invoke a sequential graph?
A: We invoke a sequential graph using the invoke method, passing in the input state.
Q: What is the purpose of the first_node and second_node functions?
A: The first_node and second_node functions are the actions that are performed on the state at each node. They update the state and return the updated state.









