[GH-ISSUE #3894] [langchain]: 'delete_old_messages' The deletion effect was not effectively demonstrated. #2769

Closed
opened 2026-06-05 17:26:42 -04:00 by yindo · 1 comment
Owner

Originally created by @AA-G-AA on GitHub (May 7, 2026).
Original GitHub issue: https://github.com/langchain-ai/docs/issues/3894

Type of issue

issue / bug

Language

Python

Description

https://docs.langchain.com/oss/python/langchain/short-term-memory#delete-messages

Issue Description

The current delete_old_messages example in the official documentation fails to effectively demonstrate the deletion effect — the agent still remembers the user's name "Bob" after the earliest two messages are deleted.

Root Cause

In the original example, the AI's first response "Hi Bob! Nice to meet you..." repeats the user's name "Bob". After deleting both the user's first message and the AI's first response (messages[:2]), the name "Bob" remains in the AI's second response, allowing the agent to still "remember" the user's name.

This does not properly demonstrate the expected "information loss after deletion" behavior.

Suggested Improvement

Insert an unrelated conversation turn between the name introduction and the name recall query. This allows the earliest two messages (containing the name info) to be naturally deleted before the agent is asked to recall the name.

Improved code:

for event in agent.stream(
    {"messages": [{"role": "user", "content": "hi! I'm bob"}]},
    config,
    stream_mode="values",
):
    print([(message.type, message.content) for message in event["messages"]])

# ✅ Insert an unrelated turn
for event in agent.stream(
    {"messages": [{"role": "user", "content": "write a short poem about cats"}]},
    config,
    stream_mode="values",
):
    print([(message.type, message.content) for message in event["messages"]])

# Now the agent should NOT remember the name
for event in agent.stream(
    {"messages": [{"role": "user", "content": "what's my name?"}]},
    config,
    stream_mode="values",
):
    print([(message.type, message.content) for message in event["messages"]])

Actual output (proof of improvement):

text
[('human', "hi! I'm bob")]
[('human', "hi! I'm bob"), ('ai', 'Hi Bob! Nice to meet you...')]
[('human', "hi! I'm bob"), ('ai', 'Hi Bob!...'), ('human', 'write a short poem about cats')]
[('human', "hi! I'm bob"), ('ai', 'Hi Bob!...'), ('human', 'write a short poem about cats'), ('ai', 'There once was a cat...')]
[('human', 'write a short poem about cats'), ('ai', 'There once was a cat...')]
[('human', 'write a short poem about cats'), ('ai', 'There once was a cat...'), ('human', "what's my name?")]
[('human', 'write a short poem about cats'), ('ai', 'There once was a cat...'), ('human', "what's my name?"), ('ai', "I don't know your name yet.")]
[('human', "what's my name?"), ('ai', "I don't know your name yet.")]
After the improvement, the agent correctly responds that it doesn't know the user's name, effectively demonstrating the deletion effect.
Originally created by @AA-G-AA on GitHub (May 7, 2026). Original GitHub issue: https://github.com/langchain-ai/docs/issues/3894 ### Type of issue issue / bug ### Language Python ### Description https://docs.langchain.com/oss/python/langchain/short-term-memory#delete-messages ## Issue Description The current `delete_old_messages` example in the official documentation fails to effectively demonstrate the deletion effect — the agent still remembers the user's name "Bob" after the earliest two messages are deleted. ## Root Cause In the original example, the AI's first response `"Hi Bob! Nice to meet you..."` repeats the user's name "Bob". After deleting both the user's first message and the AI's first response (`messages[:2]`), the name "Bob" remains in the AI's second response, allowing the agent to still "remember" the user's name. This does not properly demonstrate the expected "information loss after deletion" behavior. ## Suggested Improvement Insert an unrelated conversation turn between the name introduction and the name recall query. This allows the earliest two messages (containing the name info) to be naturally deleted before the agent is asked to recall the name. **Improved code:** ```python for event in agent.stream( {"messages": [{"role": "user", "content": "hi! I'm bob"}]}, config, stream_mode="values", ): print([(message.type, message.content) for message in event["messages"]]) # ✅ Insert an unrelated turn for event in agent.stream( {"messages": [{"role": "user", "content": "write a short poem about cats"}]}, config, stream_mode="values", ): print([(message.type, message.content) for message in event["messages"]]) # Now the agent should NOT remember the name for event in agent.stream( {"messages": [{"role": "user", "content": "what's my name?"}]}, config, stream_mode="values", ): print([(message.type, message.content) for message in event["messages"]]) Actual output (proof of improvement): text [('human', "hi! I'm bob")] [('human', "hi! I'm bob"), ('ai', 'Hi Bob! Nice to meet you...')] [('human', "hi! I'm bob"), ('ai', 'Hi Bob!...'), ('human', 'write a short poem about cats')] [('human', "hi! I'm bob"), ('ai', 'Hi Bob!...'), ('human', 'write a short poem about cats'), ('ai', 'There once was a cat...')] [('human', 'write a short poem about cats'), ('ai', 'There once was a cat...')] [('human', 'write a short poem about cats'), ('ai', 'There once was a cat...'), ('human', "what's my name?")] [('human', 'write a short poem about cats'), ('ai', 'There once was a cat...'), ('human', "what's my name?"), ('ai', "I don't know your name yet.")] [('human', "what's my name?"), ('ai', "I don't know your name yet.")] After the improvement, the agent correctly responds that it doesn't know the user's name, effectively demonstrating the deletion effect.
yindo added the langchainexternal labels 2026-06-05 17:26:42 -04:00
yindo closed this issue 2026-06-05 17:26:43 -04:00
Author
Owner

@uditstocks commented on GitHub (May 14, 2026):

I'd like to work on this!
I'll add the unrelated conversation turn to properly demonstrate the
deletion effect. Will submit a PR shortly.

<!-- gh-comment-id:4453068284 --> @uditstocks commented on GitHub (May 14, 2026): I'd like to work on this! I'll add the unrelated conversation turn to properly demonstrate the deletion effect. Will submit a PR shortly.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/docs#2769