From 7c420f89cf55f118134f1b0677476a17709775a2 Mon Sep 17 00:00:00 2001 From: Tat Dat Duong Date: Sun, 1 Dec 2024 23:39:14 +0100 Subject: [PATCH] fix(docs): use `messagesStateReducer` instead of `concat` to match Python Related #695 --- README.md | 10 ++++++---- libs/langgraph/README.md | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bab31d65..d582fb2c 100644 --- a/README.md +++ b/README.md @@ -60,16 +60,18 @@ import { tool } from "@langchain/core/tools"; import { z } from "zod"; import { ChatAnthropic } from "@langchain/anthropic"; import { StateGraph } from "@langchain/langgraph"; -import { MemorySaver, Annotation } from "@langchain/langgraph"; +import { MemorySaver, Annotation, messagesStateReducer } from "@langchain/langgraph"; import { ToolNode } from "@langchain/langgraph/prebuilt"; // Define the graph state // See here for more info: https://langchain-ai.github.io/langgraphjs/how-tos/define-state/ const StateAnnotation = Annotation.Root({ messages: Annotation({ - reducer: (x, y) => x.concat(y), - }) -}) + // `messagesStateReducer` function defines how `messages` state key should be updated + // (in this case it appends new messages to the list and overwrites messages with the same ID) + reducer: messagesStateReducer, + }), +}); // Define the tools for the agent to use const weatherTool = tool(async ({ query }) => { diff --git a/libs/langgraph/README.md b/libs/langgraph/README.md index bab31d65..d582fb2c 100644 --- a/libs/langgraph/README.md +++ b/libs/langgraph/README.md @@ -60,16 +60,18 @@ import { tool } from "@langchain/core/tools"; import { z } from "zod"; import { ChatAnthropic } from "@langchain/anthropic"; import { StateGraph } from "@langchain/langgraph"; -import { MemorySaver, Annotation } from "@langchain/langgraph"; +import { MemorySaver, Annotation, messagesStateReducer } from "@langchain/langgraph"; import { ToolNode } from "@langchain/langgraph/prebuilt"; // Define the graph state // See here for more info: https://langchain-ai.github.io/langgraphjs/how-tos/define-state/ const StateAnnotation = Annotation.Root({ messages: Annotation({ - reducer: (x, y) => x.concat(y), - }) -}) + // `messagesStateReducer` function defines how `messages` state key should be updated + // (in this case it appends new messages to the list and overwrites messages with the same ID) + reducer: messagesStateReducer, + }), +}); // Define the tools for the agent to use const weatherTool = tool(async ({ query }) => {