Skip to content

Commit 021f6f6

Browse files
google-genai-botcopybara-github
authored andcommitted
fix(sessions): apply after_timestamp and num_recent_events together in VertexAiSessionService
Previously, `get_session` sent the server-side `timestamp>=` filter only when `num_recent_events` was unset, so setting both `GetSessionConfig` filters silently dropped the timestamp one. This CL sends `after_timestamp` to the server whenever it is set and still trims to `num_recent_events` client-side, so both filters now compose. Every other ADK session service already applies both filters together -- the in-memory, database and sqlite services here, and the Vertex services in the Go/JS/Kotlin/Java ports; this was the last one out of parity. PiperOrigin-RevId: 952818684
1 parent 481fe21 commit 021f6f6

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/google/adk/sessions/vertex_ai_session_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ async def get_session(
236236
async with self._get_api_client() as api_client:
237237
# Get session resource and events in parallel.
238238
list_events_kwargs = {}
239-
if config and not config.num_recent_events and config.after_timestamp:
239+
if config and config.after_timestamp:
240240
# Filter events based on timestamp.
241241
list_events_kwargs['config'] = {
242242
'filter': 'timestamp>="{}"'.format(

tests/unittests/sessions/test_vertex_ai_session_service.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,26 @@ async def test_get_session_with_after_timestamp_filter():
796796
assert session.events[0].id == '456'
797797

798798

799+
@pytest.mark.asyncio
800+
@pytest.mark.usefixtures('mock_get_api_client')
801+
async def test_get_session_with_num_recent_events_and_after_timestamp():
802+
session_service = mock_vertex_ai_session_service()
803+
session = await session_service.get_session(
804+
app_name='123',
805+
user_id='user',
806+
session_id='2',
807+
config=GetSessionConfig(
808+
num_recent_events=2,
809+
after_timestamp=isoparse('2024-12-12T12:12:13.0Z').timestamp(),
810+
),
811+
)
812+
assert session is not None
813+
# after_timestamp must be applied even though num_recent_events is set;
814+
# without it both events would be returned.
815+
assert len(session.events) == 1
816+
assert session.events[0].id == '456'
817+
818+
799819
@pytest.mark.asyncio
800820
@pytest.mark.usefixtures('mock_get_api_client')
801821
async def test_get_session_keeps_events_newer_than_update_time(

0 commit comments

Comments
 (0)