|
| 1 | +--- |
| 2 | +catalog_title: Session Management with Firestore |
| 3 | +catalog_description: Session state management for ADK agents using Google Cloud Firestore |
| 4 | +catalog_icon: /integrations/assets/firestore-session.png |
| 5 | +catalog_tags: ["sessions", "database"] |
| 6 | +--- |
| 7 | + |
| 8 | +# Session Management with Google Cloud Firestore |
| 9 | + |
| 10 | +<div class="language-support-tag"> |
| 11 | + <span class="lst-supported">Supported in ADK</span><span class="lst-java">Java</span> |
| 12 | +</div> |
| 13 | + |
| 14 | +[Google Cloud Firestore](https://cloud.google.com/firestore) is a flexible, scalable NoSQL cloud database to store and sync data for client- and server-side development. |
| 15 | +ADK provides a native integration for managing persistent agent session states using Firestore, allowing continuous multi-turn conversations without losing conversation history. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +- A [Google Cloud Project](https://cloud.google.com/) with Firestore enabled |
| 20 | +- A [Firestore database](https://cloud.google.com/firestore/docs/setup) in your Google Cloud Project |
| 21 | +- Appropriate [Google Cloud credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc) configured in your environment |
| 22 | + |
| 23 | +## Install dependencies |
| 24 | + |
| 25 | +Add the following dependencies to your `pom.xml` (Maven) or `build.gradle` (Gradle): |
| 26 | + |
| 27 | +### Maven |
| 28 | + |
| 29 | +```xml |
| 30 | +<dependencies> |
| 31 | + <!-- ADK Core --> |
| 32 | + <dependency> |
| 33 | + <groupId>com.google.adk</groupId> |
| 34 | + <artifactId>google-adk</artifactId> |
| 35 | + <version>1.0.0</version> |
| 36 | + </dependency> |
| 37 | + <!-- Firestore Session Service --> |
| 38 | + <dependency> |
| 39 | + <groupId>com.google.adk</groupId> |
| 40 | + <artifactId>google-adk-firestore-session-service</artifactId> |
| 41 | + <version>1.0.0</version> |
| 42 | + </dependency> |
| 43 | +</dependencies> |
| 44 | +``` |
| 45 | + |
| 46 | +### Gradle |
| 47 | + |
| 48 | +```gradle |
| 49 | +dependencies { |
| 50 | + // ADK Core |
| 51 | + implementation 'com.google.adk:google-adk:1.0.0-SNAPSHOT' |
| 52 | + // Firestore Session Service |
| 53 | + implementation 'com.google.adk:google-adk-firestore-session-service:1.0.0-SNAPSHOT' |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Example: Agent with Firestore Session Management |
| 58 | + |
| 59 | +Use `FirestoreDatabaseRunner` to encapsulate your agent and Firestore-backed session management. Here is a complete example of setting up a simple assistant agent that remembers conversation context across turns using a custom session ID. |
| 60 | + |
| 61 | +```java |
| 62 | +import com.google.adk.agents.BaseAgent; |
| 63 | +import com.google.adk.agents.LlmAgent; |
| 64 | +import com.google.adk.agents.RunConfig; |
| 65 | +import com.google.adk.runner.FirestoreDatabaseRunner; |
| 66 | +import com.google.cloud.firestore.Firestore; |
| 67 | +import com.google.cloud.firestore.FirestoreOptions; |
| 68 | +import io.reactivex.rxjava3.core.Flowable; |
| 69 | +import java.util.Map; |
| 70 | +import com.google.adk.sessions.FirestoreSessionService; |
| 71 | +import com.google.adk.sessions.Session; |
| 72 | +import com.google.adk.tools.Annotations.Schema; |
| 73 | +import com.google.adk.tools.FunctionTool; |
| 74 | +import com.google.genai.types.Content; |
| 75 | +import com.google.genai.types.Part; |
| 76 | +import com.google.adk.events.Event; |
| 77 | +import java.util.Scanner; |
| 78 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 79 | + |
| 80 | +public class YourAgentApplication { |
| 81 | + |
| 82 | + public static void main(String[] args) { |
| 83 | + System.out.println("Starting YourAgentApplication..."); |
| 84 | + |
| 85 | + RunConfig runConfig = RunConfig.builder().build(); |
| 86 | + String appName = "hello-time-agent"; |
| 87 | + |
| 88 | + BaseAgent timeAgent = initAgent(); |
| 89 | + |
| 90 | + // Initialize Firestore |
| 91 | + FirestoreOptions firestoreOptions = FirestoreOptions.getDefaultInstance(); |
| 92 | + Firestore firestore = firestoreOptions.getService(); |
| 93 | + |
| 94 | + // Use FirestoreDatabaseRunner to persist session state |
| 95 | + FirestoreDatabaseRunner runner = new FirestoreDatabaseRunner( |
| 96 | + timeAgent, |
| 97 | + appName, |
| 98 | + firestore |
| 99 | + ); |
| 100 | + |
| 101 | + // Create a new session or load an existing one |
| 102 | + Session session = new FirestoreSessionService(firestore) |
| 103 | + .createSession(appName, "user1234", null, "12345") |
| 104 | + .blockingGet(); |
| 105 | + |
| 106 | + // Start interactive CLI |
| 107 | + try (Scanner scanner = new Scanner(System.in, UTF_8)) { |
| 108 | + while (true) { |
| 109 | + System.out.print("\\nYou > "); |
| 110 | + String userInput = scanner.nextLine(); |
| 111 | + if ("quit".equalsIgnoreCase(userInput)) { |
| 112 | + break; |
| 113 | + } |
| 114 | + |
| 115 | + Content userMsg = Content.fromParts(Part.fromText(userInput)); |
| 116 | + Flowable<Event> events = runner.runAsync(session.userId(), session.id(), userMsg, runConfig); |
| 117 | + |
| 118 | + System.out.print("\\nAgent > "); |
| 119 | + events.blockingForEach(event -> { |
| 120 | + if (event.finalResponse()) { |
| 121 | + System.out.println(event.stringifyContent()); |
| 122 | + } |
| 123 | + }); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** Mock tool implementation */ |
| 129 | + @Schema(description = "Get the current time for a given city") |
| 130 | + public static Map<String, String> getCurrentTime( |
| 131 | + @Schema(name = "city", description = "Name of the city to get the time for") String city) { |
| 132 | + return Map.of( |
| 133 | + "city", city, |
| 134 | + "forecast", "The time is 10:30am." |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + private static BaseAgent initAgent() { |
| 139 | + return LlmAgent.builder() |
| 140 | + .name("hello-time-agent") |
| 141 | + .description("Tells the current time in a specified city") |
| 142 | + .instruction(\""" |
| 143 | + You are a helpful assistant that tells the current time in a city. |
| 144 | + Use the 'getCurrentTime' tool for this purpose. |
| 145 | + \""") |
| 146 | + .model("gemini-3.1-pro-preview") |
| 147 | + .tools(FunctionTool.create(YourAgentApplication.class, "getCurrentTime")) |
| 148 | + .build(); |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
| 152 | +
|
| 153 | +## Configuration |
| 154 | +
|
| 155 | +> [!NOTE] |
| 156 | +> The Firestore Session Service supports properties file configuration. This allows you to easily target a dedicated Firestore database and define custom collection names for storing your agent session data. |
| 157 | +
|
| 158 | +
|
| 159 | +## Resources |
| 160 | +
|
| 161 | +- [Firestore Session Service](https://github.com/google/adk-java/tree/main/contrib/firestore-session-service): |
| 162 | + Source code for the Firestore Session Service. |
| 163 | +- [Spring Boot Google ADK + Firestore Example](https://github.com/mohan-ganesh/spring-boot-google-adk-firestore): |
| 164 | + An example project demonstrating how to build a Java-based Google ADK agent application using Cloud Firestore for session management. |
| 165 | +- [Firestore Session Service - DeepWiki](https://deepwiki.com/google/adk-java/4.3-firestore-session-service): |
| 166 | + Detailed description of Firestore integration in the Google ADK for Java. |
0 commit comments