Package com.google.genkit.ai.session
package com.google.genkit.ai.session
Provides session management for multi-turn agent conversations with
persistence.
The session package provides a stateful layer on top of Genkit's generation capabilities, enabling:
- Persistent conversation history across multiple interactions
- Custom session state management
- Multiple named conversation threads within a session
- Pluggable storage backends via
SessionStore
Key Components
Session- The main entry point for session managementChat- Manages conversations within a session threadSessionStore- Interface for session persistenceInMemorySessionStore- Default in-memory implementation
Example Usage
Create a session with custom state:
Session session = genkit.createSession(SessionOptions.builder().initialState(new MyState("John")).build());
Chat chat = session
.chat(ChatOptions.builder().model("openai/gpt-4o").system("You are a helpful assistant.").build());
// Multi-turn conversation (history is preserved automatically)
chat.send("What is the capital of France?");
chat.send("And what about Germany?");
// Access session state
MyState state = session.getState();
// Load an existing session
Session loadedSession = genkit.loadSession(sessionId, options).get();
Custom Session Stores
Implement SessionStore to provide custom
persistence backends (e.g., database, Redis, file system).
- See Also:
-
ClassDescriptionChat<S>Chat represents a conversation within a session thread.Options for individual send operations.Builder for SendOptions.ChatOptions<S>ChatOptions provides configuration options for creating a Chat instance.Builder for ChatOptions.InMemorySessionStore is a simple in-memory implementation of SessionStore.Session<S>Session represents a stateful chat session that persists conversation history and custom state across multiple interactions.Provides access to the current session context.Exception thrown when session operations fail.SessionData<S>SessionData represents the persistent data structure for a session, including state and conversation threads.Builder for SessionData.SessionOptions provides configuration options for creating or loading sessions.Builder for SessionOptions.SessionStore<S>SessionStore is an interface for persisting session data.