diff --git a/runpod/endpoint/asyncio/asyncio_runner.py b/runpod/endpoint/asyncio/asyncio_runner.py index e76ad197..c28f9e53 100644 --- a/runpod/endpoint/asyncio/asyncio_runner.py +++ b/runpod/endpoint/asyncio/asyncio_runner.py @@ -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 diff --git a/runpod/endpoint/runner.py b/runpod/endpoint/runner.py index 4bf6650b..422e6d07 100644 --- a/runpod/endpoint/runner.py +++ b/runpod/endpoint/runner.py @@ -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 diff --git a/tests/test_endpoint/test_asyncio_runner.py b/tests/test_endpoint/test_asyncio_runner.py index 0312df89..50569004 100644 --- a/tests/test_endpoint/test_asyncio_runner.py +++ b/tests/test_endpoint/test_asyncio_runner.py @@ -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 diff --git a/tests/test_endpoint/test_runner.py b/tests/test_endpoint/test_runner.py index bfca4001..812315f4 100644 --- a/tests/test_endpoint/test_runner.py +++ b/tests/test_endpoint/test_runner.py @@ -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)