Skip to content

fix(streaming): validate SSE retry field per spec instead of leniently int()-ing it - #3844

Open
adhavan18 wants to merge 1 commit into
openai:mainfrom
adhavan18:fix-sse-retry-validation
Open

fix(streaming): validate SSE retry field per spec instead of leniently int()-ing it#3844
adhavan18 wants to merge 1 commit into
openai:mainfrom
adhavan18:fix-sse-retry-validation

Conversation

@adhavan18

Copy link
Copy Markdown

Description

SSEDecoder.decode() used int(value) for the retry field, which accepts -1, +1000, leading/trailing whitespace, and other forms the SSE spec doesn't allow. Per the spec, the retry field is only valid when it consists entirely of ASCII digits.

retry: -1 was silently accepted as retry == -1 instead of the field being ignored.

Fix: validate with value.isascii() and value.isdigit() before assigning, matching the spec's [0-9]+ grammar exactly instead of parsing leniently.

Closes #3834

Verification

Added test_sse_decoder_ignores_invalid_retry_value (parametrized over -1, +1000, 1.5, leading/trailing whitespace, 1e3, and empty) and test_sse_decoder_accepts_valid_retry_value directly against SSEDecoder.decode(). Confirmed 4 of the invalid-value cases genuinely fail on unfixed main (-1, +1000, and the two whitespace variants all currently parse successfully) and pass with the fix. Full tests/test_streaming.py suite: 28 passed.

…y int()-ing it

int() accepts -1, +1000, leading/trailing whitespace, and other forms the
SSE spec doesn't allow for the retry field, only ASCII digits are valid.
retry: -1 was silently accepted as a negative reconnection time instead of
being ignored.

Validate with value.isascii() and value.isdigit() before assigning, matching
the spec's [0-9]+ grammar exactly.
@adhavan18
adhavan18 requested a review from a team as a code owner September 10, 2026 14:43

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7cb43a2af3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/openai/_streaming.py
# of ASCII digits; anything else (a sign, whitespace, a decimal point)
# must be ignored rather than parsed leniently by int().
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 Ignore unrepresentable retry values

On the default supported CPython configuration, an otherwise valid ASCII-digit retry value of 4,301+ digits passes this predicate but int(value) raises ValueError because of Python’s integer-string conversion limit, aborting both sync and async SSE iteration. The prior try/except safely ignored this field, so preserve that handling after validation rather than letting a large streaming line terminate the response.

AGENTS.md reference: AGENTS.md:L114-L121

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSE decoder accepts invalid retry field values

1 participant