Continuing a Chat Session#
Chat sessions in DecisionAI are persistent. You can stop interacting with a session and resume it later, preserving the state (constraints, variables, objective, etc.) you had previously established.
The primary way to continue a session is using the DecisionAI client and the session ID. You can also recreate a session from a saved state object, but this requires using the ChatSession class directly.
Continuing with Session ID (Recommended)#
This is the standard approach using the DecisionAI client.
If you know the chat_session_id of a previous session, you can reconnect to it using client.continue_chat_session().
If you don’t know the specific ID, you can list available sessions first:
client = DecisionAI() # Your initialized client
available_sessions = client.list_chat_sessions()
for session_state in available_sessions:
print(f"Session ID: {session_state.id}, Created: {session_state.created_at}")
# Choose an ID from the list
existing_session_id = "..."
Once you have the ID:
from decision_ai import DecisionAI
# Assuming 'client' is your initialized DecisionAI client
# Recreate the session using the client
chat_session = client.continue_chat_session(existing_session_id)
# Now you can interact with the session as usual
.. autolink-skip::
async with chat_session.connect() as chat:
# ... interact ...
solution = chat_session.solve()
# ...
When using client.continue_chat_session(), the client fetches the latest state associated with that ID from the server.
Continuing with Session State#
Alternatively, if you have previously fetched and stored the ChatSessionState object, you can initialize a session directly from that state. This avoids an initial fetch call to the server but requires you to manage the state object yourself and use the ChatSession class constructor directly, as there is no corresponding DecisionAI client method.
from decision_ai import DecisionAI
from decision_ai.client.chat_session import ChatSession
from decision_ai.client.schemas.chat_session import ChatSessionState
# Assume you have a previously saved ChatSessionState object
saved_state: ChatSessionState = ... # Your saved state instance
# Initialize client (needed for the request_handler)
client = DecisionAI() # Or however your client is initialized
# Provide input data (can be the same or new)
current_input_data = ... # Your InputData instance
# Recreate the session directly from the state object using ChatSession constructor
chat_session = ChatSession.from_state(
request_handler=client.request_handler, # Pass the handler from the client
chat_session_state=saved_state,
opt_input_data_class_name=current_input_data
)
# Interact with the session
# ...
Similar to continuing by ID, if the provided opt_input_data differs from the data associated with the saved_state, constraint compatibility checks will be performed.
Choosing the Right Method#
Use `client.continue_chat_session()` when you only have the session ID and want the latest state from the server. This is the recommended method.
Use `ChatSession.from_state()` if you have already fetched the state and want to potentially avoid an extra API call, or if you need to initialize the session with a specific historical state you’ve saved. Remember this requires using the ChatSession class directly.