From 8907409f10dbb203895c317003d26e4e5f49dc4b Mon Sep 17 00:00:00 2001 From: Zyan Date: Mon, 20 Jul 2026 15:27:41 -0600 Subject: [PATCH 1/2] Update custom_metadata per Issue 1292 - 8 Rendered page: Agent PR: #1306 Original Issue: #1292 -Polished the explanation of how and why custom_metadata works. It now clearly outlines how developers can pass service-specific configurations (like ttl and revision_ttl) directly to the underlying Agent Platform. - I found that the add_events_to_memory(events=...) section that the agent was proposing was already documented in the site. --- docs/sessions/memory.md | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/docs/sessions/memory.md b/docs/sessions/memory.md index 14e68bf6a3..68b57b3482 100644 --- a/docs/sessions/memory.md +++ b/docs/sessions/memory.md @@ -585,6 +585,61 @@ For example, you can automate this step with a callback: --8<-- "examples/kotlin/snippets/sessions/MemoryExample.kt:auto_save_callback" ``` + +## Extend memory capabilities + +When you use `VertexAiMemoryBankService`, you can provide additional +configuration to the underlying Agent Platform API via the +`custom_metadata` parameter. This parameter controls advanced features like +Time-to-Live (TTL) and is supported in both `add_session_to_memory` and +`add_events_to_memory`. + +**Supported Metadata Keys:** + +* `ttl`: Sets the expiration time for the memory. +* `revision_ttl`: Sets the expiration time for the revision of the memory. + +Use the following example to configure expiration times for your memory records: + +```python +import asyncio +from google.adk.memory import VertexAiMemoryBankService + +# Assume my_memory_service is an instance of VertexAiMemoryBankService +# and my_latest_events is a list of new adk.Event objects from the latest turn. +my_latest_events = [...] + +async def update_incremental_memory(my_memory_service, my_latest_events): + # Example 1: Basic incremental update + await my_memory_service.add_events_to_memory( + app_name="my-app", + user_id="my-user", + events=my_latest_events, + session_id="my-optional-session-id" + ) + + # Example 2: Incremental update with Custom Metadata (TTL) + await my_memory_service.add_events_to_memory( + app_name="my-app", + user_id="my-user", + events=my_latest_events, + session_id="my-optional-session-id", + custom_metadata={ + "ttl": "3600s" # Expire memory in 1 hour + } + ) + +async def update_session_memory(my_memory_service, my_completed_session): + # Example 3: Applying custom metadata to a full session + await my_memory_service.add_session_to_memory( + session=my_completed_session, + custom_metadata={ + "revision_ttl": "7200s" # Expire revision in 2 hours + } + ) + +``` + ## Advanced concepts ### How memory works in practice From 6516f6f634fb09f24c0d34da4fff0280ffbf7ef1 Mon Sep 17 00:00:00 2001 From: Zyan Date: Fri, 24 Jul 2026 14:33:33 -0600 Subject: [PATCH 2/2] Update memory.md Worked on feedback. Updated the custom metadata documentation to use a generic BaseMemoryService example instead of platform-specific services. The code snippet was also revised to use generic key-value pairs rather than prescriptive TTL keys. --- docs/sessions/memory.md | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/docs/sessions/memory.md b/docs/sessions/memory.md index 68b57b3482..9c1898dfe7 100644 --- a/docs/sessions/memory.md +++ b/docs/sessions/memory.md @@ -585,27 +585,19 @@ For example, you can automate this step with a callback: --8<-- "examples/kotlin/snippets/sessions/MemoryExample.kt:auto_save_callback" ``` - ## Extend memory capabilities -When you use `VertexAiMemoryBankService`, you can provide additional -configuration to the underlying Agent Platform API via the -`custom_metadata` parameter. This parameter controls advanced features like -Time-to-Live (TTL) and is supported in both `add_session_to_memory` and -`add_events_to_memory`. - -**Supported Metadata Keys:** - -* `ttl`: Sets the expiration time for the memory. -* `revision_ttl`: Sets the expiration time for the revision of the memory. - -Use the following example to configure expiration times for your memory records: +Memory services extended from `BaseMemoryService` support adding sessions and +events to agent memory, including custom metadata. Use the +`add_session_to_memory` and `add_events_to_memory` methods of memory services +such as `InMemoryMemoryService` to amend memory data, as shown in the +following code example: ```python import asyncio -from google.adk.memory import VertexAiMemoryBankService +from google.adk.memory import InMemoryMemoryService -# Assume my_memory_service is an instance of VertexAiMemoryBankService +# Assume my_memory_service is an instance of InMemoryMemoryService # and my_latest_events is a list of new adk.Event objects from the latest turn. my_latest_events = [...] @@ -618,14 +610,14 @@ async def update_incremental_memory(my_memory_service, my_latest_events): session_id="my-optional-session-id" ) - # Example 2: Incremental update with Custom Metadata (TTL) + # Example 2: Incremental update with Custom Metadata await my_memory_service.add_events_to_memory( app_name="my-app", user_id="my-user", events=my_latest_events, session_id="my-optional-session-id", custom_metadata={ - "ttl": "3600s" # Expire memory in 1 hour + "my_custom_key": "my_custom_value" } ) @@ -634,7 +626,7 @@ async def update_session_memory(my_memory_service, my_completed_session): await my_memory_service.add_session_to_memory( session=my_completed_session, custom_metadata={ - "revision_ttl": "7200s" # Expire revision in 2 hours + "category": "user_preference" } )