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
4 changes: 1 addition & 3 deletions src/openai/_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,8 @@ def decode(self, line: str) -> ServerSentEvent | None:
else:
self._last_event_id = value
elif fieldname == "retry":
try:
if value.isascii() and value.isdigit():
self._retry = int(value)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep oversized retry fields from aborting streams

When an SSE endpoint emits a digit-only retry field longer than CPython's configured integer-conversion limit (4,300 digits by default on supported versions), this condition passes but int(value) raises ValueError, aborting both synchronous and asynchronous stream iteration. The prior try/except safely ignored such a field, so retain conversion error handling after the ASCII-digit validation.

AGENTS.md reference: AGENTS.md:L112-L121

Useful? React with 👍 / 👎.

except (TypeError, ValueError):
pass
else:
pass # Field is ignored.

Expand Down
25 changes: 25 additions & 0 deletions tests/test_sse_retry_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pytest

from openai._streaming import SSEDecoder


@pytest.mark.parametrize("value", ["-1", "+1000", "١٠٠٠", "1.0", "1_000"])
def test_invalid_retry_value_is_ignored(value: str) -> None:
decoder = SSEDecoder()
decoder.decode("retry: 2500")
decoder.decode(f"retry: {value}")
decoder.decode("data: {}")

event = decoder.decode("")
assert event is not None
assert event.retry == 2500


def test_ascii_retry_digits_are_accepted() -> None:
decoder = SSEDecoder()
decoder.decode("retry: 0010")
decoder.decode("data: {}")

event = decoder.decode("")
assert event is not None
assert event.retry == 10