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
Session object 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 those
num_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 page
This would allow efficient retrieval of event history in chunks, which is essential for building features like scrollable conversation history in UIs.
Related
Environment
- google-adk version: 1.27.2
- Python: 3.11
- Database: PostgreSQL (async via asyncpg)
Description
When calling
get_session(), all events for a session are loaded into memory at once. WhileGetSessionConfigoffersnum_recent_eventsandafter_timestampfor 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
Sessionobject is expensive, especially when the caller only needs a subset.get_session()for a session with ~3k events takes ~8 seconds due to deserializing all event data at once.Current behavior
num_recent_eventsacts as a simpleLIMITfrom the tail — there's no continuation mechanism.Proposed solution
Add an
EventPaginationclass (similar toSessionPaginationfrom #4873) toGetSessionConfig:This would allow efficient retrieval of event history in chunks, which is essential for building features like scrollable conversation history in UIs.
Related
list_sessions()(same pattern)list_sessions()performanceEnvironment