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
Tiny example
Section titled “Tiny example”import operatorfrom typing import Annotatedfrom 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'] |
Failure note
Section titled “Failure note”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.
Related
Section titled “Related”Footnotes
Section titled “Footnotes”-
LangChain, Use the graph API. ↩