Skip to content

Conversation

@mattt
Copy link
Contributor

@mattt mattt commented Nov 11, 2023

This PR adds paginate and async_paginate methods that let you iterate over a paginated list of resources.

sync

import replicate

for page in replicate.paginate(replicate.collections.list):
  for collection in page:
      print(collection.name)

async

import replicate

async for page in replicate.async_paginate(replicate.collections.async_list):
  for collection in page:
      print(collection.name)

@mattt mattt requested a review from bfirsh November 11, 2023 15:17
@bfirsh
Copy link
Member

bfirsh commented Nov 11, 2023

Why would somebody not want to paginate? If that happens much less often than wanting to paginate, it feels like the default should be pagination, otherwise you get unexpected silent failures.

Suppose there are 150 objects, and you do replicate.collections.list(). It returns 100 items without the user being aware that there are more.

@mattt
Copy link
Contributor Author

mattt commented Nov 12, 2023

@bfirsh For me, it's not as much a question about whether or not to paginate a list, as much as it's a matter of having control over how pagination happens.

If a user wants to fetch all of the items all at once, that's still a one-liner in Python:

collections = [collection for page in replicate.paginate(replicate.collections.list) for collection in page]

If you think about fetching much larger lists, like public models or an org's predictions, then you start to see more of the benefit to having more fine-grained control. I'm less concerned about an API consumer seeing less than expected than inadvertently downloading everything.

@mattt mattt merged commit 15f9c59 into main Nov 17, 2023
@mattt mattt deleted the mattt/paginate-method branch November 17, 2023 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants