-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
Description
When calling get_session(), all events for a session are loaded into memory at once. While GetSessionConfig offers num_recent_events and after_timestamp for basic filtering, there is no proper pagination mechanism (offset/token-based) to retrieve events in manageable chunks.
For sessions with thousands of events (e.g. long-running agent conversations), this causes high memory usage and slow response times when fetching a session.
Impact
- Memory pressure: Loading 5k+ events into a single
Sessionobject is expensive, especially when the caller only needs a subset. - Latency: In our deployment with PostgreSQL,
get_session()for a session with ~3k events takes ~8 seconds due to deserializing all event data at once. - No random access: There's no way to fetch events starting from a specific position (e.g. "give me events 100-120") without loading everything up to that point.
Current behavior
config = GetSessionConfig(num_recent_events=50)
session = await session_service.get_session(
app_name="app", user_id="user", session_id="sid", config=config
)
# Gets last 50 events, but no way to get the *next* 50 before thosenum_recent_events acts as a simple LIMIT from the tail — there's no continuation mechanism.
Proposed solution
Add an EventPagination class (similar to SessionPagination from #4873) to GetSessionConfig:
from google.adk.sessions.base_session_service import EventPagination
config = GetSessionConfig(
event_pagination=EventPagination(page_size=50)
)
session = await session_service.get_session(
app_name="app", user_id="user", session_id="sid", config=config
)
# Access paginated events
events = session.events # up to 50 events
next_token = session.next_event_page_token # token for next pageThis would allow efficient retrieval of event history in chunks, which is essential for building features like scrollable conversation history in UIs.
Related
- list_sessions() performance degrades significantly with large session counts #4871 — pagination for
list_sessions()(same pattern) - DatabaseSessionService.list_sessions lacks pagination support #4621 — original report on
list_sessions()performance
Environment
- google-adk version: 1.27.2
- Python: 3.11
- Database: PostgreSQL (async via asyncpg)