Skip to content

Commit 4e765ae

Browse files
soundTrickercopybara-github
authored andcommitted
fix: raise ValueError when sessionId and userId are incorrect combination(#1653)
Merge #1655 Fix #1653 When session_id and user_id are incorrect combination on vertex ai session service, it should not return the session. COPYBARA_INTEGRATE_REVIEW=#1655 from soundTricker:fix/1653-invalid-session 7a3dee2 PiperOrigin-RevId: 776648361
1 parent c13c987 commit 4e765ae

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/google/adk/sessions/vertex_ai_session_service.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ async def get_session(
178178
)
179179
get_session_api_response = _convert_api_response(get_session_api_response)
180180

181+
if get_session_api_response['userId'] != user_id:
182+
raise ValueError(f'Session not found: {session_id}')
183+
181184
session_id = get_session_api_response['name'].split('/')[-1]
182185
update_timestamp = isoparse(
183186
get_session_api_response['updateTime']

tests/unittests/sessions/test_vertex_ai_session_service.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,21 @@ async def test_get_empty_session(agent_engine_id):
292292
assert str(excinfo.value) == 'Session not found: 0'
293293

294294

295+
@pytest.mark.asyncio
296+
@pytest.mark.usefixtures('mock_get_api_client')
297+
@pytest.mark.parametrize('agent_engine_id', [None, '123'])
298+
async def test_get_another_user_session(agent_engine_id):
299+
if agent_engine_id:
300+
session_service = mock_vertex_ai_session_service(agent_engine_id)
301+
else:
302+
session_service = mock_vertex_ai_session_service()
303+
with pytest.raises(ValueError) as excinfo:
304+
await session_service.get_session(
305+
app_name='123', user_id='user2', session_id='1'
306+
)
307+
assert str(excinfo.value) == 'Session not found: 1'
308+
309+
295310
@pytest.mark.asyncio
296311
@pytest.mark.usefixtures('mock_get_api_client')
297312
async def test_get_and_delete_session():

0 commit comments

Comments
 (0)