Skip to content

Commit b138b5a

Browse files
rioyu123copybara-github
authored andcommitted
fix: exclude thought text from preloaded memories
Merge #7473 Parts on recalled MemoryEntry content with thought=True are now omitted by _memory_entry_utils.extract_text and from PreloadMemoryTool's <PAST_CONVERSATIONS> block, InMemoryMemoryService.search_memory skips thought parts when scoring events and drops them from the content of the entries it returns (so load_memory no longer hands them to the model), VertexAiRagMemoryService.add_session_to_memory skips thought parts at ingest, and entries with a timestamp but no visible text no longer emit a bare Time line; parts with thought=False or thought=None are unchanged. PiperOrigin-RevId: 1004025538
1 parent cd72afa commit b138b5a

7 files changed

Lines changed: 273 additions & 9 deletions

‎src/google/adk/memory/in_memory_memory_service.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,11 @@ async def search_memory(
163163
for event in session_events:
164164
if not event.content or not event.content.parts:
165165
continue
166+
visible_parts = [
167+
part for part in event.content.parts if not part.thought
168+
]
166169
event_text = ' '.join(
167-
[part.text for part in event.content.parts if part.text]
170+
[part.text for part in visible_parts if part.text]
168171
)
169172
words_in_event = _extract_searchable_words(event_text)
170173
if not words_in_event:
@@ -182,7 +185,15 @@ async def search_memory(
182185
matched_words,
183186
MemoryEntry(
184187
id=event.id,
185-
content=event.content.model_copy(deep=True),
188+
content=event.content.model_copy(
189+
deep=True,
190+
update={
191+
'parts': [
192+
part.model_copy(deep=True)
193+
for part in visible_parts
194+
]
195+
},
196+
),
186197
author=event.author,
187198
timestamp=_utils.format_timestamp(event.timestamp),
188199
),

‎src/google/adk/memory/vertex_ai_rag_memory_service.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ async def add_session_to_memory(self, session: Session) -> None:
239239
text_parts = [
240240
part.text.replace("\n", " ")
241241
for part in event.content.parts
242-
if part.text
242+
if part.text and not part.thought
243243
]
244244
if text_parts:
245245
output_lines.append(

‎src/google/adk/tools/_memory_entry_utils.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222

2323

2424
def extract_text(memory: MemoryEntry, splitter: str = ' ') -> str:
25-
"""Extracts the text from the memory entry."""
25+
"""Extracts text from the memory entry, excluding thought parts."""
2626
if not memory.content.parts:
2727
return ''
28-
return splitter.join(
29-
[part.text for part in memory.content.parts if part.text]
30-
)
28+
return splitter.join([
29+
part.text
30+
for part in memory.content.parts
31+
if part.text and not part.thought
32+
])

‎src/google/adk/tools/preload_memory_tool.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ async def process_llm_request(
7373

7474
memory_text_lines = []
7575
for memory in response.memories:
76-
if time_str := (f'Time: {memory.timestamp}' if memory.timestamp else ''):
77-
memory_text_lines.append(time_str)
7876
if memory_text := _memory_entry_utils.extract_text(memory):
77+
if memory.timestamp:
78+
memory_text_lines.append(f'Time: {memory.timestamp}')
7979
memory_text_lines.append(
8080
f'{memory.author}: {memory_text}' if memory.author else memory_text
8181
)

‎tests/unittests/memory/test_in_memory_memory_service.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,54 @@ async def test_search_memory_continues_past_event_without_words():
729729
assert len(result.memories) == 1
730730

731731

732+
@pytest.mark.asyncio
733+
async def test_search_memory_skips_thought_parts() -> None:
734+
"""Thought parts are neither matched by nor returned from a search."""
735+
memory_service = InMemoryMemoryService()
736+
await memory_service.add_events_to_memory(
737+
app_name=MOCK_APP_NAME,
738+
user_id=MOCK_USER_ID,
739+
events=[
740+
Event(
741+
id='thought-only',
742+
author='model',
743+
content=types.Content(
744+
role='model',
745+
parts=[
746+
types.Part(text='Secret plan', thought=True),
747+
types.Part.from_function_call(name='lookup', args={}),
748+
],
749+
),
750+
),
751+
Event(
752+
id='mixed',
753+
author='model',
754+
content=types.Content(
755+
role='model',
756+
parts=[
757+
types.Part(text='Secret internal note', thought=True),
758+
types.Part(text='Take the train to Paris.'),
759+
],
760+
),
761+
),
762+
],
763+
)
764+
765+
secret_search = await memory_service.search_memory(
766+
app_name=MOCK_APP_NAME, user_id=MOCK_USER_ID, query='Secret'
767+
)
768+
769+
assert not secret_search.memories
770+
771+
paris_search = await memory_service.search_memory(
772+
app_name=MOCK_APP_NAME, user_id=MOCK_USER_ID, query='Paris'
773+
)
774+
775+
assert [memory.content.parts for memory in paris_search.memories] == [
776+
[types.Part(text='Take the train to Paris.')]
777+
]
778+
779+
732780
@pytest.mark.asyncio
733781
async def test_delete_memory():
734782
"""Tests that delete_memory purges a single memory entry by its id."""

‎tests/unittests/memory/test_vertex_ai_rag_memory_service.py‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import json
1717
import logging
1818
import os
19+
from pathlib import Path
1920
import tempfile
2021
from types import SimpleNamespace
2122

@@ -590,3 +591,76 @@ async def test_add_session_leaves_no_temp_file_when_corpus_missing(temp_dir):
590591
await memory_service.add_session_to_memory(_session())
591592

592593
assert not list(temp_dir.iterdir())
594+
595+
596+
@pytest.mark.asyncio
597+
async def test_add_session_to_memory_skips_thought_parts(
598+
mocker: MockerFixture,
599+
temp_dir: Path,
600+
) -> None:
601+
"""Thought parts are omitted when a session is added to RAG memory."""
602+
memory_service = VertexAiRagMemoryService(rag_corpus="corpus")
603+
uploaded: dict[str, str] = {}
604+
605+
async def upload_file(
606+
*, path: str, display_name: str, **_kwargs: object
607+
) -> None:
608+
with open(path, encoding="utf-8") as transcript:
609+
uploaded["display_name"] = display_name
610+
uploaded["text"] = transcript.read()
611+
612+
fake_client = _unlistable_client(mocker)
613+
fake_client.rag.upload_file.side_effect = upload_file
614+
fake_client.rag.retrieve_contexts.side_effect = (
615+
lambda **_kwargs: SimpleNamespace(
616+
contexts=SimpleNamespace(
617+
contexts=[
618+
SimpleNamespace(
619+
source_display_name=uploaded["display_name"],
620+
text=uploaded["text"],
621+
)
622+
]
623+
)
624+
)
625+
)
626+
mocker.patch(
627+
"agentplatform.Client", return_value=mocker.Mock(aio=fake_client)
628+
)
629+
session = Session(
630+
app_name="demo",
631+
user_id="alice",
632+
id="session-1",
633+
events=[
634+
Event(
635+
id="thought-only",
636+
author="model",
637+
timestamp=1.0,
638+
content=types.Content(
639+
role="model",
640+
parts=[
641+
types.Part(text="Internal thought only", thought=True)
642+
],
643+
),
644+
),
645+
Event(
646+
id="mixed",
647+
author="model",
648+
timestamp=2.0,
649+
content=types.Content(
650+
role="model",
651+
parts=[
652+
types.Part(text="Consider a zeppelin.", thought=True),
653+
types.Part(text="Take the train to Paris."),
654+
],
655+
),
656+
),
657+
],
658+
)
659+
660+
await memory_service.add_session_to_memory(session)
661+
response = await memory_service.search_memory(
662+
app_name="demo", user_id="alice", query="Paris"
663+
)
664+
665+
assert _memory_texts(response) == ["Take the train to Paris."]
666+
assert not list(temp_dir.iterdir())

‎tests/unittests/tools/test_preload_memory_tool.py‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
import logging
1616
from unittest import mock
1717

18+
from google.adk.events.event import Event
1819
from google.adk.memory.base_memory_service import SearchMemoryResponse
20+
from google.adk.memory.in_memory_memory_service import InMemoryMemoryService
1921
from google.adk.memory.memory_entry import MemoryEntry
2022
from google.adk.models.gemini_context_cache_manager import GeminiContextCacheManager
2123
from google.adk.models.llm_request import LlmRequest
24+
from google.adk.sessions.session import Session
2225
from google.adk.tools.preload_memory_tool import PreloadMemoryTool
2326
from google.genai import types
2427
import pytest
@@ -41,6 +44,132 @@ def _memory(text: str) -> MemoryEntry:
4144
)
4245

4346

47+
@pytest.mark.parametrize('thought', [True, False, None])
48+
@pytest.mark.asyncio
49+
async def test_preload_memory_preserves_visible_text_only(
50+
thought: bool | None,
51+
) -> None:
52+
"""Recalled reasoning is not presented as a previous conversation answer."""
53+
memory = MemoryEntry(
54+
content=types.Content(
55+
role='model',
56+
parts=[
57+
types.Part(text='Consider a zeppelin.', thought=thought),
58+
types.Part(text='Take the train to Paris.'),
59+
],
60+
),
61+
author='assistant',
62+
)
63+
request = LlmRequest()
64+
original_content = memory.content.model_copy(deep=True)
65+
66+
await PreloadMemoryTool().process_llm_request(
67+
tool_context=_tool_context(memory), llm_request=request
68+
)
69+
70+
assert request.contents[0].parts
71+
recalled_text = request.contents[0].parts[0].text
72+
assert recalled_text is not None
73+
assert 'Take the train to Paris.' in recalled_text
74+
assert ('Consider a zeppelin.' in recalled_text) == (not thought)
75+
assert memory.content == original_content
76+
77+
78+
@pytest.mark.parametrize(
79+
'parts',
80+
[
81+
[],
82+
[types.Part(text='Consider a zeppelin.', thought=True)],
83+
[
84+
types.Part(
85+
inline_data=types.Blob(data=b'hello', mime_type='text/plain')
86+
)
87+
],
88+
],
89+
)
90+
@pytest.mark.asyncio
91+
async def test_preload_memory_omits_entries_without_visible_text(
92+
parts: list[types.Part],
93+
) -> None:
94+
"""Empty, non-text and thought-only memories do not add conversation text."""
95+
memory = MemoryEntry(
96+
content=types.Content(role='model', parts=parts),
97+
author='assistant',
98+
timestamp='2026-07-13T12:00:00Z',
99+
)
100+
request = LlmRequest()
101+
102+
await PreloadMemoryTool().process_llm_request(
103+
tool_context=_tool_context(memory), llm_request=request
104+
)
105+
106+
assert not request.contents
107+
108+
109+
@pytest.mark.asyncio
110+
async def test_preload_memory_in_memory_service_skips_thought_parts() -> None:
111+
"""Thought-only events do not match or crowd out visible search results."""
112+
memory_service = InMemoryMemoryService()
113+
events = [
114+
Event(
115+
id=f'thought-{i}',
116+
author='model',
117+
timestamp=float(i),
118+
content=types.Content(
119+
role='model',
120+
parts=[
121+
types.Part(text='Paris travel plan', thought=True),
122+
types.Part.from_function_call(name='lookup', args={}),
123+
],
124+
),
125+
)
126+
for i in range(10)
127+
]
128+
events.append(
129+
Event(
130+
id='visible',
131+
author='model',
132+
timestamp=11.0,
133+
content=types.Content(
134+
role='model',
135+
parts=[
136+
types.Part(text='Secret internal note', thought=True),
137+
types.Part(text='Take the train to Paris.'),
138+
],
139+
),
140+
)
141+
)
142+
await memory_service.add_session_to_memory(
143+
Session(
144+
app_name='app',
145+
user_id='user',
146+
id='session-1',
147+
events=events,
148+
)
149+
)
150+
151+
async def search_memory(query: str) -> SearchMemoryResponse:
152+
return await memory_service.search_memory(
153+
app_name='app', user_id='user', query=query
154+
)
155+
156+
tool_context = mock.Mock()
157+
tool_context.user_content = types.UserContent('Paris')
158+
tool_context.search_memory = search_memory
159+
request = LlmRequest()
160+
161+
await PreloadMemoryTool().process_llm_request(
162+
tool_context=tool_context, llm_request=request
163+
)
164+
165+
assert request.contents[0].parts
166+
recalled_text = request.contents[0].parts[0].text
167+
assert recalled_text is not None
168+
assert 'Take the train to Paris.' in recalled_text
169+
assert 'Paris travel plan' not in recalled_text
170+
assert 'Secret internal note' not in recalled_text
171+
172+
44173
@pytest.mark.asyncio
45174
async def test_preload_memory_keeps_system_prefix_stable():
46175
"""Recalled memory goes into contents, never into the system instruction."""

0 commit comments

Comments
 (0)