Skip to content

Pager and AsyncPager raise IndexError when a list page is empty but next_page_token is set #3429

Todo

Description

@chaitanya0305

Environment details

  • Programming language: Python
  • OS: macOS (not OS specific)
  • Language runtime version: 3.12
  • Package version: google-genai 3.6.1, main at 36e2970

Description

Iterating the pager returned by files.list, models.list, batches.list, caches.list, tunings.list, file_search_stores.list or documents.list crashes with IndexError: list index out of range when one page comes back empty but still has a next_page_token. Items on later pages are never returned.

Google list APIs are allowed to return fewer items than page_size, including zero, before the end of the collection (AIP-158). This is common with filters (for example batches.list with a filter), where a page of the underlying scan can match nothing.

The same happens with async for on AsyncPager.

Steps to reproduce

Runs offline. It uses an httpx.MockTransport in place of the API.

import os
import httpx
from google import genai
from google.genai import types

os.environ['GOOGLE_GENAI_HTTP_CLIENT'] = 'httpx'

# page_token -> (file names, next_page_token). The second page is empty.
PAGES = {
    None: (['files/a'], 'token-1'),
    'token-1': ([], 'token-2'),
    'token-2': (['files/b'], None),
}


def handler(request):
  names, next_token = PAGES[request.url.params.get('pageToken')]
  body = {'files': [{'name': n} for n in names]}
  if next_token:
    body['nextPageToken'] = next_token
  return httpx.Response(200, json=body)


client = genai.Client(
    api_key='test-key',
    http_options=types.HttpOptions(
        httpx_client=httpx.Client(transport=httpx.MockTransport(handler))
    ),
)
print([f.name for f in client.files.list(config={'page_size': 1})])

Expected: ['files/a', 'files/b']

Actual:

  File ".../google/genai/pagers.py", line 179, in __next__
    item = self.page[self._idx]
           ~~~~~~~~~^^^^^^^^^^^
IndexError: list index out of range

Root cause

google/genai/pagers.py:173 (sync) and :230 (async):

if self._idx >= len(self):
  try:
    self.next_page()
  except IndexError:
    raise StopIteration
item = self.page[self._idx]

The page is fetched only once. If the new page is empty, self.page[0] raises IndexError. It is not converted to StopIteration, so it escapes the for loop. A last page that is empty and has no token fails the same way.

Suggested fix: change if to while, so the pager keeps fetching until it gets a non-empty page or there is no next_page_token.

A tested fix with regression tests (sync, async, all-empty pages, and client.files.list end to end) is ready on branch chaitanya0305:fix/pager-empty-page.

Activity

  1. added
    priority: p2
    type: bug
    on Nov 28, 2026
  2. Venkaiahbabuneelam commented on Nov 28, 2026

    @Venkaiahbabuneelam

    Hi @chaitanya0305,

    We have successfully reproduced this issue,IndexError on main for both Pager.next() ↗ and AsyncPager.anext() ↗ when a list page is empty. We also verified on main that replacing if self._idx >= len(self): with while self._idx >= len(self): in both methods fixes the issue.

    python
    def iter_pager_safe(pager):
    while True:
    yield from pager.page
    try:
    pager.next_page()
    except IndexError:
    break

    Thanks

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

Metadata

Metadata

Labels

priority: p2type: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions