DOC: filter_message code is wrong #185

Closed
opened 2026-02-20 17:30:55 -05:00 by yindo · 5 comments
Owner

Originally created by @streamnsight on GitHub (Aug 12, 2024).

Issue with current documentation:

simple issue, but worth pointing out

the code here:
https://langchain-ai.github.io/langgraph/how-tos/memory/manage-conversation-history/#filtering-messages

    # This is very simple helper function which only ever uses the last two messages
    return messages[-1:]

actually returns the last message, not the last 2.
last 2 would be messages[-2:]

Idea or request for content:

No response

Originally created by @streamnsight on GitHub (Aug 12, 2024). ### Issue with current documentation: simple issue, but worth pointing out the code here: https://langchain-ai.github.io/langgraph/how-tos/memory/manage-conversation-history/#filtering-messages ```def filter_messages(messages: list): # This is very simple helper function which only ever uses the last two messages return messages[-1:] ``` actually returns the last message, not the last 2. last 2 would be `messages[-2:]` ### Idea or request for content: _No response_
yindo closed this issue 2026-02-20 17:30:55 -05:00
Author
Owner

@vbarda commented on GitHub (Aug 22, 2024):

this actually needs to be messages[-1:]

@vbarda commented on GitHub (Aug 22, 2024): this actually needs to be `messages[-1:]`
Author
Owner

@streamnsight commented on GitHub (Aug 22, 2024):

@vbarda

messages[-1:] gives the last message
last 2 is messages[-2:]

>>> a = [1,2,3,4,5]
>>> a[-2:]
[4, 5]
>>> a[-1:]
[5]

so if the function is to return last 2 as described, then it is messages[-2:]

@streamnsight commented on GitHub (Aug 22, 2024): @vbarda messages[-1:] gives the last message last 2 is messages[-2:] ``` >>> a = [1,2,3,4,5] >>> a[-2:] [4, 5] >>> a[-1:] [5] ``` so if the function is to return last 2 as described, then it is messages[-2:]
Author
Owner

@vbarda commented on GitHub (Aug 22, 2024):

the description was wrong

@vbarda commented on GitHub (Aug 22, 2024): the description was wrong
Author
Owner

@vbarda commented on GitHub (Aug 22, 2024):

see https://github.com/langchain-ai/langgraph/pull/1438

@vbarda commented on GitHub (Aug 22, 2024): see https://github.com/langchain-ai/langgraph/pull/1438
Author
Owner

@gbaian10 commented on GitHub (Aug 23, 2024):

I previously thought that this tutorial example was meant to include only the first two sentences of the historical dialogue record, not all the dialogue, until I saw that the final run included this:

# This will now not remember the previous messages
# (because we set `messages[-1:]` in the filter messages argument)
input_message = HumanMessage(content="what's my name?")
for event in app.stream({"messages": [input_message]}, config, stream_mode="values"):
    event["messages"][-1].pretty_print()

In that case, indeed, just updating the comments within the filter is enough.

@gbaian10 commented on GitHub (Aug 23, 2024): I previously thought that this tutorial example was meant to include only the first two sentences of the historical dialogue record, not all the dialogue, until I saw that the final run included this: ```py # This will now not remember the previous messages # (because we set `messages[-1:]` in the filter messages argument) input_message = HumanMessage(content="what's my name?") for event in app.stream({"messages": [input_message]}, config, stream_mode="values"): event["messages"][-1].pretty_print() ``` In that case, indeed, just updating the comments within the filter is enough.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#185