[Langgraph] Remove a message from the graph state #22632
Replies: 4 comments 2 replies
-
Hello @HGInfoNancy! 👋 I'm here to help you with any bugs, questions, or contributions you have. Let's tackle this issue together! To ensure that the state update persists across nodes in a LangChain graph state when you want to delete a message from the state, you can use the Here is the relevant code for the def clear(self) -> None:
"""Clear session memory from Neo4j"""
query = (
f"MATCH (s:`{self._node_label}`)-[:LAST_MESSAGE]->(last_message) "
"WHERE s.id = $session_id MATCH p=(last_message)<-[:NEXT]-() "
"WITH p, length(p) AS length ORDER BY length DESC LIMIT 1 "
"UNWIND nodes(p) as node DETACH DELETE node;"
)
self._driver.execute_query(query, {"session_id": self._session_id}).summary This method constructs a Cypher query to match the session node and its associated messages, then deletes them. This ensures that the state update (i.e., deletion of messages) is persisted across nodes [1]. If you are not using Neo4j, you might need to implement a similar mechanism in your storage backend to ensure that the state changes are persisted correctly across nodes. |
Beta Was this translation helpful? Give feedback.
-
So nobody cares? |
Beta Was this translation helpful? Give feedback.
-
According to this doc page, you can use Say your class is
(@HGInfoNancy idk if While you can add messages like this:
you can remove messages the same way, identifying the message you want to change to the
|
Beta Was this translation helpful? Give feedback.
-
A minor change in your code can solve this problem. While defining the function Here is the correct approach:
This would remove the last message, but doesn't reset the state completely. I hope this can help... |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
Hi, I want to forget/delete some messages previously stored in my graph state.
In my case I have "messages" and I want in another node delete this message.
I tried something like :
def function_node2(state):
state = state["messages"][:-1]
The state is now updated within my function, but as soon as I went into another node (let's say node3) it seems load the state from another way and it rolls back to the previous state
Can someone help me? Thanks
System Info
Beta Was this translation helpful? Give feedback.
All reactions