Skip to content

Commit 74e4475

Browse files
authored
Merge pull request #42 from replicate/release-please--branches--main--changes--next
release: 2.0.0-alpha.14
2 parents 219641d + 0c9780e commit 74e4475

File tree

7 files changed

+139
-8
lines changed

7 files changed

+139
-8
lines changed

.release-please-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "2.0.0-alpha.13"
2+
".": "2.0.0-alpha.14"
33
}

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# Changelog
22

3+
## 2.0.0-alpha.14 (2025-07-31)
4+
5+
Full Changelog: [v2.0.0-alpha.13...v2.0.0-alpha.14](https://github.com/replicate/replicate-python-stainless/compare/v2.0.0-alpha.13...v2.0.0-alpha.14)
6+
7+
### Features
8+
9+
* add replicate.use() ([a7b12dd](https://github.com/replicate/replicate-python-stainless/commit/a7b12dd4bcd3a6292b6b06a59047994e408dad59))
10+
* **client:** support file upload requests ([756cad1](https://github.com/replicate/replicate-python-stainless/commit/756cad11209fa5d3661cb6fb5636660255ca7fbe))
11+
12+
13+
### Chores
14+
15+
* **docs:** document replicate.run() ([681772d](https://github.com/replicate/replicate-python-stainless/commit/681772de2e0dca70018db9cefafe37277ad0c014))
16+
* **docs:** document replicate.use() ([b47c9bd](https://github.com/replicate/replicate-python-stainless/commit/b47c9bd42e1d390ca4a652247b1b65a936529017))
17+
* make the linter happy ([70c1af2](https://github.com/replicate/replicate-python-stainless/commit/70c1af25352ce7033fb834620a48b8aff275ad65))
18+
* make the mypy happy ([24ddc92](https://github.com/replicate/replicate-python-stainless/commit/24ddc922c5ab2e446fc090f5b5f60b1689712a77))
19+
320
## 2.0.0-alpha.13 (2025-07-25)
421

522
Full Changelog: [v2.0.0-alpha.12...v2.0.0-alpha.13](https://github.com/replicate/replicate-python-stainless/compare/v2.0.0-alpha.12...v2.0.0-alpha.13)

README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,89 @@ we recommend using [python-dotenv](https://pypi.org/project/python-dotenv/)
4343
to add `REPLICATE_API_TOKEN="My Bearer Token"` to your `.env` file
4444
so that your Bearer Token is not stored in source control.
4545

46+
## Run a model
47+
48+
You can run a model synchronously using `replicate.run()`:
49+
50+
```python
51+
import replicate
52+
53+
output = replicate.run(
54+
"black-forest-labs/flux-schnell", input={"prompt": "astronaut riding a rocket like a horse"}
55+
)
56+
print(output)
57+
```
58+
59+
The `run()` method is a convenience function that creates a prediction, waits for it to complete, and returns the output. If you want more control over the prediction process, you can use the lower-level API methods.
60+
61+
### Handling errors
62+
63+
`replicate.run()` raises `ModelError` if the prediction fails. You can catch this exception to handle errors gracefully:
64+
65+
```python
66+
import replicate
67+
from replicate.exceptions import ModelError
68+
69+
try:
70+
output = replicate.run(
71+
"stability-ai/stable-diffusion-3", input={"prompt": "An astronaut riding a rainbow unicorn"}
72+
)
73+
except ModelError as e:
74+
print(f"Prediction failed: {e}")
75+
# The prediction object is available as e.prediction
76+
print(f"Prediction ID: {e.prediction.id}")
77+
print(f"Status: {e.prediction.status}")
78+
```
79+
80+
### File inputs
81+
82+
To run a model that takes file inputs, you can pass either a URL to a publicly accessible file or a file handle:
83+
84+
```python
85+
# Using a URL
86+
output = replicate.run(
87+
"andreasjansson/blip-2:f677695e5e89f8b236e52ecd1d3f01beb44c34606419bcc19345e046d8f786f9",
88+
input={"image": "https://example.com/image.jpg"},
89+
)
90+
91+
# Using a local file
92+
with open("path/to/image.jpg", "rb") as f:
93+
output = replicate.run(
94+
"andreasjansson/blip-2:f677695e5e89f8b236e52ecd1d3f01beb44c34606419bcc19345e046d8f786f9",
95+
input={"image": f},
96+
)
97+
```
98+
99+
### Wait parameter
100+
101+
By default, `replicate.run()` will wait up to 60 seconds for the prediction to complete. You can configure this timeout:
102+
103+
```python
104+
# Wait up to 30 seconds
105+
output = replicate.run("...", input={...}, wait=30)
106+
107+
# Don't wait at all - returns immediately
108+
output = replicate.run("...", input={...}, wait=False)
109+
```
110+
111+
When `wait=False`, the method returns immediately after creating the prediction, and you'll need to poll for the result manually.
112+
113+
## Run a model and stream its output
114+
115+
For models that support streaming (particularly language models), you can use `replicate.stream()`:
116+
117+
```python
118+
import replicate
119+
120+
for event in replicate.stream(
121+
"meta/meta-llama-3-70b-instruct",
122+
input={
123+
"prompt": "Please write a haiku about llamas.",
124+
},
125+
):
126+
print(str(event), end="")
127+
```
128+
46129
## Async usage
47130

48131
Simply import `AsyncReplicate` instead of `Replicate` and use `await` with each API call:
@@ -69,6 +152,34 @@ asyncio.run(main())
69152

70153
Functionality between the synchronous and asynchronous clients is otherwise identical.
71154

155+
### Async run() and stream()
156+
157+
The async client also supports `run()` and `stream()` methods:
158+
159+
```python
160+
import asyncio
161+
from replicate import AsyncReplicate
162+
163+
replicate = AsyncReplicate()
164+
165+
166+
async def main():
167+
# Run a model
168+
output = await replicate.run(
169+
"black-forest-labs/flux-schnell", input={"prompt": "astronaut riding a rocket like a horse"}
170+
)
171+
print(output)
172+
173+
# Stream a model's output
174+
async for event in replicate.stream(
175+
"meta/meta-llama-3-70b-instruct", input={"prompt": "Write a haiku about coding"}
176+
):
177+
print(str(event), end="")
178+
179+
180+
asyncio.run(main())
181+
```
182+
72183
### With aiohttp
73184

74185
By default, the async client uses `httpx` for HTTP requests. However, for improved concurrency performance you may also use `aiohttp` as the HTTP backend.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "replicate"
3-
version = "2.0.0-alpha.13"
3+
version = "2.0.0-alpha.14"
44
description = "The official Python library for the replicate API"
55
dynamic = ["readme"]
66
license = "Apache-2.0"

src/replicate/_base_client.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,10 @@ def _build_request(
532532
is_body_allowed = options.method.lower() != "get"
533533

534534
if is_body_allowed:
535-
kwargs["json"] = json_data if is_given(json_data) else None
535+
if isinstance(json_data, bytes):
536+
kwargs["content"] = json_data
537+
else:
538+
kwargs["json"] = json_data if is_given(json_data) else None
536539
kwargs["files"] = files
537540
else:
538541
headers.pop("Content-Type", None)

src/replicate/_files.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ def _transform_file(file: FileTypes) -> HttpxFileTypes:
6969
return file
7070

7171
if is_tuple_t(file):
72-
return (file[0], _read_file_content(file[1]), *file[2:])
72+
return (file[0], read_file_content(file[1]), *file[2:])
7373

7474
raise TypeError(f"Expected file types input to be a FileContent type or to be a tuple")
7575

7676

77-
def _read_file_content(file: FileContent) -> HttpxFileContent:
77+
def read_file_content(file: FileContent) -> HttpxFileContent:
7878
if isinstance(file, os.PathLike):
7979
return pathlib.Path(file).read_bytes()
8080
return file
@@ -111,12 +111,12 @@ async def _async_transform_file(file: FileTypes) -> HttpxFileTypes:
111111
return file
112112

113113
if is_tuple_t(file):
114-
return (file[0], await _async_read_file_content(file[1]), *file[2:])
114+
return (file[0], await async_read_file_content(file[1]), *file[2:])
115115

116116
raise TypeError(f"Expected file types input to be a FileContent type or to be a tuple")
117117

118118

119-
async def _async_read_file_content(file: FileContent) -> HttpxFileContent:
119+
async def async_read_file_content(file: FileContent) -> HttpxFileContent:
120120
if isinstance(file, os.PathLike):
121121
return await anyio.Path(file).read_bytes()
122122

src/replicate/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
__title__ = "replicate"
4-
__version__ = "2.0.0-alpha.13" # x-release-please-version
4+
__version__ = "2.0.0-alpha.14" # x-release-please-version

0 commit comments

Comments
 (0)