LangGraph Beginner to Advanced: Part 4: Sequential Graph
Last Updated on September 30, 2025 by Editorial Team
Author(s): Talib
Originally published on Towards AI.

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. We will learn how to connect nodes together in a graph through edges of course and we’re going to invoke the graph and really see how the state gets transformed as we progress through our graphs step by step. Again your main goal is should be to understand how to create and handle multiple nodes in langraph. Sounds cool. Okay I’ll see you at the code.
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.
# imports (as said in transcript)
from typing_extensions import TypedDict # type dictionary
from langgraph.graph import StateGraph # state graph
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 is equal to f state name: hi
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 is equal to: you are state age years old
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.
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.
Once we finally get to our second node, again, we’re updating state final, which you can do. You can repeat, you can interact with these attributes at in in any node possible in all of the nodes. And you can do it as many times as you want. But notice this part. What’s happening here is we’ve completely replaced all of the content we had before.
So remember how we had hi Charlie? We’ve just completely replaced it with you are age years old. But we want both of them both of those stuff, right? How do we get both of them? Well, again we just concatenate them. We can have something like state plus state file. And there we go. Logical error should be now solved, right? Cuz now we have concatenated state final. We’re essentially just like adding on to we’re preserving what we had before, right?
# fix the logical error by concatenating the previous final
def second_node(state: AgentState) -> AgentState:
"""This is the second node."""
state["final"] = state["final"] + ", you are " + state["age"] + " years old"
return state
Step 4: Build the Sequential Graph and Add Edge
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.
Okay. Now we add our nodes. We do graph add node. And for simplicity sake I’m just going to have the name as the same name as the function. Okay. That way it’ll just be easy to follow. So graph add node and second node. Second node. Cool. Okay. Now that we’ve added both nodes, we need to obviously s add the entry point and the end point, right?
We set the entry point like this. Again, quite self-explanatory because we wanted to connect to the first node, not the second node, right? So it should be start first node, second node, end point. How do we connect the first node and the second node together though? Hopefully you had an answer for that. If you remember or recall from the previous section, theory section, there was an element in called the edge.
That’s exactly what we’re about to do right now. We’re about to use edge and that was the new thing which I was talking about a few moments ago which you’re about to learn. So how do we use it? Well you use graph edge add edge and if we can hi perfect.
So again it’s quite simple you use a start key and end key. Similar to entry point where but your in this case you need to pass two parameters. The edge we want is between the first node and the second node right? Well that’s exactly what we pass here. First node and second node and like before we will just set the finish point at second node and we will compile this.
# framework and edges
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") # start key -> end key
graph.set_finish_point("second_node")app = graph.compile()
Now how will this graph look like? Take a moment to try to think of how it will look like like that. Start point end point and these two notes are sandwiched in between. But now there is a edge. It should be called a directed edge if I’m being like quite picky. But yes, a directed edge cuz the flow of data or your flow of your state updates is from the first node to your second node. Right?
Step 5: Invoke 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"]) # hi Charlie, you are 20 years old
Perfect. Apart from the misalignment here which I can just change right now. Perfect. Okay. Now you can see it says hi Charlie you are 20 years old.
Exercise (Sequential Graph)
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. So the first node will be specifically for personalizing the name field with a greeting. The second node will be describing the user’s age. The third node will be listing all of the user skill in a formatted string. And then you’ll need to combine this and store it in a result field and output that. And this should be a combined message. And the format I would like you to output is something like this. So let’s say the name was Linda. And let’s say Linda welcome to the system. You are 31 years old and you have skills in Python, machine learning and langraph. Okay. And just as a hint for this exercise, I would you’ll need to use the add edge method twice. So this will really solidify your understanding on how to build graphs in general. All right, cool. So once you’ve done that, again, answers will be on GitHub for all of the exercises. Once you have cross referenced and checked that you’ve done it right, I will see you in the next section where we build our fourth graph. All right, see you there.
Catch the whole LangGraph Series here: LangGraph Reading List
Code is available here: LangGraph Github
Thank you for reading!
Subscribe to my FREE AI NEWSLETTER | Mohammed Talib | Substack
I like to break down complex topics in simple words
substack.com
You might be interested in Reading!
- Where did multi-agent systems come from?
- Summarising Large Documents with GPT-4o
- How does LlamaIndex compare to LangChain in terms of ease of use for beginners
- Pre-training vs. Fine-tuning [With code implementation]
- Costs of Hosting Open Source LLMs vs Closed Sourced (OpenAI)
- Embeddings: The Back Bone of LLMs
- How to Use a Fine-Tuned Language Model for Summarization
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.
Published via Towards AI
Towards AI Academy
We Build Enterprise-Grade AI. We'll Teach You to Master It Too.
15 engineers. 100,000+ students. Towards AI Academy teaches what actually survives production.
Start free — no commitment:
→ 6-Day Agentic AI Engineering Email Guide — one practical lesson per day
→ Agents Architecture Cheatsheet — 3 years of architecture decisions in 6 pages
Our courses:
→ AI Engineering Certification — 90+ lessons from project selection to deployed product. The most comprehensive practical LLM course out there.
→ Agent Engineering Course — Hands on with production agent architectures, memory, routing, and eval frameworks — built from real enterprise engagements.
→ AI for Work — Understand, evaluate, and apply AI for complex work tasks.
Note: Article content contains the views of the contributing authors and not Towards AI.