Pinecone’s list_ids operation is your gateway to traversing your vector index, but its default behavior is to give you just a taste, not the whole buffet.

Let’s see it in action. Imagine you have a Pinecone index named my-index and you want to start listing IDs.

from pinecone import Pinecone

# Initialize Pinecone (replace with your actual API key and environment)
pc = Pinecone(api_key="YOUR_API_KEY", environment="YOUR_ENVIRONMENT")

# Connect to your index
index = pc.Index("my-index")

# Fetch the first page of IDs
initial_list = index.list_ids(limit=100)

print("First page of IDs:", initial_list)

This gives you a VectorIdList object, which contains a ids field (a list of strings) and a pagination field. The pagination field is crucial here. It holds a next_token. If next_token is present, it means there are more IDs to fetch.

To paginate through all vectors, you repeatedly call list_ids, using the next_token from the previous response in the pagination_token parameter of the next request. You continue this loop until next_token is no longer present in the response.

all_ids = []
next_page_token = None
page_limit = 1000 # Pinecone's max limit per page

while True:
    response = index.list_ids(
        limit=page_limit,
        pagination_token=next_page_token
    )
    all_ids.extend(response.ids)
    next_page_token = response.pagination.next_token

    if not next_page_token:
        break

print(f"Fetched a total of {len(all_ids)} IDs.")
# Now 'all_ids' contains all vector IDs in your index

This process of iterating with next_token is the fundamental mechanism for handling large datasets that exceed a single API response. It’s designed to be efficient, allowing you to retrieve data in manageable chunks without overwhelming your client or the Pinecone service.

The limit parameter, set to a maximum of 1000, dictates how many IDs are returned per request. Choosing a higher limit reduces the number of round trips to Pinecone, potentially speeding up the overall fetch, but it also means larger individual responses. The next_token is an opaque string generated by Pinecone that uniquely identifies the position in the dataset from which to start the next fetch. It’s not a cursor you can manipulate directly; you simply pass it back to Pinecone.

What often trips people up is assuming the first list_ids call will return everything, or not handling the next_token correctly. The pagination object is always present, but next_token will be None on the last page of results, which is your signal to stop.

Once you’ve paginated through all your IDs, the next logical step is to fetch the actual vectors associated with those IDs, or perhaps to start performing similarity searches across the entire dataset.

Want structured learning?

Take the full Pinecone course →