You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The shared sync and async SSE streams currently skip [DONE] but continue reading the HTTP response until EOF. If a server leaves the connection open after this terminal event, consuming an otherwise completed stream can wait for more data or raise a later transport error.
Stop both iterators at [DONE]; their existing finally blocks close the response. Strengthen the sync regression test and add its async counterpart using a mock body that raises if another read is attempted after the terminal event. Both tests exercise the public chat completions API and assert that the response is closed.
Related Issue: Discovered through source inspection and reproduced locally; no existing issue. Security Considerations: Tests use mock HTTP responses and a dummy API key. No live model requests or credentials are included.
Type of Change
Bug fix
New feature
Breaking change
Documentation
Refactoring
Component(s) Affected
Model
Application
Common
Documentation
Tests
CI/CD
Checklist
Pre-commit hooks pass
Tests pass locally (focused suite passes; two existing full-suite failures documented below)
Documentation updated (not needed for this bug fix)
Ready for review
Testing
Python 3.12.13:
Before the fix, python -m pytest tests/test_sse.py -k done_marker -q: both sync and async regression tests fail with read beyond the [DONE] marker.
After the fix, python -m pytest tests/test_sse.py tests/test_sse_comprehensive_verify.py tests/test_transport.py tests/test_response.py tests/test_response_drift.py -q: 250 passed.
pre-commit run --all-files: all applicable hooks passed.
python -m pytest tests --ignore=tests/legacy_1x -q (API-key environment variables unset): 5628 passed, 4 skipped, 2 failed. The failures are tests/cli/test_progress.py::TestSleepHook::test_mentions_the_job_id and tests/cli/test_progress.py::TestSleepHook::test_custom_label, which expect a progress label in stderr but receive an empty string. Both also fail when the SSE implementation is temporarily restored to the unchanged upstream version. The legacy suite was not run.
git diff --check: passed.
Additional Notes
AI assistance was used for source inspection, implementation, regression tests, and validation. The tests reproduce the issue using mock transport; this is not a claim of a live-service incident.
Verdict: LGTM — the fix is correct and the two full-suite failures are unrelated to this PR.
The 2 failing tests: environment-dependent, not introduced here
TestSleepHook::test_mentions_the_job_id and test_custom_label assert the label text that rich Status renders to stderr. The tty_stderr fixture fakes isatty()=True but does not control TERM. Under TERM=dumb, rich treats the console as a dumb terminal and Status renders nothing, so the captured stderr is empty.
Reproduced locally on this branch:
TERM=dumb python -m pytest tests/cli/test_progress.py -q → exactly these 2 fail, 23 pass (matches the report)
normal TERM, API-key env unset → 25 passed
CI is green on all legs (3.9–3.13)
Optional hardening (fine as a follow-up): make the fixture hermetic with monkeypatch.setenv("TERM", "xterm-256color") so the tests don't depend on the developer's terminal.
The SSE fix
Previously [DONE] was skipped by _next_event but the loop kept reading to EOF; a server holding the connection open could stall or raise a later transport error. Breaking at DONE_MARKER in both iterators with the existing finally closing the response matches the OpenAI SSE convention (and openai-python's behavior).
The regression tests are well designed: _OpenEndedSSEBody raises if read past the terminal event, and both assert response.is_closed.
Local run on the PR branch: tests/test_sse.py → 61 passed.
Only theoretical edge: a server emitting events after [DONE] would now be truncated — that violates the convention and is handled the same way by mainstream SDKs, so not a concern.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The shared sync and async SSE streams currently skip
[DONE]but continue reading the HTTP response until EOF. If a server leaves the connection open after this terminal event, consuming an otherwise completed stream can wait for more data or raise a later transport error.Stop both iterators at
[DONE]; their existingfinallyblocks close the response. Strengthen the sync regression test and add its async counterpart using a mock body that raises if another read is attempted after the terminal event. Both tests exercise the public chat completions API and assert that the response is closed.Related Issue: Discovered through source inspection and reproduced locally; no existing issue.
Security Considerations: Tests use mock HTTP responses and a dummy API key. No live model requests or credentials are included.
Type of Change
Component(s) Affected
Checklist
Testing
Python 3.12.13:
python -m pytest tests/test_sse.py -k done_marker -q: both sync and async regression tests fail withread beyond the [DONE] marker.python -m pytest tests/test_sse.py tests/test_sse_comprehensive_verify.py tests/test_transport.py tests/test_response.py tests/test_response_drift.py -q: 250 passed.pre-commit run --all-files: all applicable hooks passed.python -m pytest tests --ignore=tests/legacy_1x -q(API-key environment variables unset): 5628 passed, 4 skipped, 2 failed. The failures aretests/cli/test_progress.py::TestSleepHook::test_mentions_the_job_idandtests/cli/test_progress.py::TestSleepHook::test_custom_label, which expect a progress label in stderr but receive an empty string. Both also fail when the SSE implementation is temporarily restored to the unchanged upstream version. The legacy suite was not run.git diff --check: passed.Additional Notes
AI assistance was used for source inspection, implementation, regression tests, and validation. The tests reproduce the issue using mock transport; this is not a claim of a live-service incident.