Skip to content

Commit 88a4402

Browse files
DeanChensjcopybara-github
authored andcommitted
chore: Do not send api request when session does not have events
PiperOrigin-RevId: 775423356
1 parent 09f1269 commit 88a4402

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/google/adk/memory/vertex_ai_memory_bank_service.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,15 @@ async def add_session_to_memory(self, session: Session):
7878
},
7979
}
8080

81-
api_response = await api_client.async_request(
82-
http_method='POST',
83-
path=f'reasoningEngines/{self._agent_engine_id}/memories:generate',
84-
request_dict=request_dict,
85-
)
86-
logger.info(f'Generate memory response: {api_response}')
81+
if events:
82+
api_response = await api_client.async_request(
83+
http_method='POST',
84+
path=f'reasoningEngines/{self._agent_engine_id}/memories:generate',
85+
request_dict=request_dict,
86+
)
87+
logger.info(f'Generate memory response: {api_response}')
88+
else:
89+
logger.info('No events to add to memory.')
8790

8891
@override
8992
async def search_memory(self, *, app_name: str, user_id: str, query: str):

tests/unittests/memory/test_vertex_ai_memory_bank_service.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@
4848
],
4949
)
5050

51+
MOCK_SESSION_WITH_EMPTY_EVENTS = Session(
52+
app_name=MOCK_APP_NAME,
53+
user_id=MOCK_USER_ID,
54+
id='444',
55+
last_update_time=22333,
56+
)
57+
5158

5259
RETRIEVE_MEMORIES_REGEX = r'^reasoningEngines/([^/]+)/memories:retrieve$'
5360
GENERATE_MEMORIES_REGEX = r'^reasoningEngines/([^/]+)/memories:generate$'
@@ -136,6 +143,15 @@ async def test_add_session_to_memory(mock_get_api_client):
136143
)
137144

138145

146+
@pytest.mark.asyncio
147+
@pytest.mark.usefixtures('mock_get_api_client')
148+
async def test_add_empty_session_to_memory(mock_get_api_client):
149+
memory_service = mock_vertex_ai_memory_bank_service()
150+
await memory_service.add_session_to_memory(MOCK_SESSION_WITH_EMPTY_EVENTS)
151+
152+
mock_get_api_client.async_request.assert_not_called()
153+
154+
139155
@pytest.mark.asyncio
140156
@pytest.mark.usefixtures('mock_get_api_client')
141157
async def test_search_memory(mock_get_api_client):

0 commit comments

Comments
 (0)