Class Chat<S>

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

public class Chat<S> extends Object
Chat represents a conversation within a session thread.

Chat provides a simple interface for multi-turn conversations with automatic history management. Messages are persisted to the session store after each interaction.

Example usage:


 // Simple chat
 Chat<MyState> chat = session.chat();
 ModelResponse response = chat.send("Hello!");
 
 // Chat with system prompt
 Chat<MyState> chat = session
 		.chat(ChatOptions.<MyState>builder().model("openai/gpt-4o").system("You are a helpful assistant.").build());
 
 // Multi-turn conversation
 chat.send("What is the capital of France?");
 chat.send("And what about Germany?"); // Context is preserved
 
  • Method Details

    • send

      public ModelResponse send(String text) throws GenkitException
      Sends a message and gets a response.

      This method:

      1. Adds the user message to history
      2. Builds a request with all conversation history
      3. Sends to the model and gets a response
      4. Adds the model response to history
      5. Persists the updated history
      Parameters:
      text - the user message
      Returns:
      the model response
      Throws:
      GenkitException - if generation fails
    • send

      public ModelResponse send(Message message) throws GenkitException
      Sends a message and gets a response.
      Parameters:
      message - the message to send
      Returns:
      the model response
      Throws:
      GenkitException - if generation fails
    • send

      public ModelResponse send(String text, Chat.SendOptions sendOptions) throws GenkitException
      Sends a message with send options and gets a response.
      Parameters:
      text - the user message
      sendOptions - additional options for this send
      Returns:
      the model response
      Throws:
      GenkitException - if generation fails
    • send

      public ModelResponse send(Message message, Chat.SendOptions sendOptions) throws GenkitException
      Sends a message with send options and gets a response.
      Parameters:
      message - the message to send
      sendOptions - additional options for this send
      Returns:
      the model response
      Throws:
      GenkitException - if generation fails
    • sendStream

      public ModelResponse sendStream(String text, Consumer<ModelResponseChunk> streamCallback) throws GenkitException
      Sends a message with streaming response.
      Parameters:
      text - the user message
      streamCallback - callback for each response chunk
      Returns:
      the final aggregated response
      Throws:
      GenkitException - if generation fails
    • sendStream

      public ModelResponse sendStream(Message message, Chat.SendOptions sendOptions, Consumer<ModelResponseChunk> streamCallback) throws GenkitException
      Sends a message with streaming response.
      Parameters:
      message - the message to send
      sendOptions - additional options for this send
      streamCallback - callback for each response chunk
      Returns:
      the final aggregated response
      Throws:
      GenkitException - if generation fails
    • getHistory

      public List<Message> getHistory()
      Gets the current conversation history.
      Returns:
      a copy of the message history
    • getSession

      public Session<S> getSession()
      Gets the session.
      Returns:
      the parent session
    • getThreadName

      public String getThreadName()
      Gets the thread name.
      Returns:
      the thread name
    • getPendingInterrupts

      public List<InterruptRequest> getPendingInterrupts()
      Gets the pending interrupt requests from the last send.

      If the last send(java.lang.String) call returned with interrupts, this method returns the list of pending interrupts that need to be resolved before continuing.

      Returns:
      the list of pending interrupt requests (empty if none)
    • hasPendingInterrupts

      public boolean hasPendingInterrupts()
      Checks if there are pending interrupts.
      Returns:
      true if there are pending interrupts
    • getCurrentAgentName

      public String getCurrentAgentName()
      Gets the current agent name.

      Returns null if no agent handoff has occurred, otherwise returns the name of the agent that the conversation was most recently handed off to.

      Returns:
      the current agent name, or null if no handoff has occurred