Skip to content

feat: add pagination support to list_sessions()#4873

Open
ferponse wants to merge 3 commits intogoogle:mainfrom
ferponse:feat/list-sessions-pagination
Open

feat: add pagination support to list_sessions()#4873
ferponse wants to merge 3 commits intogoogle:mainfrom
ferponse:feat/list-sessions-pagination

Conversation

@ferponse
Copy link

Closes #4871

Summary

DatabaseSessionService.list_sessions() (and all other session service implementations) now support pagination via page_size and page_token parameters.

Changes

base_session_service.py

  • Added next_page_token: Optional[str] field to ListSessionsResponse
  • Updated list_sessions abstract signature with page_size and page_token kwargs
  • Added shared pagination helpers: _resolve_page_size, _encode_page_token, _decode_page_token

database_session_service.py

  • Added ORDER BY update_time DESC + LIMIT/OFFSET to the SQL query
  • Fetches page_size + 1 rows to detect next page without a separate COUNT query
  • Returns next_page_token when more results exist

in_memory_session_service.py

  • Sorts sessions by last_update_time descending
  • Applies offset/limit slicing with the same pagination contract

sqlite_session_service.py

  • Same ORDER BY update_time DESC LIMIT ? OFFSET ? approach as DatabaseSessionService

vertex_ai_session_service.py

  • Signature updated for compatibility (pagination not yet wired to the upstream API)

Tests

  • 8 new test cases covering: default page size, custom page size, max clamping, full iteration, exact-fit boundary, empty results, backward compatibility, and ordering

Backward Compatibility

All parameters are optional with sensible defaults:

  • page_size defaults to 20 (max 100)
  • page_token defaults to None (first page)
  • Existing callers that pass no pagination args continue to work — they receive the first page of up to 20 sessions with next_page_token=None when there are ≤20 results

Test plan

  • Default pagination returns first 20 sessions
  • Custom page_size is respected
  • page_size > 100 is clamped to 100
  • Full iteration via page_token collects all sessions exactly once
  • Exact-fit boundary (total == page_size) returns no next token
  • Empty result returns no token
  • Backward compatible without pagination args
  • Results ordered by update_time DESC

Add page_size and page_token parameters to list_sessions() across all
session service implementations to avoid unbounded queries on large
datasets.

Closes google#4871
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces robust pagination capabilities to the list_sessions() method across all session service implementations. This enhancement allows for more efficient retrieval of session data by enabling clients to request sessions in manageable chunks using page_size and page_token parameters. The changes ensure improved performance and resource utilization, especially for applications handling a large volume of sessions, while maintaining backward compatibility for existing API callers.

Highlights

  • Pagination Support: Added pagination via page_size and page_token parameters to DatabaseSessionService.list_sessions() and other session service implementations.
  • Base Session Service Updates: Introduced next_page_token to ListSessionsResponse, updated the list_sessions abstract signature, and added shared pagination helper functions (_resolve_page_size, _encode_page_token, _decode_page_token).
  • Database Query Enhancements: Modified SQL queries in DatabaseSessionService and SqliteSessionService to include ORDER BY update_time DESC and LIMIT/OFFSET for pagination, fetching an extra row to detect the next page.
  • In-Memory Session Service: Implemented sorting by last_update_time descending and applied offset/limit slicing for pagination in the InMemorySessionService.
  • Vertex AI Session Service: Updated the list_sessions signature for compatibility, though pagination is not yet wired to the upstream API.
  • Comprehensive Testing: Added 8 new test cases covering various pagination scenarios, including default page size, custom page size, max clamping, full iteration, exact-fit boundary, empty results, backward compatibility, and ordering.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@ferponse
Copy link
Author

@sasha-gitg Could you please take a look at this when you get a chance? This is blocking our production deployment — we're hitting severe latency issues with list_sessions() on apps with thousands of sessions (see #4871 and #4621). Would really appreciate a review. Thanks!

@adk-bot adk-bot added the services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc label Mar 18, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces pagination support to the list_sessions() method across various session service implementations. The changes include adding page_size and page_token parameters, updating the ListSessionsResponse model with a next_page_token, and implementing the pagination logic in DatabaseSessionService, InMemorySessionService, and SqliteSessionService. The implementation correctly uses ORDER BY, LIMIT, and OFFSET (or equivalent slicing) and fetches one extra row to efficiently determine the presence of a next page. The page_token is base64 encoded/decoded for robustness. Comprehensive unit tests have been added to cover various pagination scenarios, ensuring correctness and backward compatibility. Overall, the changes are well-structured and address the pagination requirement effectively.

Catch (ValueError, TypeError) instead of (ValueError, Exception) to
avoid masking unexpected programming errors.
@gemini-code-assist
Copy link
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

services [Component] This issue is related to runtime services, e.g. sessions, memory, artifacts, etc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

list_sessions() performance degrades significantly with large session counts

2 participants