Skip to content

fix(memory): keep the model's thought parts out of Memory Bank events - #1253

Open
gjija wants to merge 1 commit into
google:mainfrom
gjija:fix-memory-bank-thoughts
Open

gjija wants to merge 1 commit into
google:mainfrom
gjija:fix-memory-bank-thoughts

Conversation

@gjija

@gjija gjija commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

2. Or, if no issue exists, describe the change:

Problem:

VertexAiMemoryBankService.addSessionToMemory and addEventsToMemory send each event's content to memories.generate as it is. With a thinking model and thinkingConfig: {includeThoughts: true}, session events keep the model's thought parts (thought: true), so they reach Memory Bank, and Memory Bank can build memories from the model's reasoning. An event whose only part is a text thought is sent too.

shouldFilterOutEvent does not look at thought: it skips an event only when no part has text or data, and otherwise the content is sent whole, so text thoughts got through.

Example: a model event with parts [{text: 'The user may live in Paris.', thought: true}, {text: 'Try Lisbon.'}] plus an event with only {text: 'Thinking only.', thought: true}. On main the request carries both thoughts:

"directContentsSource": {
  "events": [
    {"content": {"role": "model", "parts": [
+     {"text": "The user may live in Paris.", "thought": true},
      {"text": "Try Lisbon."}
    ]}},
+   {"content": {"role": "model", "parts": [
+     {"text": "Thinking only.", "thought": true}
+   ]}}
  ]
}

Solution:

Run each event through the existing pruneThoughts helper (events/event.ts) first, and use that content for both the empty-event check and the request. pruneThoughts returns a copy, so the caller's event is not changed. An event left with no parts is skipped, as before.

This matches adk-python, where _without_thoughts in memory/vertex_ai_memory_bank_service.py is used in _should_filter_out_event and on both write paths (generate and ingest). It landed in google/adk-python#7851. The new comment follows that helper's docstring.

What changes for users: Memory Bank no longer gets thought parts, and an event with only text thoughts is not sent. If a batch holds nothing else, generate is not called and "No events to add to memory." is logged, as for any empty batch.

Testing Plan

Unit Tests:

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

New test in core/test/memory/vertex_ai_memory_bank_service_test.ts, next to "still filters out an event whose only part is a thought": "leaves the model thought parts out of the events it sends". It sends the two events above and expects only [{text: 'Try Lisbon.'}] in the request, and checks that the caller's event still has 2 parts. addEventsToMemory goes through the same function.

Without the source change, vertex_ai_memory_bank_service_test.ts fails (1 failed | 48 passed): the request still carries both thought parts, as shown above. With it:

$ npx vitest run --project unit:core core/test/memory
 Test Files  5 passed (5)
      Tests  113 passed (113)

Full unit run (npx vitest run --project unit:core --project unit:dev --project unit:integrations): all tests pass except the two streaming tests in core/test/a2a/remote_agent_test.ts, which fail only because they resolve example.com and this machine has no outside network. dev/test/utils/agent_loader_test.ts does not load without a built dist/, so its tests were skipped.

prettier --check, eslint and tsc --noEmit -p core are clean.

Manual End-to-End (E2E) Tests:

Not run against a real Memory Bank: I have no Vertex AI project for it. The unit test above stands in. It checks the exact directContentsSource events passed to the mocked memories.generateInternal.

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.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

The same gap exists in the other two memory services, which I left for separate PRs so each change comes with its own test: the RAG memory transcript (serializeSessionTranscript in rag_memory_transcript.ts) and InMemoryMemoryService.searchMemory. adk-python filters thoughts in both.

VertexAiMemoryBankService sent each event's content to memories.generate
as is, so parts marked thought: true reached Memory Bank, and an event
with only text thoughts was sent too. Memory Bank could then build
memories from the model's reasoning.

Drop the thought parts with pruneThoughts before the empty-event check
and before building the request, as adk-python does.
@Varun-S10 Varun-S10 self-assigned this Nov 13, 2026
@Varun-S10

Copy link
Copy Markdown
Contributor

Hi @gjija, thank you for raising the PR to fix the issue. We have reviewed the code changes and it does resolve the issue.
I will escalate the PR to the team to review and merge, thank you for your patience and understanding.
Hi @kalenkevich, could you please take a look?

@Varun-S10 Varun-S10 added the needs review [Status] The PR/issue is awaiting review from the maintainer label Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs review [Status] The PR/issue is awaiting review from the maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants