Skip to content
Closed
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
2 changes: 1 addition & 1 deletion src/openai/_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 22 additions & 0 deletions tests/test_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down