diff --git a/src/google/adk/integrations/firestore/firestore_memory_service.py b/src/google/adk/integrations/firestore/firestore_memory_service.py index c3fb1a99e59..7a2bb82ac06 100644 --- a/src/google/adk/integrations/firestore/firestore_memory_service.py +++ b/src/google/adk/integrations/firestore/firestore_memory_service.py @@ -156,7 +156,11 @@ async def _write_memories( if not event.content or not event.content.parts: continue - text = " ".join([part.text for part in event.content.parts if part.text]) + # The model's thoughts are not part of the conversation: keep them out + # of the keywords and the stored content, as the other memory services + # do. + parts = [part for part in event.content.parts if not part.thought] + text = " ".join([part.text for part in parts if part.text]) if not text: continue @@ -180,8 +184,10 @@ async def _write_memories( "sessionId": session_id, "keywords": list(keywords), "author": event.author, - "content": event.content.model_dump( - exclude_none=True, mode="json" + "content": ( + event.content.model_copy(update={"parts": parts}).model_dump( + exclude_none=True, mode="json" + ) ), "timestamp": event.timestamp, }, @@ -237,6 +243,10 @@ async def _search_by_keyword( from google.genai import types content = types.Content.model_validate(data["content"]) + # Memories written before thoughts were filtered at ingest may + # still hold them. + if content.parts: + content.parts = [part for part in content.parts if not part.thought] entries.append( MemoryEntry( content=content, diff --git a/tests/unittests/integrations/firestore/test_firestore_memory_service.py b/tests/unittests/integrations/firestore/test_firestore_memory_service.py index 479b6cfed84..c659ecff965 100644 --- a/tests/unittests/integrations/firestore/test_firestore_memory_service.py +++ b/tests/unittests/integrations/firestore/test_firestore_memory_service.py @@ -299,6 +299,97 @@ async def test_add_session_to_memory(mock_firestore_client): assert data["timestamp"] == 1234567890.0 +def _mock_batch(client): + batch = mock.MagicMock() + batch.commit = mock.AsyncMock() + client.batch.return_value = batch + return batch + + +@pytest.mark.asyncio +async def test_add_session_to_memory_keeps_thoughts_out(mock_firestore_client): + service = FirestoreMemoryService(client=mock_firestore_client) + batch = _mock_batch(mock_firestore_client) + session = Session(id="s1", app_name="test_app", user_id="test_user") + session.events.append( + Event( + invocation_id="inv", + author="model", + content=types.Content( + role="model", + parts=[ + types.Part( + text="weighing the hidden escalation", thought=True + ), + types.Part.from_text(text="quick brown fox"), + ], + ), + ) + ) + + await service.add_session_to_memory(session) + + # The model's thoughts are not part of the conversation, so they must not + # become search keywords or be handed back to the model as a memory. + data = batch.set.call_args.args[1] + assert set(data["keywords"]) == {"quick", "brown", "fox"} + assert data["content"]["parts"] == [{"text": "quick brown fox"}] + + +@pytest.mark.asyncio +async def test_add_session_to_memory_skips_thought_only_events( + mock_firestore_client, +): + service = FirestoreMemoryService(client=mock_firestore_client) + batch = _mock_batch(mock_firestore_client) + session = Session(id="s1", app_name="test_app", user_id="test_user") + session.events.append( + Event( + invocation_id="inv", + author="model", + content=types.Content( + role="model", + parts=[types.Part(text="planning the reply", thought=True)], + ), + ) + ) + + await service.add_session_to_memory(session) + + batch.set.assert_not_called() + + +@pytest.mark.asyncio +async def test_search_memory_drops_stored_thought_parts(mock_firestore_client): + service = FirestoreMemoryService(client=mock_firestore_client) + doc_snapshot = mock_firestore_client.collection.return_value.get.return_value[ + 0 + ] + # A memory written before thoughts were filtered at ingest. + content = types.Content( + role="model", + parts=[ + types.Part(text="weighing the hidden escalation", thought=True), + types.Part.from_text(text="quick brown fox"), + ], + ) + doc_snapshot.to_dict.return_value = { + "appName": "test_app", + "userId": "test_user", + "author": "model", + "content": content.model_dump(exclude_none=True, mode="json"), + "timestamp": 1234567890.0, + } + + response = await service.search_memory( + app_name="test_app", user_id="test_user", query="fox" + ) + + assert [p.text for p in response.memories[0].content.parts] == [ + "quick brown fox" + ] + + def _text_event(text, event_id): return Event( id=event_id,