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 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.