You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+63Lines changed: 63 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,6 +77,69 @@ Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typ
77
77
78
78
Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `basic`.
79
79
80
+
## Pagination
81
+
82
+
List methods in the Replicate Client API are paginated.
83
+
84
+
This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:
85
+
86
+
```python
87
+
from replicate import ReplicateClient
88
+
89
+
client = ReplicateClient()
90
+
91
+
all_predictions = []
92
+
# Automatically fetches more pages as needed.
93
+
for prediction in client.predictions.list():
94
+
# Do something with prediction here
95
+
all_predictions.append(prediction)
96
+
print(all_predictions)
97
+
```
98
+
99
+
Or, asynchronously:
100
+
101
+
```python
102
+
import asyncio
103
+
from replicate import AsyncReplicateClient
104
+
105
+
client = AsyncReplicateClient()
106
+
107
+
108
+
asyncdefmain() -> None:
109
+
all_predictions = []
110
+
# Iterate through items across all pages, issuing requests as needed.
111
+
asyncfor prediction in client.predictions.list():
112
+
all_predictions.append(prediction)
113
+
print(all_predictions)
114
+
115
+
116
+
asyncio.run(main())
117
+
```
118
+
119
+
Alternatively, you can use the `.has_next_page()`, `.next_page_info()`, or `.get_next_page()` methods for more granular control working with pages:
120
+
121
+
```python
122
+
first_page =await client.predictions.list()
123
+
if first_page.has_next_page():
124
+
print(f"will fetch next page using these details: {first_page.next_page_info()}")
125
+
next_page =await first_page.get_next_page()
126
+
print(f"number of items we just fetched: {len(next_page.results)}")
When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `replicate.APIConnectionError` is raised.
0 commit comments