Skip to content

Commit 72dcbb9

Browse files
committed
Yield initial prediction output in polling iterators
Assisted-by: Codex Signed-off-by: jeeth-kataria <172952299+jeeth-kataria@users.noreply.github.com>
1 parent d2956ff commit 72dcbb9

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

‎replicate/prediction.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,20 @@ def output_iterator(self) -> Iterator[Any]:
250250
"""
251251

252252
# TODO: check output is list
253-
previous_output = self.output or []
253+
output_offset = 0
254254
while self.status not in ["succeeded", "failed", "canceled"]:
255255
output = self.output or []
256-
new_output = output[len(previous_output) :]
256+
new_output = output[output_offset:]
257257
yield from new_output
258-
previous_output = output
258+
output_offset = len(output)
259259
time.sleep(self._client.poll_interval) # pylint: disable=no-member
260260
self.reload()
261261

262262
if self.status == "failed":
263263
raise ModelError(self)
264264

265265
output = self.output or []
266-
new_output = output[len(previous_output) :]
266+
new_output = output[output_offset:]
267267
yield from new_output
268268

269269
async def async_output_iterator(self) -> AsyncIterator[Any]:
@@ -272,21 +272,21 @@ async def async_output_iterator(self) -> AsyncIterator[Any]:
272272
"""
273273

274274
# TODO: check output is list
275-
previous_output = self.output or []
275+
output_offset = 0
276276
while self.status not in ["succeeded", "failed", "canceled"]:
277277
output = self.output or []
278-
new_output = output[len(previous_output) :]
278+
new_output = output[output_offset:]
279279
for item in new_output:
280280
yield item
281-
previous_output = output
281+
output_offset = len(output)
282282
await asyncio.sleep(self._client.poll_interval) # pylint: disable=no-member
283283
await self.async_reload()
284284

285285
if self.status == "failed":
286286
raise ModelError(self)
287287

288288
output = self.output or []
289-
new_output = output[len(previous_output) :]
289+
new_output = output[output_offset:]
290290
for output in new_output:
291291
yield output
292292

‎tests/test_run.py‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,32 @@ async def test_run(async_flag, record_mode):
5454

5555

5656
@pytest.mark.asyncio
57-
async def test_run_with_iterator(mock_replicate_api_token):
57+
async def test_completed_prediction_output_iterators(mock_replicate_api_token):
58+
router = respx.Router(base_url="https://api.replicate.com/v1")
59+
get_prediction = router.get("/predictions/p1").mock(
60+
return_value=httpx.Response(
61+
200, json=_prediction_with_status("succeeded", ["A", "B"])
62+
)
63+
)
64+
client = Client(
65+
api_token="test-token", transport=httpx.MockTransport(router.handler)
66+
)
67+
prediction = client.predictions.get("p1")
68+
assert list(prediction.output_iterator()) == ["A", "B"]
69+
assert [chunk async for chunk in prediction.async_output_iterator()] == ["A", "B"]
70+
assert get_prediction.call_count == 1
71+
72+
73+
@pytest.mark.parametrize("initial_output", [None, ["Hello, "]])
74+
@pytest.mark.asyncio
75+
async def test_run_with_iterator(mock_replicate_api_token, initial_output):
5876
router = respx.Router(base_url="https://api.replicate.com/v1")
5977
router.route(method="POST", path="/predictions").mock(
6078
return_value=httpx.Response(
6179
201,
62-
json=_prediction_with_status("starting"),
80+
json=_prediction_with_status(
81+
"processing" if initial_output else "starting", initial_output
82+
),
6383
)
6484
)
6585
router.route(method="GET", path="/predictions/p1").mock(
@@ -124,13 +144,16 @@ async def test_run_with_iterator(mock_replicate_api_token):
124144
assert output == ["Hello, ", "world!"]
125145

126146

147+
@pytest.mark.parametrize("initial_output", [None, ["Hello, "]])
127148
@pytest.mark.asyncio
128-
async def test_async_run_with_iterator(mock_replicate_api_token):
149+
async def test_async_run_with_iterator(mock_replicate_api_token, initial_output):
129150
router = respx.Router(base_url="https://api.replicate.com/v1")
130151
router.route(method="POST", path="/predictions").mock(
131152
return_value=httpx.Response(
132153
201,
133-
json=_prediction_with_status("starting"),
154+
json=_prediction_with_status(
155+
"processing" if initial_output else "starting", initial_output
156+
),
134157
)
135158
)
136159
router.route(method="GET", path="/predictions/p1").mock(

0 commit comments

Comments
 (0)