How to remove session memory via python api #3060
-
Hi, I am currently using Langflow and would like to know how to remove session memory via the Python API. I have searched for similar discussions and tried the following code to delete a session:
However, I am receiving the following response:
It seems like the DELETE method is not allowed for the session endpoint. Could you please guide me on the correct approach to remove session memory via the API? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 10 replies
-
Hi @LeeMoofon0222! I'm here to help you out. Let's work together to tackle this issue. To remove session memory via the Langflow Python API, you can use the from langflow.services.session.service import SessionService
from langflow.services.cache.base import CacheService
# Create an instance of the cache service
cache_service = CacheService()
# Create an instance of the session service
session_service = SessionService(cache_service)
# Specify the session ID you want to clear
session_id = "your_session_id_here"
# Call the clear_session method to clear the session memory
await session_service.clear_session(session_id)
# Optionally, you can check if the session has been cleared
cleared_data = cache_service.get(session_id)
print(cleared_data) # Should print None if the session was successfully cleared Alternatively, you can use the import requests
# Define the session ID you want to clear
session_id = "your_session_id_here"
# Define the URL for the delete_messages_session endpoint
url = f"http://your-langflow-api-url/monitor/messages/session/{session_id}"
# Make the DELETE request to clear the session messages
response = requests.delete(url)
# Check the response status
if response.status_code == 204:
print("Session messages successfully cleared.")
else:
print(f"Failed to clear session messages: {response.status_code} - {response.text}") This code will clear the session memory for the specified session ID using the |
Beta Was this translation helpful? Give feedback.
-
It works! Thank you. |
Beta Was this translation helpful? Give feedback.
Hi @LeeMoofon0222! I'm here to help you out. Let's work together to tackle this issue.
To remove session memory via the Langflow Python API, you can use the
SessionService
class provided in theservice.py
file. This class includes a method to clear the session for a given session ID. Here is how you can use it: