Skip to content
Open
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
16 changes: 8 additions & 8 deletions replicate/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,20 @@ def output_iterator(self) -> Iterator[Any]:
"""

# TODO: check output is list
previous_output = self.output or []
output_offset = 0
while self.status not in ["succeeded", "failed", "canceled"]:
output = self.output or []
new_output = output[len(previous_output) :]
new_output = output[output_offset:]
yield from new_output
previous_output = output
output_offset = len(output)
time.sleep(self._client.poll_interval) # pylint: disable=no-member
self.reload()

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

output = self.output or []
new_output = output[len(previous_output) :]
new_output = output[output_offset:]
yield from new_output

async def async_output_iterator(self) -> AsyncIterator[Any]:
Expand All @@ -272,21 +272,21 @@ async def async_output_iterator(self) -> AsyncIterator[Any]:
"""

# TODO: check output is list
previous_output = self.output or []
output_offset = 0
while self.status not in ["succeeded", "failed", "canceled"]:
output = self.output or []
new_output = output[len(previous_output) :]
new_output = output[output_offset:]
for item in new_output:
yield item
previous_output = output
output_offset = len(output)
await asyncio.sleep(self._client.poll_interval) # pylint: disable=no-member
await self.async_reload()

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

output = self.output or []
new_output = output[len(previous_output) :]
new_output = output[output_offset:]
for output in new_output:
yield output

Expand Down
31 changes: 27 additions & 4 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,32 @@ async def test_run(async_flag, record_mode):


@pytest.mark.asyncio
async def test_run_with_iterator(mock_replicate_api_token):
async def test_completed_prediction_output_iterators(mock_replicate_api_token):
router = respx.Router(base_url="https://api.replicate.com/v1")
get_prediction = router.get("/predictions/p1").mock(
return_value=httpx.Response(
200, json=_prediction_with_status("succeeded", ["A", "B"])
)
)
client = Client(
api_token="test-token", transport=httpx.MockTransport(router.handler)
)
prediction = client.predictions.get("p1")
assert list(prediction.output_iterator()) == ["A", "B"]
assert [chunk async for chunk in prediction.async_output_iterator()] == ["A", "B"]
assert get_prediction.call_count == 1


@pytest.mark.parametrize("initial_output", [None, ["Hello, "]])
@pytest.mark.asyncio
async def test_run_with_iterator(mock_replicate_api_token, initial_output):
router = respx.Router(base_url="https://api.replicate.com/v1")
router.route(method="POST", path="/predictions").mock(
return_value=httpx.Response(
201,
json=_prediction_with_status("starting"),
json=_prediction_with_status(
"processing" if initial_output else "starting", initial_output
),
)
)
router.route(method="GET", path="/predictions/p1").mock(
Expand Down Expand Up @@ -124,13 +144,16 @@ async def test_run_with_iterator(mock_replicate_api_token):
assert output == ["Hello, ", "world!"]


@pytest.mark.parametrize("initial_output", [None, ["Hello, "]])
@pytest.mark.asyncio
async def test_async_run_with_iterator(mock_replicate_api_token):
async def test_async_run_with_iterator(mock_replicate_api_token, initial_output):
router = respx.Router(base_url="https://api.replicate.com/v1")
router.route(method="POST", path="/predictions").mock(
return_value=httpx.Response(
201,
json=_prediction_with_status("starting"),
json=_prediction_with_status(
"processing" if initial_output else "starting", initial_output
),
)
)
router.route(method="GET", path="/predictions/p1").mock(
Expand Down