I’m attempting to create a paginated query with FastAPI and the Python Azure Cosmos SDK. The pagination is successful when the max_item_count is specified at 1, but anything greater and there are 2 breaking scenarios.
- The max_item_count is greater than total number of documents. You would think this would still work, with it fetching up to the total number of documents in cosmos, but it is failing and returning [[], None].
- The max_item_count is less than the total number of documents. This does allow pagination, but the final page is still also returning [[], None] and I believe for the same reason as scenario 1, where the max_item_count is now larger than the remaining documents to be fetched.
For example, having 7 documents in Cosmos DB, with a max_item_count of 10 will fail to return any documents.
Having 7 documents in Cosmos DB, with a max_item_count of 2 will allow pagination, but on page 4 which should return the last result it actually returns 0 documents.
Here’s my code, which has been directly adapted from the samples on the SDK’s github:
def paginated_query_all_items(
container: ContainerProxy, continuation_token: str | None
) -> tuple[list | None, str | None]:
query_iterable = container.query_items(
query="SELECT * FROM r",
enable_cross_partition_query=True,
max_item_count=1,
)
try:
logger.debug(f"Querying for the first page of items")
item_pages = query_iterable.by_page(continuation_token)
page = item_pages.next()
cont_token = item_pages.continuation_token
return page, cont_token
except StopIteration:
return [], None
I’m open to any suggestions or fixes. Hoping to avoid falling back to raw HTTP requests for the data as a last resort.
Changing the max_item_count to 1 will fetch all documents via pagination, but this won’t be scalable or logical once we reach a large number of documents.
I was expecting the query to return up to max_item_count number of results, but instead it returns nothing if the max_item_count ever goes above the number of remaining documents in cosmos