> ## Documentation Index
> Fetch the complete documentation index at: https://genai.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Graph II: Multiple Inputs Graph

> Learn how to manage a state structure with multiple inputs in LangGraph

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

```python theme={null}
{"values": [1, 2, 3, 4], "name": "Sanjay"}
```

#### Sample Output

```python theme={null}
{"values": [1, 2, 3, 4], "name": "Sanjay", "result": "Hi there Sanjay! Your sum = 10"}
```

#### 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:

```python theme={null}
from typing import TypedDict

class AgentState(TypedDict):
    values: list[int]
    name: str
    result: str
```

#### 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:

```python theme={null}
def process_values(state: AgentState) -> AgentState:
    state["result"] = f"Hi there {state['name']}! Your sum = {sum(state['values'])}"
    return state
```

#### 3. Create the Graph

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

```python theme={null}
from langgraph.graph import StateGraph

graph = StateGraph(AgentState)
graph.add_node("processor", process_values)

graph.set_entry_point("processor")
graph.set_finish_point("processor")

app = graph.compile()
```

#### 4. Invoke the Graph

Pass multiple fields inside the initial dictionary:

```python theme={null}
answers = app.invoke({"values": [1, 2, 3, 4], "name": "Sanjay"})
print(answers["result"])
# Output: Hi there Sanjay! Your sum = 10
```

## 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

```python theme={null}
{
    "name": "Vikram",
    "values": [1, 2, 3, 4],
    "operation": "*"
}
```

#### Sample Output

```python theme={null}
"Hi Vikram, your answer is: 24"
```

#### 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.

<Accordion title="Solution">
  ```python theme={null}
  from typing import TypedDict
  from langgraph.graph import StateGraph

  # 1. State Schema
  class AgentState(TypedDict):
      name: str
      values: list[int]
      operation: str
      result: str

  # 2. Node Function
  def calculate_node(state: AgentState) -> AgentState:
      name = state["name"]
      values = state["values"]
      op = state["operation"]
      
      if op == "+":
          ans = sum(values)
      elif op == "*":
          ans = 1
          for v in values:
              ans *= v
      else:
          ans = 0
          
      state["result"] = f"Hi {name}, your answer is: {ans}"
      return state

  # 3. Create Graph
  graph = StateGraph(AgentState)
  graph.add_node("calculator", calculate_node)
  graph.set_entry_point("calculator")
  graph.set_finish_point("calculator")

  # 4. Compile and Run
  app = graph.compile()
  output = app.invoke({
      "name": "Vikram",
      "values": [1, 2, 3, 4],
      "operation": "*"
  })
  print(output["result"])
  ```
</Accordion>

## 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:

<CardGroup cols={1}>
  <Card title="Practice & Exercises" icon="laptop-code">
    Practice managing multiple-input states, writing processing nodes, and building a calculator agent.

    [💻 VS Code](vscode://file/Users/sivaprasad/Downloads/GenAI%20With%20Python/public/notebooks/langgraph/Graph2_Multiple_Inputs.ipynb) | [🚀 Colab](https://colab.research.google.com/github/prasad230776/genai-course/blob/master/public/notebooks/langgraph/Graph2_Multiple_Inputs.ipynb) | <a href="/public/notebooks/langgraph/Graph2_Multiple_Inputs.ipynb" download>📥 Download</a>
  </Card>
</CardGroup>
