Pagination¶
Some SDK operations return results in pages. The SDK provides Paginator (sync) and
AsyncPaginator (async) to iterate over those pages lazily, fetching the next page
only when you ask for it.
Paginator and AsyncPaginator¶
Both classes share the same interface:
Member |
Description |
|---|---|
|
Iterate over individual items across all pages |
|
Iterate over |
|
Fetch all items into a list in one call |
|
Token for the next page; |
Iterating over items¶
from pinecone import Pinecone
pc = Pinecone()
for assistant in pc.assistants.list():
print(assistant.name)
import asyncio
from pinecone import AsyncPinecone
async def main() -> None:
async with AsyncPinecone() as pc:
async for assistant in pc.assistants.list():
print(assistant.name)
asyncio.run(main())
Iterating page by page¶
for page in pc.assistants.list().pages():
print(f"Page has {len(page.items)} items")
for assistant in page.items:
print(assistant.name)
if not page.has_more:
break
async with AsyncPinecone() as pc:
async for page in pc.assistants.list().pages():
print(f"Page has {len(page.items)} items")
for assistant in page.items:
print(assistant.name)
Collecting all results at once¶
all_assistants = pc.assistants.list().to_list()
async with AsyncPinecone() as pc:
all_assistants = await pc.assistants.list().to_list()
The Page Object¶
Each page yielded by .pages() has two attributes:
Attribute |
Type |
Description |
|---|---|---|
|
|
The items on this page |
|
|
Opaque token to fetch the next page; |
|
|
|
Limiting Results¶
Pass limit to cap the total number of items returned across all pages:
# Return at most 50 assistants total
for assistant in pc.assistants.list(limit=50):
print(assistant.name)
Resuming Pagination¶
Save pagination_token to resume iteration later:
paginator = pc.assistants.list()
first_page = next(paginator.pages())
# Store the token
token = first_page.pagination_token
# Later: resume from where you left off
for assistant in pc.assistants.list(pagination_token=token):
print(assistant.name)
async with AsyncPinecone() as pc:
paginator = pc.assistants.list()
first_page = await paginator.pages().__anext__()
# Store the token
token = first_page.pagination_token
# Later: resume from where you left off
async for assistant in pc.assistants.list(pagination_token=token):
print(assistant.name)
Paginating Vector IDs¶
Index.list() automatically follows pagination and yields a ListResponse per page:
from pinecone import Pinecone
pc = Pinecone()
desc = pc.indexes.describe("product-search")
index = pc.index(host=desc.host)
for page in index.list(prefix="product#", limit=100):
for item in page.vectors:
print(item.id)
For manual control over individual pagination tokens, call Index.list_paginated()
directly:
page = index.list_paginated(prefix="product#", limit=100)
for item in page.vectors:
print(item.id)
while page.pagination is not None and page.pagination.next is not None:
page = index.list_paginated(
prefix="product#",
limit=100,
pagination_token=page.pagination.next,
)
for item in page.vectors:
print(item.id)
Single-Page Responses¶
Not every list operation pages through multiple responses. pc.indexes.list()
returns a Paginator (an AsyncPaginator on AsyncPinecone) for interface
consistency, but the server returns all indexes in a single page, so iteration
makes exactly one request:
for index in pc.indexes.list():
print(index.name)