Message-state reduction
Define a graph state field containing conversation history and merge node outputs into it.
AI Agents · Function
addMessages is a LangGraph reducer that combines an existing message list with incoming messages. It supports standard messages and special modifiers such as RemoveMessage.
addMessages is a prebuilt reducer for combining LangGraph message-state updates. It appends new messages, handles updates to existing messages, supports RemoveMessage instances, and returns LangChain message objects.
addMessages(left: Messages, right: Messages): BaseMessage<MessageStructure<MessageToolSet>, MessageType>[]| Name | Type | Description |
|---|---|---|
left | Messages | The existing messages to merge. |
right | Messages | The new messages or message modifiers to merge. |
BaseMessage<MessageStructure<MessageToolSet>, MessageType>[]
import { addMessages } from "@langchain/langgraph";
import { AIMessage, HumanMessage } from "@langchain/core/messages";
const current = [new HumanMessage({ content: "Hello", id: "1" })];
const updated = addMessages(
current,
[new AIMessage({ content: "Hi there!", id: "2" })],
);
console.log(updated.map((message) => message.content));The reducer merges an existing message list with new messages and returns the combined list.
Define a graph state field containing conversation history and merge node outputs into it.
Add newly generated human or AI messages without manually concatenating arrays.
Apply message operations such as RemoveMessage while reducing state updates.
MessagesRemoveMessageBaseMessageMessageStructureMessageToolSet