Skip to content

fix(memory): keep thought parts out of Memory Bank events - #7851

Closed
hyeonsang010716 wants to merge 2 commits into
google:mainfrom
hyeonsang010716:fix/memory-bank-thought-parts
Closed

hyeonsang010716 wants to merge 2 commits into
google:mainfrom
hyeonsang010716:fix/memory-bank-thought-parts

Conversation

@hyeonsang010716

@hyeonsang010716 hyeonsang010716 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Link to Issue or Description of Change

Problem:

VertexAiMemoryBankService passes event.content to ingest_events and
memories.generate unchanged, so the model's thought parts
(Part.thought=True) reach Memory Bank as conversation content. Memories can
then be extracted from the model's private reasoning. An event whose only text
is a thought is sent too, because _is_empty_content counts any part.text.

# A model event with parts:
#   [Part(text="the user sounds anxious, keep the reply short", thought=True),
#    Part(text="Here is the answer.")]
# and a second event whose only part is that thought.
await memory_service.add_session_to_memory(session)

# Before: 901 [(<thought>, True), ('Here is the answer.', None)]
#         902 [(<thought>, True)]
# After:  901 [('Here is the answer.', None)]

InMemoryMemoryService, VertexAiRagMemoryService (#7473) and
SqliteMemoryService (#7691) already exclude thoughts.

Solution:

Add _without_thoughts, which returns a copy of the content without thought
parts, and use it in both direct-contents paths. _should_filter_out_event
checks the filtered content, so an event with nothing left is skipped. The
session's own events are not modified, and content without thoughts is passed
through as before.

add_memory is unchanged: it takes MemoryEntry content from the caller
rather than session events.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added to test_vertex_ai_memory_bank_service.py, covering both paths:

  • Ingest path (add_session_to_memory): thought parts are dropped, a
    thought-only event is skipped, and the session's events are unchanged.
  • Ingest path (add_events_to_memory): a thought-only event alone sends no
    events.
  • Generate path: thought parts are dropped and the caller's events are
    unchanged.
  • Generate path: no call is made when every event is thought-only.

All 4 new tests fail without the source change and pass with it. Changing the
event content in place instead of copying it fails the "unchanged" checks on
both paths.

$ pytest tests/unittests/memory/test_vertex_ai_memory_bank_service.py
62 passed

$ pytest tests/unittests/memory tests/unittests/tools/test_load_memory_tool.py tests/unittests/tools/test_preload_memory_tool.py
224 passed

pre-commit run passes on the changed files, and mypy reports no issues in
vertex_ai_memory_bank_service.py.

Manual End-to-End (E2E) Tests:

I don't have a Memory Bank instance, so I ran the reproduction script from
#7850 against the mocked agentplatform.Client. On main both events are sent
with their thought parts; with this change only Here is the answer. is sent.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end. (Mocked client only; see above.)
  • Any dependent changes have been merged and published in downstream modules (N/A; no dependencies).

VertexAiMemoryBankService passed event.content to ingest_events and
memories.generate unchanged, so the model's thought parts were treated as
conversation and could be extracted into memories. Events whose only text
was a thought were sent as well.

Drop thought parts from the content of each event, and skip events that
have nothing left, as the other memory services already do.

Fixes google#7850
Build the thought events per test so one test cannot change them for
another, check the events are unchanged on the generate path as well, and
cover a thought-only event on the ingest path.
copybara-service Bot pushed a commit that referenced this pull request Oct 30, 2026
Merge #7851

VertexAiMemoryBankService no longer sends model thought parts to Memory Bank,
and skips events whose only content is thoughts.

Fixes #7850

PiperOrigin-RevId: 1010580092
@adk-bot

adk-bot commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

Thank you @hyeonsang010716 for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 4fae7b7.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot added the merged [Status] This PR is merged label Oct 30, 2026
@adk-bot adk-bot closed this Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VertexAiMemoryBankService sends the model's thought parts to Memory Bank

3 participants