Problem with add_messages, message didn't get merged #222

Closed
opened 2026-02-20 17:32:24 -05:00 by yindo · 3 comments
Owner

Originally created by @CalendulaED on GitHub (Sep 1, 2024).

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangGraph/LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangGraph/LangChain rather than my code.
  • I am sure this is better as an issue rather than a GitHub discussion, since this is a LangGraph bug and not a design question.

Example Code

from typing import Annotated

from typing_extensions import TypedDict

from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain_openai import ChatOpenAI

class State(TypedDict):
    # Messages have the type "list". The `add_messages` function
    # in the annotation defines how this state key should be updated
    # (in this case, it appends messages to the list, rather than overwriting them)
    messages: Annotated[list, add_messages]

def chatbot(state: State):
    model = ChatOpenAI(temperature=0.1, model_name="gpt-3.5-turbo")
    print(state)
    return {"messages": [model.invoke(state["messages"])]}

graph_builder = StateGraph(State)
graph_builder.add_node("chatbot", chatbot)
graph_builder.add_edge(START, "chatbot")
graph_builder.add_edge("chatbot", END)
graph = graph_builder.compile()

while True:
    user_input = input("User: ")
    if user_input.lower() in ["quit", "exit", "q"]:
        print("Goodbye!")
        break
    for event in graph.stream({"messages": ("user", user_input)}):
        for value in event.values():
            print("Assistant:", value["messages"][-1].content)

Error Message and Stack Trace (if applicable)

python my_agent/agent.py
User: Do you know Calculus?
{'messages': [HumanMessage(content='Do you know Calculus?', id='05425ee6-2c31-4c0b-8843-2ca88596e420')]}
Assistant: Yes, I am familiar with Calculus. Calculus is a branch of mathematics that deals with rates of change and accumulation of quantities. It includes topics such as differentiation, integration, limits, and infinite series.
User: Can you make an example?
{'messages': [HumanMessage(content='Can you make an example?', id='e7aee16b-f51d-4dd1-b7d3-ee606278369d')]}
Assistant: Sure! Here is an example:

Sentence: "The cat chased the mouse around the house."

Example: The cat, a sleek and agile tabby with bright green eyes, darted after the small grey mouse as it scurried through the various rooms of the house, knocking over vases and skidding around corners in a frantic attempt to escape.

Description

I follow the first example in quickstart here: https://langchain-ai.github.io/langgraph/tutorials/introduction/#setup
and I try to print the state, and I realized that the message is being replaced rather than appended.
But on the website, it states: "The messages key is annotated with the add_messages reducer function, which tells LangGraph to append new messages to the existing list, rather than overwriting it."

System Info

Name: langgraph
Version: 0.2.15
Summary: Building stateful, multi-actor applications with LLMs
Home-page: https://www.github.com/langchain-ai/langgraph
Author:
Author-email:
License: MIT
Location: /Users/yuxuanwu/opt/anaconda3/envs/langchain/lib/python3.11/site-packages
Requires: langchain-core, langgraph-checkpoint
Required-by:

