Session: Tracking individual conversations¶
Supported in ADKPython v0.1.0TypeScript v0.2.0Go v0.1.0Java v0.1.0Kotlin v0.1.0
A Session represents a single conversation thread between a user and your
agent. Just like you wouldn't start every text message from scratch, agents need
context regarding the ongoing interaction. The Session object in ADK is
designed specifically to track and manage these individual conversation threads.
Session objects¶
When a user starts interacting with your agent, the SessionService creates a
Session object (google.adk.sessions.Session). This object acts as the
container holding everything related to that one specific chat thread. Here
are its key properties:
- Identification (
id,appName,userId): Unique labels for the conversation.id: A unique identifier for this specific conversation thread, essential for retrieving it later. A SessionService object can handle multipleSession(s). This field identifies which particular session object are we referring to. For example, "test_id_modification".app_name: Identifies which agent application this conversation belongs to. For example, "id_modifier_workflow".userId: Links the conversation to a particular user.
- History (
events): A chronological sequence of all interactions (Eventobjects – user messages, agent responses, tool actions) that have occurred within this specific thread. - Session State (
state): A place to store temporary data relevant only to this specific, ongoing conversation. This acts as a scratchpad for the agent during the interaction. We will cover how to use and managestatein detail in the next section. - Activity Tracking (
lastUpdateTime): A timestamp indicating the last time an event occurred in this conversation thread.
Example: Examining session properties¶
The following code example demonstrates how to list various values stored in a session object:
from google.adk.sessions import InMemorySessionService, Session
# Create a simple session to examine its properties
temp_service = InMemorySessionService()
example_session = await temp_service.create_session(
app_name="my_app",
user_id="example_user",
state={"initial_key": "initial_value"} # State can be initialized
)
print(f"--- Examining Session Properties ---")
print(f"ID (`id`): {example_session.id}")
print(f"Application Name (`app_name`): {example_session.app_name}")
print(f"User ID (`user_id`): {example_session.user_id}")
print(f"State (`state`): {example_session.state}") # Note: Only shows initial state here
print(f"Events (`events`): {example_session.events}") # Initially empty
print(f"Last Update (`last_update_time`): {example_session.last_update_time:.2f}")
print(f"---------------------------------")
# Clean up (optional for this example)
await temp_service.delete_session(app_name=example_session.app_name,
user_id=example_session.user_id, session_id=example_session.id)
print("The final status of temp_service - ", temp_service)
import { InMemorySessionService } from "@google/adk";
// Create a simple session to examine its properties
const tempService = new InMemorySessionService();
const exampleSession = await tempService.createSession({
appName: "my_app",
userId: "example_user",
state: {"initial_key": "initial_value"} // State can be initialized
});
console.log("--- Examining Session Properties ---");
console.log(`ID ('id'): ${exampleSession.id}`);
console.log(`Application Name ('appName'): ${exampleSession.appName}`);
console.log(`User ID ('userId'): ${exampleSession.userId}`);
console.log(`State ('state'): ${JSON.stringify(exampleSession.state)}`); // Note: Only shows initial state here
console.log(`Events ('events'): ${JSON.stringify(exampleSession.events)}`); // Initially empty
console.log(`Last Update ('lastUpdateTime'): ${exampleSession.lastUpdateTime}`);
console.log("---------------------------------");
// Clean up (optional for this example)
const finalStatus = await tempService.deleteSession({
appName: exampleSession.appName,
userId: exampleSession.userId,
sessionId: exampleSession.id
});
console.log("The final status of temp_service - ", finalStatus);
appName := "my_go_app"
userID := "example_go_user"
initialState := map[string]any{"initial_key": "initial_value"}
// Create a session to examine its properties.
createResp, err := inMemoryService.Create(ctx, &session.CreateRequest{
AppName: appName,
UserID: userID,
State: initialState,
})
if err != nil {
log.Fatalf("Failed to create session: %v", err)
}
exampleSession :=