# Subject: operator.add reducer causes exponential duplication in Annotated List state fields when tools update state

**URL:** <https://forum.langchain.com/t/subject-operator-add-reducer-causes-exponential-duplication-in-annotated-list-state-fields-when-tools-update-state/1546>\
**Category:** Agent Server\
**Created:** [September 16, 2025, 11:46am UTC](https://forum.langchain.com/t/subject-operator-add-reducer-causes-exponential-duplication-in-annotated-list-state-fields-when-tools-update-state/1546 "2025-09-16T11:46:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trapti](https://avatars.discourse-cdn.com/v4/letter/t/a4c791/32.png) [@Trapti](https://forum.langchain.com/u/Trapti)\
**Post date:** [September 16, 2025, 11:46am UTC](https://forum.langchain.com/t/subject-operator-add-reducer-causes-exponential-duplication-in-annotated-list-state-fields-when-tools-update-state/1546/1 "2025-09-16T11:46:33Z")

</div>

## **Issue Description:**

### **Problem:**

When using Annotated[List[Dict], operator.add] for state fields that are updated by tools via Command(update=…), the state accumulates exponentially instead of properly merging list items. Each tool update appears to wrap the previous state in a new array, causing nested concatenation rather than flat list merging.

### **Reproduction Steps:**

1. Define a state field: current\_context\_source: Annotated[List[Dict[str, str]], operator.add]

2. Create tools that update this field: Command(update={“current\_context\_source”: [{“id”: “A”, “value”: [“source1”]}]})

3. Run multiple queries through the graph

4. Observe exponential growth: [A] → [[A], B] → [[[A], B], C]

### **Expected Behavior:**

**python**

_**# Query 1: [{“id”: “A”, “value”: [“source1”]}]**_

_**# Query 2: [{“id”: “A”, “value”: [“source1”]}, {“id”: “B”, “value”: [“source2”]}]**_

_**# Query 3: [{“id”: “A”, “value”: [“source1”]}, {“id”: “B”, “value”: [“source2”]}, {“id”: “C”, “value”: [“source3”]}]**_

### **Actual Behavior:**

**python**

_**# Query 1: [{“id”: “A”, “value”: [“source1”]}]**_

_**# Query 2: [{“id”: “A”, “value”: [“source1”]}, {“id”: “A”, “value”: [“source1”]}, {“id”: “B”, “value”: [“source2”]}]**_

_**# Query 3: [{“id”: “A”, “value”: [“source1”]}, {“id”: “A”, “value”: [“source1”]}, {“id”: “B”, “value”: [“source2”]}, {“id”: “A”, “value”: [“source1”]}, {“id”: “A”, “value”: [“source1”]}, {“id”: “B”, “value”: [“source2”]}, {“id”: “C”, “value”: [“source3”]}]**_

### **Code Example:**

**python**

**class MyState(AgentState):**

```
**current_context_source: Annotated\[List\[Dict\[str, str\]\], operator.add\] = Field(*default_factory*=list)**

```

**@tool(“my\_tool”)**

**async def my\_tool(_query\_id_: str):**

```
***return***** Command(*update*={**

    **"current_context_source": \[{"id": query_id, "value": \["source"\]}\]**

**})**

```

## **Attempted Solutions (All Failed):**

### **1. Custom Lambda Reducer:**

**python**

**current\_context\_source: Annotated[List[Dict], lambda _x_, _y_: (x or []) + (y or [])]**

**Result:** Same behavior as operator.add - still creates nested arrays

### **2. Messages-Style Manual Concatenation:**

**python**

_ **# In tools** _

**existing\_sources = state.get(“current\_context\_source”, [])**

**“current\_context\_source”: existing\_sources + [{“id”: query\_id, “value”: sources}]**

**Result:** Failed because tools don’t have access to state parameter

### **3. Different Reducer Functions:**

- operator.setitem - Overwrites instead of merging

- operator.add - Same nested behavior

- Custom functions - All produce same nested concatenation

### **4. State Structure Changes:**

- Tried different Annotated syntax

- Attempted to flatten in custom reducers

- Modified update patterns

**All approaches failed because the issue appears to be in LangGraph’s internal handling of Command(update=…) with operator.add reducers.**

## **Root Cause Analysis:**

The issue seems to be that LangGraph internally wraps tool updates in arrays before applying the operator.add reducer, causing:

python

_# What we send: [{“id”: “A”, “value”: [“source1”]}]_

_# What LangGraph does internally: existing + [new\_array]_

_# Instead of: existing + new\_items_

## **Workaround:**

Currently using response-level deduplication:

python

_# Deduplicate at response creation time_

unique\_sources =

seen\_ids = set()

_for_ source _in_ raw\_sources:

```
*if* source\["id"\] not in seen_ids:

    unique_sources.append(source)

    seen_ids.add(source\["id"\])

```

## **Request:**

Please investigate the operator.add reducer behavior with tool updates and provide a proper solution for flat list merging in state management. Also explain why this does not happen with the in built messafes state in AgentState ?

---

<div class="post-metadata">

**Author:** ![wfh](https://yyz1.discourse-cdn.com/flex007/user_avatar/forum.langchain.com/wfh/32/33_2.png) [@wfh](https://forum.langchain.com/u/wfh)\
**Post date:** [September 16, 2025, 11:49am UTC](https://forum.langchain.com/t/subject-operator-add-reducer-causes-exponential-duplication-in-annotated-list-state-fields-when-tools-update-state/1546/2 "2025-09-16T11:49:21Z")

</div>

operator.add is just the python stdlib “+” sign. If you want deduplication then the function should be something different than naive +.

You can check out the add\_messages reducer as an example.

---

<div class="post-metadata">

**Author:** ![Trapti](https://avatars.discourse-cdn.com/v4/letter/t/a4c791/32.png) [@Trapti](https://forum.langchain.com/u/Trapti)\
**Post date:** [September 16, 2025, 12:29pm UTC](https://forum.langchain.com/t/subject-operator-add-reducer-causes-exponential-duplication-in-annotated-list-state-fields-when-tools-update-state/1546/3 "2025-09-16T12:29:16Z")

</div>

thank you for the reminder ! I wrote the custom reducer and fixed the issue.
