diff --git a/src/openai/_streaming.py b/src/openai/_streaming.py index 78e2d20aa7..63bc4147fe 100644 --- a/src/openai/_streaming.py +++ b/src/openai/_streaming.py @@ -369,7 +369,7 @@ def decode(self, line: str) -> ServerSentEvent | None: # See: https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation # noqa: E501 if not line: - if not self._event and not self._data and not self._last_event_id and self._retry is None: + if not self._event and not self._data: return None sse = ServerSentEvent( diff --git a/tests/test_streaming.py b/tests/test_streaming.py index ae6c0590f7..130f6b1151 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py @@ -58,6 +58,28 @@ def body() -> Iterator[bytes]: await assert_empty_iter(iterator) +@pytest.mark.asyncio +@pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"]) +async def test_control_only_blocks_are_skipped(sync: bool, client: OpenAI, async_client: AsyncOpenAI) -> None: + def body() -> Iterator[bytes]: + yield b"retry: 1000\n" + yield b"\n" + yield b"id: 1\n" + yield b"\n" + yield b'data: {"foo":true}\n' + yield b"\n" + yield b"\n" + + iterator = make_event_iterator(content=body(), sync=sync, client=client, async_client=async_client) + + sse = await iter_next(iterator) + assert sse.id == "1" + assert sse.retry == 1000 + assert sse.json() == {"foo": True} + + await assert_empty_iter(iterator) + + @pytest.mark.asyncio @pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"]) async def test_multiple_events(sync: bool, client: OpenAI, async_client: AsyncOpenAI) -> None: