Class Session<S>

java.lang.Object
com.google.genkit.ai.session.Session<S>
Type Parameters:
S - the type of the custom session state

public class Session<S> extends Object
Session represents a stateful chat session that persists conversation history and custom state across multiple interactions.

Sessions provide:

  • Persistent conversation threads
  • Custom state management
  • Multiple named chat threads within a session
  • Automatic history management

Example usage:


 // Create a session with initial state
 Session<MyState> session = genkit
 		.createSession(SessionOptions.<MyState>builder().initialState(new MyState("John")).build());

 // Create a chat and interact
 Chat chat = session.chat();
 ModelResponse response = chat.send("Hello!");

 // Access session state
 MyState state = session.getState();
 
  • Field Details

    • DEFAULT_THREAD

      public static final String DEFAULT_THREAD
      Default thread name for chat conversations.
      See Also:
  • Method Details

    • getId

      public String getId()
      Gets the session ID.
      Returns:
      the unique session identifier
    • getState

      public S getState()
      Gets the current session state.
      Returns:
      the session state, or null if not set
    • updateState

      public CompletableFuture<Void> updateState(S state)
      Updates the session state and persists it.
      Parameters:
      state - the new state
      Returns:
      a CompletableFuture that completes when the state is saved
    • getMessages

      public List<Message> getMessages(String threadName)
      Gets the message history for a thread.
      Parameters:
      threadName - the thread name
      Returns:
      the list of messages in the thread
    • getMessages

      public List<Message> getMessages()
      Gets the message history for the default thread.
      Returns:
      the list of messages
    • updateMessages

      public CompletableFuture<Void> updateMessages(String threadName, List<Message> messages)
      Updates the messages for a thread and persists them.
      Parameters:
      threadName - the thread name
      messages - the messages to save
      Returns:
      a CompletableFuture that completes when saved
    • chat

      public Chat<S> chat()
      Creates a new Chat instance for the default thread.
      Returns:
      a new Chat instance
    • chat

      public Chat<S> chat(ChatOptions<S> options)
      Creates a new Chat instance with options.
      Parameters:
      options - the chat options
      Returns:
      a new Chat instance
    • chat

      public Chat<S> chat(String threadName)
      Creates a new Chat instance for a specific thread.
      Parameters:
      threadName - the thread name
      Returns:
      a new Chat instance
    • chat

      public Chat<S> chat(String threadName, ChatOptions<S> options)
      Creates a new Chat instance for a specific thread with options.
      Parameters:
      threadName - the thread name
      options - the chat options
      Returns:
      a new Chat instance
    • getStore

      public SessionStore<S> getStore()
      Gets the session store.
      Returns:
      the session store
    • getRegistry

      public Registry getRegistry()
      Gets the registry.
      Returns:
      the registry
    • getAgentRegistry

      public Map<String,Agent> getAgentRegistry()
      Gets the agent registry for multi-agent handoffs.
      Returns:
      the agent registry, or null if not set
    • getSessionData

      public SessionData<S> getSessionData()
      Gets the session data.
      Returns:
      the session data
    • toJSON

      public SessionData<S> toJSON()
      Serializes the session to JSON-compatible data.
      Returns:
      the session data
    • create

      public static <S> Session<S> create(Registry registry, SessionOptions<S> options)
      Creates a new Session with a generated ID.
      Type Parameters:
      S - the state type
      Parameters:
      registry - the Genkit registry
      options - the session options
      Returns:
      a new Session
    • create

      public static <S> Session<S> create(Registry registry, SessionOptions<S> options, Map<String,Agent> agentRegistry)
      Creates a new Session with a generated ID and agent registry.
      Type Parameters:
      S - the state type
      Parameters:
      registry - the Genkit registry
      options - the session options
      agentRegistry - the agent registry for multi-agent handoffs (may be null)
      Returns:
      a new Session
    • load

      public static <S> CompletableFuture<Session<S>> load(Registry registry, String sessionId, SessionOptions<S> options)
      Loads an existing session from a store.
      Type Parameters:
      S - the state type
      Parameters:
      registry - the Genkit registry
      sessionId - the session ID to load
      options - the session options (must include store)
      Returns:
      a CompletableFuture containing the loaded session, or null if not found
    • load

      public static <S> CompletableFuture<Session<S>> load(Registry registry, String sessionId, SessionOptions<S> options, Map<String,Agent> agentRegistry)
      Loads an existing session from a store with agent registry.
      Type Parameters:
      S - the state type
      Parameters:
      registry - the Genkit registry
      sessionId - the session ID to load
      options - the session options (must include store)
      agentRegistry - the agent registry for multi-agent handoffs (may be null)
      Returns:
      a CompletableFuture containing the loaded session, or null if not found