Skip to content

Commit 958ed88

Browse files
feat(api): update via SDK Studio (#17)
1 parent ffa306e commit 958ed88

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,69 @@ Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typ
7777

7878
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`.
7979

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+
async def main() -> None:
109+
all_predictions = []
110+
# Iterate through items across all pages, issuing requests as needed.
111+
async for 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)}")
127+
128+
# Remove `await` for non-async usage.
129+
```
130+
131+
Or just work directly with the returned data:
132+
133+
```python
134+
first_page = await client.predictions.list()
135+
136+
print(f"next URL: {first_page.next}") # => "next URL: ..."
137+
for prediction in first_page.results:
138+
print(prediction.id)
139+
140+
# Remove `await` for non-async usage.
141+
```
142+
80143
## Handling errors
81144

82145
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

Comments
 (0)