Originally created by @CalendulaED on GitHub (Sep 1, 2024). ### Checked other resources - [X] I added a very descriptive title to this issue. - [X] I searched the [LangGraph](https://langchain-ai.github.io/langgraph/)/LangChain documentation with the integrated search. - [X] I used the GitHub search to find a similar question and didn't find it. - [X] I am sure that this is a bug in LangGraph/LangChain rather than my code. - [X] I am sure this is better as an issue [rather than a GitHub discussion](https://github.com/langchain-ai/langgraph/discussions/new/choose), since this is a LangGraph bug and not a design question. ### Example Code ```python from typing import Annotated from typing_extensions import TypedDict from langgraph.graph import StateGraph, START, END from langgraph.graph.message import add_messages from langchain_openai import ChatOpenAI class State(TypedDict): # Messages have the type "list". The `add_messages` function # in the annotation defines how this state key should be updated # (in this case, it appends messages to the list, rather than overwriting them) messages: Annotated[list, add_messages] def chatbot(state: State): model = ChatOpenAI(temperature=0.1, model_name="gpt-3.5-turbo") print(state) return {"messages": [model.invoke(state["messages"])]} graph_builder = StateGraph(State) graph_builder.add_node("chatbot", chatbot) graph_builder.add_edge(START, "chatbot") graph_builder.add_edge("chatbot", END) graph = graph_builder.compile() while True: user_input = input("User: ") if user_input.lower() in ["quit", "exit", "q"]: print("Goodbye!") break for event in graph.stream({"messages": ("user", user_input)}): for value in event.values(): print("Assistant:", value["messages"][-1].content) ``` ### Error Message and Stack Trace (if applicable) ```shell python my_agent/agent.py User: Do you know Calculus? {'messages': [HumanMessage(content='Do you know Calculus?', id='05425ee6-2c31-4c0b-8843-2ca88596e420')]} Assistant: Yes, I am familiar with Calculus. Calculus is a branch of mathematics that deals with rates of change and accumulation of quantities. It includes topics such as differentiation, integration, limits, and infinite series. User: Can you make an example? {'messages': [HumanMessage(content='Can you make an example?', id='e7aee16b-f51d-4dd1-b7d3-ee606278369d')]} Assistant: Sure! Here is an example: Sentence: "The cat chased the mouse around the house." Example: The cat, a sleek and agile tabby with bright green eyes, darted after the small grey mouse as it scurried through the various rooms of the house, knocking over vases and skidding around corners in a frantic attempt to escape. ``` ### Description I follow the first example in quickstart here: https://langchain-ai.github.io/langgraph/tutorials/introduction/#setup and I try to print the state, and I realized that the message is being replaced rather than appended. But on the website, it states: "The messages key is annotated with the [add_messages](https://langchain-ai.github.io/langgraph/reference/graphs/?h=add+messages#add_messages) reducer function, which tells LangGraph to append new messages to the existing list, rather than overwriting it." ### System Info Name: langgraph Version: 0.2.15 Summary: Building stateful, multi-actor applications with LLMs Home-page: https://www.github.com/langchain-ai/langgraph Author: Author-email: License: MIT Location: /Users/yuxuanwu/opt/anaconda3/envs/langchain/lib/python3.11/site-packages Requires: langchain-core, langgraph-checkpoint Required-by:
yindo closed this issue 2026-02-20 17:32:27 -05:00
Author
Owner

@gbaian10 commented on GitHub (Sep 1, 2024):

Insert a checkpoint,
Otherwise, your graph will restart after each run.

from langgraph.checkpoint.memory import MemorySaver

# ...

graph = graph_builder.compile(checkpointer=MemorySaver())
thread_config = {"configurable": {"thread_id": "1"}}

# ...

for event in graph.stream({"messages": ("user", user_input)}, thread_config):

# ...
@gbaian10 commented on GitHub (Sep 1, 2024): Insert a `checkpoint`, Otherwise, your graph will restart after each run. ```py from langgraph.checkpoint.memory import MemorySaver # ... graph = graph_builder.compile(checkpointer=MemorySaver()) thread_config = {"configurable": {"thread_id": "1"}} # ... for event in graph.stream({"messages": ("user", user_input)}, thread_config): # ... ```
Author
Owner

@CalendulaED commented on GitHub (Sep 1, 2024):

Insert a checkpoint, Otherwise, your graph will restart after each run.

from langgraph.checkpoint.memory import MemorySaver

# ...

graph = graph_builder.compile(checkpointer=MemorySaver())
thread_config = {"configurable": {"thread_id": "1"}}

# ...

for event in graph.stream({"messages": ("user", user_input)}, thread_config):

# ...

Thank you so much!

@CalendulaED commented on GitHub (Sep 1, 2024): > Insert a `checkpoint`, Otherwise, your graph will restart after each run. > > ```python > from langgraph.checkpoint.memory import MemorySaver > > # ... > > graph = graph_builder.compile(checkpointer=MemorySaver()) > thread_config = {"configurable": {"thread_id": "1"}} > > # ... > > for event in graph.stream({"messages": ("user", user_input)}, thread_config): > > # ... > ``` Thank you so much!
Author
Owner

@2BitSalute commented on GitHub (Feb 1, 2025):

Thank you for asking the question. I also had the same one while reading the quickstart.

The quickstart says: "This bot can engage in basic conversation by taking user input and generating responses using an LLM."

This is really misleading because it makes it sound like you can actually have a conversation taking turns, but every turn, the LLM only gets the latest message from the user.

The thread thing is required, otherwise you get the following error from graph.stream:

"Checkpointer requires one or more of the following 'configurable' keys: ['thread_id', 'checkpoint_ns', 'checkpoint_id']"

@2BitSalute commented on GitHub (Feb 1, 2025): Thank you for asking the question. I also had the same one while reading the [quickstart](https://langchain-ai.github.io/langgraph/tutorials/introduction/#part-1-build-a-basic-chatbot). The quickstart says: "This bot can engage in basic conversation by taking user input and generating responses using an LLM." This is really misleading because it makes it sound like you can actually have a conversation taking turns, but every turn, the LLM only gets the latest message from the user. The thread thing is required, otherwise you get the following error from `graph.stream`: > "Checkpointer requires one or more of the following 'configurable' keys: ['thread_id', 'checkpoint_ns', 'checkpoint_id']"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#222