Replies: 6 comments 4 replies
-
Hey @yhygta, back for more LangChain adventures, I see! 🚀 Based on the context provided, it seems like you're looking to clear the state in LangGraph. However, the term "LangGraph" is not explicitly defined in the LangChain framework. It's inferred that you might be referring to the If you're referring to the If you're referring to the However, if you're looking to clear the state of a conversation, you can do so by calling the memory.chat_memory.clear() This line of code effectively clears the state by removing all messages from the If you're looking to clear the state of the message_history = SingleStoreDBChatMessageHistory(session_id="my-session", host="127.0.0.1", port=3306, user="user", password="password", database="db")
message_history.clear() This will clear all messages associated with the Please note that clearing the state can have several potential side effects or consequences such as loss of cached data, performance impact, cost implications, concurrency considerations, and keyword arguments impact. If none of these solutions apply to your situation, could you please provide more details about what you mean by "clearing the state in LangGraph"? This will help me provide a more accurate solution. Sources
|
Beta Was this translation helpful? Give feedback.
-
Ideally, we should call a function in the In the meantime, I call the def clear_memory(memory: BaseCheckpointSaver, thread_id: str) -> None:
checkpoint = empty_checkpoint()
memory.put(config={"configurable": {"thread_id": thread_id}}, checkpoint=checkpoint, metadata={})
# Calling the function
memory = SqliteSaver.from_conn_string("checkpoints.sqlite")
app = graph.compile(checkpointer=memory)
clear_memory(memory=memory, thread_id="123456") |
Beta Was this translation helpful? Give feedback.
-
It there a way to clear one variable of the state ..? Rather reset the whole state |
Beta Was this translation helpful? Give feedback.
-
Hello, any updates on this matter? |
Beta Was this translation helpful? Give feedback.
-
Here's my current implementation of from langgraph.checkpoint.base import BaseCheckpointSaver
def clear_memory(memory: BaseCheckpointSaver, thread_id: str) -> None:
""" Clear the memory for a given thread_id. """
try:
# If it's an InMemorySaver (which MemorySaver is an alias for),
# we can directly clear the storage and writes
if hasattr(memory, 'storage') and hasattr(memory, 'writes'):
# Clear all checkpoints for this thread_id (all namespaces)
memory.storage.pop(thread_id, None)
# Clear all writes for this thread_id (for all namespaces)
keys_to_remove = [key for key in memory.writes.keys() if key[0] == thread_id]
for key in keys_to_remove:
memory.writes.pop(key, None)
print(f"Memory cleared for thread_id: {thread_id}")
return
except Exception as e:
print(f"Error clearing InMemorySaver storage for thread_id {thread_id}: {e}") |
Beta Was this translation helpful? Give feedback.
-
I don't want to make noise in the thread, but thinking about a use case with a chatbot where the thread_id represents each conversation, it would be very useful to have a method that clears all data associated with a thread_id that hasn't had any data saved or accessed for a certain period of time. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
How to clear state in langgraph?
System Info
python 3.10
Beta Was this translation helpful? Give feedback.
All reactions