Pagination
The SDK uses cursor-based pagination for list operations. This page covers how to work with paginated results effectively.
Basic Pagination
All list() methods return a paginated response:
from credoai import CredoAI
client = CredoAI()
# Get the first page
response = client.use_cases.list(page_limit=10)
print(f"Items: {len(response.items)}")
print(f"Has more: {response.pagination.has_more}")
print(f"Next cursor: {response.pagination.next_cursor}")
Pagination Parameters
| Parameter | Type | Description |
|---|---|---|
page_limit | int | Number of items per page (default: 50, max: 100) |
page_after | str | Cursor for the next page (from pagination.next_cursor) |
Manual Pagination
Iterate through pages manually using the cursor:
all_use_cases = []
cursor = None
while True:
response = client.use_cases.list(
page_limit=50,
page_after=cursor
)
all_use_cases.extend(response.items)
if not response.pagination.has_more:
break
cursor = response.pagination.next_cursor
print(f"Total use cases: {len(all_use_cases)}")
Automatic Pagination with list_all()
The list_all() method handles pagination automatically and returns a generator:
# Iterate through all items
for use_case in client.use_cases.list_all(page_size=50):
print(f" - {use_case.name}")
Collecting All Items
# Collect all items into a list
all_use_cases = list(client.use_cases.list_all(page_size=50))
print(f"Total: {len(all_use_cases)}")
Memory-Efficient Processing
Using list_all() as a generator is memory-efficient for large datasets:
# Process items one at a time without loading all into memory
for use_case in client.use_cases.list_all(page_size=100):
process_use_case(use_case)
Async Pagination
The async client supports async iteration:
import asyncio
from credoai import AsyncCredoAI
async def main():
async with AsyncCredoAI() as client:
# Async iteration
async for use_case in client.use_cases.list_all(page_size=50):
print(f" - {use_case.name}")
# Or collect all
all_cases = [uc async for uc in client.use_cases.list_all()]
print(f"Total: {len(all_cases)}")
asyncio.run(main())
Pagination for Relationships
Relationship resources also support pagination:
# List all models for a use case
for model in client.use_case_models.list_all(use_case_id="uc_abc123"):
print(f" - {model.name}")
# List all vendors for a model
for vendor in client.model_vendors.list_all(model_id="model_xyz789"):
print(f" - {vendor.name}")
Best Practices
Choose an Appropriate Page Size
# For quick overviews, use smaller pages
response = client.use_cases.list(page_limit=10)
# For bulk operations, use larger pages
for uc in client.use_cases.list_all(page_size=100):
process(uc)
Handle Empty Results
response = client.use_cases.list()
if not response.items:
print("No use cases found")
else:
for uc in response.items:
print(uc.name)
Early Exit
Break out of iteration when you've found what you need:
target_name = "Production Model"
for model in client.models.list_all():
if model.name == target_name:
print(f"Found: {model.id}")
break
Response Structure
The paginated response contains:
response = client.use_cases.list(page_limit=10)
# Items for this page
response.items # List[UseCaseResponse]
# Pagination metadata
response.pagination.has_more # bool
response.pagination.next_cursor # str | None
response.pagination.total_count # int | None (if available)
Next Steps
- Learn about Error Handling
- Explore Async Operations