Skip to main content
In this section, we will see how to handle multiple inputs inside the shared AgentState and perform operations using them.

Objectives

  1. Define a more complex AgentState containing multiple fields.
  2. Create a processing node that performs operations on list data and string data.
  3. Build and compile a LangGraph with multiple fields in its state.
  4. Invoke the graph with structured inputs and retrieve the computed result.

Graph II: Multiple Inputs Graph

Goal

Build a simple graph that takes a list of integers and a name as input, computes the sum of the integers, and returns a personalized message displaying the sum.

Sample Input

Sample Output

Plan

  1. Define the state schema (AgentState) containing values (list of integers), name (str), and result (str).
  2. Define the node function process_values that extracts the fields, calculates the sum, and formats the personalized greeting.
  3. Build and compile the graph.
  4. Invoke the graph with multiple input values.

Code Implementation

1. Define the State Schema

We define a schema AgentState with a list of integers, a name string, and a result string:

2. Define the Node Function

The node function receives the state, extracts the name and list of integers, computes the sum, and formats a greeting:

3. Create the Graph

We define the graph structure, add the processor node, and set entry/finish points:

4. Invoke the Graph

Pass multiple fields inside the initial dictionary:

Exercise: Calculator Agent 🏆

Goal

Create a graph where you pass in a list of integers, a name, and an operation (addition "+", or multiplication "*"). Perform the corresponding operation in the node.

Sample Input

Sample Output

Plan

  1. Define the state schema with name, values, operation, and result keys.
  2. Write a node function that evaluates the operation field and calculates either the sum or product of the list.
  3. Build, compile, and run the graph.

Practice & Exercises

To reinforce what you’ve learned in this section (handling multiple input fields, processing list and string data), practice with the interactive notebook:

Practice & Exercises

Practice managing multiple-input states, writing processing nodes, and building a calculator agent.💻 VS Code | 🚀 Colab | 📥 Download