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
8 changes: 3 additions & 5 deletions runpod/endpoint/asyncio/asyncio_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ async def stream(self) -> Any:
while True:
await asyncio.sleep(1)
stream_partial = await self._fetch_job(source="stream")
if (
stream_partial["status"] not in FINAL_STATES
or len(stream_partial.get("stream", [])) > 0
):
for chunk in stream_partial.get("stream", []):
stream_chunks = stream_partial.get("stream") or []
if stream_partial["status"] not in FINAL_STATES or len(stream_chunks) > 0:
for chunk in stream_chunks:
yield chunk["output"]
elif stream_partial["status"] in FINAL_STATES:
break
Expand Down
8 changes: 3 additions & 5 deletions runpod/endpoint/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,9 @@ def stream(self) -> Any:
while True:
time.sleep(1)
stream_partial = self._fetch_job(source="stream")
if (
stream_partial["status"] not in FINAL_STATES
or len(stream_partial["stream"]) > 0
):
for chunk in stream_partial.get("stream", []):
stream_chunks = stream_partial.get("stream") or []
if stream_partial["status"] not in FINAL_STATES or len(stream_chunks) > 0:
for chunk in stream_chunks:
yield chunk["output"]
elif stream_partial["status"] in FINAL_STATES:
break
Expand Down
39 changes: 39 additions & 0 deletions tests/test_endpoint/test_asyncio_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,45 @@ async def json_side_effect():
assert outputs == ["OUTPUT1"]
assert await job.output() == "FINAL"

async def test_stream_terminal_missing_or_none_stream(self):
"""Job.stream handles missing or None 'stream' on in-progress and terminal responses."""
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer MOCK_API_KEY",
"X-Request-ID": "job_id",
}
with patch(
"runpod.endpoint.asyncio.asyncio_runner.asyncio.sleep"
), patch(
"aiohttp.ClientSession", new_callable=AsyncMock
) as mock_session_class:
mock_session = mock_session_class.return_value
for status in ("FAILED", "TIMED_OUT", "COMPLETED"):
for terminal_payload in (
{"id": "job_id", "status": status},
{"id": "job_id", "status": status, "stream": None},
):
with self.subTest(
status=status, has_stream="stream" in terminal_payload
):
responses = [
{"status": "IN_PROGRESS"},
{"status": "IN_PROGRESS", "stream": None},
{
"status": "IN_PROGRESS",
"stream": [{"output": "OUTPUT1"}],
},
terminal_payload,
]
mock_resp = AsyncMock()
mock_resp.json.side_effect = responses
mock_session.get.return_value = mock_resp

job = Job("endpoint_id", "job_id", mock_session, headers)
outputs = [chunk async for chunk in job.stream()]
assert outputs == ["OUTPUT1"]
assert job.job_status == status

async def test_cancel(self):
"""
Tests Job.cancel
Expand Down
34 changes: 34 additions & 0 deletions tests/test_endpoint/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,37 @@ def test_output_after_stream_fetches_aggregate_result(self, mock_client):
job = runner.Job("endpoint_id", "job_id", mock_client)
self.assertEqual(list(job.stream()), ["chunk 1"])
self.assertEqual(job.output(), "final result")

@patch("runpod.endpoint.runner.time.sleep")
@patch("runpod.endpoint.runner.RunPodClient")
def test_job_stream_terminal_without_stream_key(self, mock_client, _mock_sleep):
"""Job.stream exits cleanly when a terminal response omits 'stream'."""
for status in ("FAILED", "TIMED_OUT", "COMPLETED"):
with self.subTest(status=status):
payload = {"id": "job_id", "status": status}
if status == "FAILED":
payload["error"] = "out of memory"
mock_client.get.side_effect = [payload]

job = runner.Job("endpoint_id", "job_id", mock_client)
self.assertEqual(list(job.stream()), [])
self.assertEqual(job.job_status, status)

@patch("runpod.endpoint.runner.time.sleep")
@patch("runpod.endpoint.runner.RunPodClient")
def test_job_stream_terminal_with_none_stream(self, mock_client, _mock_sleep):
"""Job.stream handles 'stream': None on in-progress and terminal responses."""
for status in ("FAILED", "TIMED_OUT", "COMPLETED"):
with self.subTest(status=status):
mock_client.get.side_effect = [
{"status": "IN_PROGRESS", "stream": None},
{
"status": "IN_PROGRESS",
"stream": [{"output": "chunk 1"}, {"output": "chunk 2"}],
},
{"id": "job_id", "status": status, "stream": None},
]

job = runner.Job("endpoint_id", "job_id", mock_client)
self.assertEqual(list(job.stream()), ["chunk 1", "chunk 2"])
self.assertEqual(job.job_status, status)