Skip to content

fix(sessions): use async iteration for VertexAiSessionService.list_sessions pagination#4435

Closed
anmolg1997 wants to merge 4 commits intogoogle:mainfrom
anmolg1997:fix/vertex-ai-session-service-pagination
Closed

fix(sessions): use async iteration for VertexAiSessionService.list_sessions pagination#4435
anmolg1997 wants to merge 4 commits intogoogle:mainfrom
anmolg1997:fix/vertex-ai-session-service-pagination

Conversation

@anmolg1997
Copy link
Contributor

@anmolg1997 anmolg1997 commented Feb 10, 2026

Link to Issue or Description of Change

Problem:

VertexAiSessionService.list_sessions() only returns the first ~100 sessions. The sessions_iterator from api_client.agent_engines.sessions.list() is an AsyncPager — it implements __aiter__/__anext__ for fetching subsequent pages, but the code uses a plain for loop which only calls __iter__/__next__, so it never fetches beyond the first page.

Solution:

Changed for api_session in sessions_iterator to async for api_session in sessions_iterator so the AsyncPager actually paginates. Updated the test mock to return an AsyncIterableList (supports both sync and async iteration) instead of a bare list, so the tests properly simulate real AsyncPager behaviour.

Testing Plan

Unit Tests:

$ pytest tests/unittests/sessions/
115 passed, 1 warning in 2.25s

The existing test_list_sessions, test_list_sessions_with_pagination, and test_list_sessions_all_users all continue to pass with the updated mock.

…ssions pagination

The sessions_iterator returned by api_client.agent_engines.sessions.list()
is an AsyncPager that only fetches additional pages when consumed via
async iteration (__aiter__/__anext__). Using a synchronous `for` loop
only retrieved the first page (~100 sessions), silently dropping all
subsequent pages.

Changed `for` to `async for` to enable proper async pagination,
allowing all sessions to be returned regardless of count.

Fixes google#4302
@google-cla
Copy link

google-cla bot commented Feb 10, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @anmolg1997, 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 addresses a critical pagination issue within the VertexAiSessionService.list_sessions method, which previously failed to retrieve all available sessions beyond the initial page. By transitioning from a synchronous iteration to an asynchronous one, the service now correctly processes AsyncPager objects, ensuring comprehensive session data retrieval. The accompanying test updates provide robust validation for this fix, preventing future regressions.

Highlights

  • Pagination Fix: Corrected a pagination bug in VertexAiSessionService.list_sessions that caused only the first page of sessions to be retrieved, silently dropping subsequent results.
  • Asynchronous Iteration: Switched from a synchronous for loop to an async for loop to properly consume AsyncPager objects, ensuring all available pages of sessions are fetched.
  • Test Mock Enhancement: Enhanced test mocks with a new AsyncIterableList class to accurately simulate asynchronous pagination behavior, improving test coverage and reliability.

🧠 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.

Changelog
  • src/google/adk/sessions/vertex_ai_session_service.py
    • Changed the iteration over sessions_iterator from for to async for to enable full pagination.
  • tests/unittests/sessions/test_vertex_ai_session_service.py
    • Introduced AsyncIterableList class to simulate AsyncPager behavior for testing purposes.
    • Modified the mock _list_sessions method to return AsyncIterableList instances, ensuring tests accurately reflect asynchronous pagination.
Activity
  • All 19 VertexAI session service tests passed.
  • All 115 session tests passed, indicating no regressions.
  • The test mock was updated with AsyncIterableList to correctly simulate AsyncPager behavior.
  • The pagination test (test_list_sessions_with_pagination) continues to pass.
  • The author completed a self-review checklist, including reading CLA and contributing guidelines, updating documentation, adding tests, ensuring all tests pass, following coding style, running linting/formatting, rebasing, and reviewing their own code.
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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

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.

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

adk-bot commented Feb 10, 2026

Response from ADK Triaging Agent

Hello @anmolg1997, thank you for your contribution!

It looks like the Contributor License Agreement (CLA) check has failed. Please sign the CLA so we can proceed with reviewing this PR.

Thanks!

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 correctly addresses a critical pagination bug in VertexAiSessionService.list_sessions by switching from a synchronous for loop to an async for loop, which is the proper way to consume an AsyncPager and retrieve all pages of results. The test suite has been thoughtfully updated to reflect this change, with the introduction of AsyncIterableList to accurately mock the behavior of the async pager. The changes are well-contained and the fix is solid. I have one minor suggestion to improve the conciseness of the new test helper class.

@anmolg1997 anmolg1997 force-pushed the fix/vertex-ai-session-service-pagination branch from bbf14a1 to e7713de Compare February 10, 2026 14:20
@anmolg1997
Copy link
Contributor Author

I have signed the CLA - @googlebot please verify.

@anmolg1997 anmolg1997 force-pushed the fix/vertex-ai-session-service-pagination branch from e7713de to 1fddf90 Compare February 10, 2026 14:36
@wuliang229 wuliang229 self-assigned this Feb 10, 2026
copybara-service bot pushed a commit that referenced this pull request Feb 11, 2026
…ssions pagination

Merge #4435

### Link to Issue or Description of Change

- Closes: #4302

**Problem:**

`VertexAiSessionService.list_sessions()` only returns the first ~100 sessions. The `sessions_iterator` from `api_client.agent_engines.sessions.list()` is an `AsyncPager` — it implements `__aiter__`/`__anext__` for fetching subsequent pages, but the code uses a plain `for` loop which only calls `__iter__`/`__next__`, so it never fetches beyond the first page.

**Solution:**

Changed `for api_session in sessions_iterator` to `async for api_session in sessions_iterator` so the `AsyncPager` actually paginates. Updated the test mock to return an `AsyncIterableList` (supports both sync and async iteration) instead of a bare list, so the tests properly simulate real `AsyncPager` behaviour.

### Testing Plan

**Unit Tests:**

```
$ pytest tests/unittests/sessions/
115 passed, 1 warning in 2.25s
```

The existing `test_list_sessions`, `test_list_sessions_with_pagination`, and `test_list_sessions_all_users` all continue to pass with the updated mock.

Co-authored-by: Liang Wu <wuliang@google.com>
COPYBARA_INTEGRATE_REVIEW=#4435 from anmolg1997:fix/vertex-ai-session-service-pagination 14c71b6
PiperOrigin-RevId: 868466166
@adk-bot
Copy link
Collaborator

adk-bot commented Feb 11, 2026

Thank you @anmolg1997 for your contribution! 🎉

Your changes have been successfully imported and merged via Copybara in commit 758d337.

Closing this PR as the changes are now in the main branch.

@adk-bot adk-bot closed this Feb 11, 2026
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.

VertexAiSessionService does not paginate when invoking list_sessions

3 participants