Skip to content

Reducers and Annotated

A reducer decides how an old state value and a new update become one value. Without a reducer, a new update replaces the old value.1

import operator
from typing import Annotated
from typing_extensions import TypedDict
class ResearchState(TypedDict):
question: str
findings: Annotated[list[str], operator.add]
def search_policy(state: ResearchState) -> dict:
return {"findings": ["Refund window is 30 days"]}

Annotated[list[str], operator.add] means “append this update to the existing list.”

Update No reducer operator.add reducer
old ['A'], new ['B'] ['B'] ['A', 'B']

Parallel nodes that update the same key need a merge rule. Choose a deterministic reducer and do not assume parallel results arrive in a business-significant order.

  1. LangChain, Use the graph API.