Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions replicate/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ def create( # type: ignore
webhook: Optional[str] = None,
webhook_completed: Optional[str] = None,
webhook_events_filter: Optional[List[str]] = None,
*,
stream: Optional[bool] = None,
**kwargs,
) -> Prediction:
"""
Expand Down Expand Up @@ -157,6 +159,8 @@ def create( # type: ignore
body["webhook_completed"] = webhook_completed
if webhook_events_filter is not None:
body["webhook_events_filter"] = webhook_events_filter
if stream is True:
body["stream"] = "true"

resp = self._client._request(
"POST",
Expand Down
49 changes: 49 additions & 0 deletions tests/test_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,55 @@ def test_cancel():
assert rsp.call_count == 1


@responses.activate
def test_stream():
client = create_client()
version = create_version(client)

rsp = responses.post(
"https://api.replicate.com/v1/predictions",
match=[
matchers.json_params_matcher(
{
"version": "v1",
"input": {"text": "world"},
"stream": "true",
}
),
],
json={
"id": "p1",
"version": "v1",
"urls": {
"get": "https://api.replicate.com/v1/predictions/p1",
"cancel": "https://api.replicate.com/v1/predictions/p1/cancel",
"stream": "https://streaming.api.replicate.com/v1/predictions/p1",
},
"created_at": "2022-04-26T20:00:40.658234Z",
"completed_at": "2022-04-26T20:02:27.648305Z",
"source": "api",
"status": "processing",
"input": {"text": "world"},
"output": None,
"error": None,
"logs": "",
},
)

prediction = client.predictions.create(
version=version,
input={"text": "world"},
stream=True,
)

assert rsp.call_count == 1

assert (
prediction.urls["stream"]
== "https://streaming.api.replicate.com/v1/predictions/p1"
)


@responses.activate
def test_async_timings():
client = create_client()
Expand Down