Skip to content

Commit d4d2f6e

Browse files
GWealecopybara-github
authored andcommitted
fix(memory): honor Vertex RAG top-k configuration
VertexAiRagMemoryService accepts a similarity_top_k argument but dropped it when constructing the VertexRagStore, so search_memory read back None and retrieval silently ignored the configured limit. The writer was removed during the agentplatform migration while the reader was kept. The value is now held on the service and passed to the RagQuery, which is where retrieveContexts reads it. It is deliberately not restored on the VertexRagStore: that request message carries rag_resources, rag_corpora and vector_distance_threshold only, so a similarity_top_k set there would travel as an undefined field on the request rather than as a retrieval limit. Behavior change: anyone who set similarity_top_k has been getting the API default and now gets the value they configured, so result counts move down or up depending on whether that value is below or above the default. No public interface changes. Co-authored-by: George Weale <gweale@google.com> PiperOrigin-RevId: 954790154
1 parent f72f0db commit d4d2f6e

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

src/google/adk/memory/vertex_ai_rag_memory_service.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ def __init__(
136136
self._project = self._project or parts[1]
137137
self._location = self._location or parts[3]
138138

139+
# Top-k belongs on the retrieval query, not on the store: the
140+
# retrieveContexts request's VertexRagStore has no such field.
141+
self._similarity_top_k = similarity_top_k
139142
self._vertex_rag_store = types.VertexRagStore(
140143
rag_resources=[
141144
types.VertexRagStoreRagResource(rag_corpus=rag_corpus),
@@ -208,7 +211,7 @@ async def search_memory(
208211
vertex_rag_store=self._vertex_rag_store,
209212
query=agentplatform_types.RagQuery(
210213
text=query,
211-
similarity_top_k=self._vertex_rag_store.similarity_top_k,
214+
similarity_top_k=self._similarity_top_k,
212215
),
213216
)
214217
memory_results = []

tests/unittests/memory/test_vertex_ai_rag_memory_service.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from google.adk.sessions.session import Session
2323
from google.genai import types
2424
import pytest
25+
from pytest_mock import MockerFixture
2526

2627

2728
def _rag_context(source_display_name: str, text: str) -> SimpleNamespace:
@@ -31,6 +32,34 @@ def _rag_context(source_display_name: str, text: str) -> SimpleNamespace:
3132
)
3233

3334

35+
@pytest.mark.asyncio
36+
@pytest.mark.parametrize("configured_top_k", [7, None])
37+
async def test_search_memory_forwards_similarity_top_k(
38+
mocker: MockerFixture,
39+
configured_top_k: int | None,
40+
) -> None:
41+
memory_service = VertexAiRagMemoryService(
42+
rag_corpus="unused",
43+
similarity_top_k=configured_top_k,
44+
)
45+
fake_client = mocker.Mock()
46+
fake_client.rag.retrieve_contexts.return_value = SimpleNamespace(
47+
contexts=SimpleNamespace(contexts=[])
48+
)
49+
mocker.patch("agentplatform.Client", return_value=fake_client)
50+
51+
await memory_service.search_memory(
52+
app_name="demo", user_id="alice", query="memory"
53+
)
54+
55+
fake_client.rag.retrieve_contexts.assert_called_once()
56+
kwargs = fake_client.rag.retrieve_contexts.call_args.kwargs
57+
assert kwargs["query"].similarity_top_k == configured_top_k
58+
# retrieveContexts reads top-k from the query; sending it on the store
59+
# would put an undefined field on the request.
60+
assert kwargs["vertex_rag_store"].similarity_top_k is None
61+
62+
3463
@pytest.mark.asyncio
3564
async def test_search_memory_rejects_ambiguous_legacy_display_names(mocker):
3665
"""Ensures dotted user IDs cannot match another user's legacy memory."""

0 commit comments

Comments
 (0)