Objectives
- Create multiple nodes that sequentially process and update different parts of the state.
- Connect nodes together using normal edges.
- Invoke the graph and see how the state is transformed step-by-step.
Graph III: Sequential Graph
Goal
Build a sequential graph where data flows through multiple nodes, and each node updates a specific part of the shared state.Sample Input
Sample Output
Plan
- Define the state schema (
AgentState) withname,age, andfinalvariables. - Define two node functions:
first_node(to set greeting) andsecond_node(to append age description). - Build, connect sequentially using normal edges, and compile the graph.
- Invoke the graph and view the final result.
Code Implementation
1. Define the State Schema
We define the state structure. Notice thatfinal will be used as a combined output accumulator:
2. Define the Nodes
We define two nodes. The first node sets the greeting using the name, and the second node appends the age explanation:3. Create and Compile the Graph
We add both nodes, connect them usingadd_edge, and compile the graph:
4. Invoke the Graph
Pass the name and age to retrieve the final compiled string:Exercise: Three-Node Sequential Agent 💪
Goal
Connect three nodes sequentially to customize a greeting, display the user’s age, and format a list of their skills.Sample Input
Sample Output
Plan
- Define an
AgentStatecontainingname(str),age(int),skills(list of strings), andresult(str) fields. - Create
greeting_node(to greet),age_node(to append age description), andskills_node(to join skills with commas and ‘and’, appending them to the result). - Build the graph, register all three nodes, connect them in order (
greeting->age->skills), set entry/finish points, compile and run.
Solution
Solution
Practice & Exercises
To reinforce what you’ve learned in this section (sequential node execution and data transformation), practice with the interactive notebook:Practice & Exercises
Practice defining multiple sequential nodes, linking them with edges, and building a sequential agent.💻 VS Code | 🚀 Colab | 📥 Download