diff --git a/docs/sessions/memory.md b/docs/sessions/memory.md index 14e68bf6a3..9c1898dfe7 100644 --- a/docs/sessions/memory.md +++ b/docs/sessions/memory.md @@ -585,6 +585,53 @@ For example, you can automate this step with a callback: --8<-- "examples/kotlin/snippets/sessions/MemoryExample.kt:auto_save_callback" ``` +## Extend memory capabilities + +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 InMemoryMemoryService + +# 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 = [...] + +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 + 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={ + "my_custom_key": "my_custom_value" + } + ) + +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={ + "category": "user_preference" + } + ) + +``` + ## Advanced concepts ### How memory works in practice