Skip to content

Read the Responses event type from the SSE event field - #8608

Merged
danielhanchen merged 5 commits into
mainfrom
fix-responses-sse-event-name
Aug 13, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
fix-responses-sse-event-name

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Two suites are red on origin/main today, and every open PR inherits them:

FAILED tests/test_gemini_provider.py::test_openai_responses_forced_function_tool_choice_drops_hosted_tools
FAILED tests/test_external_provider_usage_chunk.py::test_other_providers_do_not_get_the_continuation_flags[openai-...]
        ValueError: Responses event type is missing

response_event_type() arrived with #8511 and raises when neither the JSON object nor the SSE event name carries a type. It takes the event name as its second argument for exactly that reason, and openai_codex_client.py passes it. The external-provider Responses loop did not: it discarded event: lines outright and then passed only the parsed object, so a perfectly standard frame

event: response.completed
data: {}

raised and killed the whole completion.

Two changes:

  1. Keep the SSE event name and pass it to response_event_type(). The Responses API puts the type there and need not repeat it in the data object.
  2. When the type still cannot be determined, skip that frame instead of failing the stream. An OpenAI-compatible endpoint behind this base URL can answer a Responses request with Chat Completions frames, which is exactly what the second failing test serves, and skipping is the behaviour that existed before Unsloth Studio: add ChatGPT subscription chat with Codex tools #8511. The ChatGPT path keeps raising, since the wire shape is guaranteed there.

Verification, and one part worth reading:

  • Both suites: 177 passed, from 2 failed on the merge base.
  • Reverting change 2 fails test_other_providers_do_not_get_the_continuation_flags.
  • Reverting change 1 initially passed, because change 2 swallows it. That is a real gap, not a technicality: with only the skip in place, an event: response.completed frame is silently dropped and its usage never surfaces. test_a_type_carried_only_by_the_sse_event_field_is_honoured now pins it by asserting the usage chunk appears, and reverting change 1 fails it.
  • Wider run across the provider, auto-switch and desktop-auth suites: 686 passed. The 4 remaining desktop-auth failures are unrelated, 3 are a local diceware gap and the fourth is the routes-stub drift covered by Fix the desktop-auth routes stub so the health test runs again #8590.

#8511 added response_event_type(), which raises when neither the JSON nor the
SSE name carries a type. The external-provider Responses loop discarded
`event:` lines outright and passed only the parsed object, so a standard
`event: response.completed` + `data: {}` frame raised and killed the whole
completion. Both failing suites are red on main today.

Two changes: keep the SSE name and pass it, and skip a frame whose type still
cannot be determined instead of failing the stream. An OpenAI-compatible
endpoint behind this base URL can answer a Responses request with Chat
Completions frames, which is what the second failing test serves; skipping is
the pre-existing behaviour. The ChatGPT path keeps raising, where the wire
shape is guaranteed.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@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: c00d608686

ℹ️ 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 on lines +5518 to +5521
# behaviour and beats failing the whole completion; the
# ChatGPT path stays strict, where the shape is guaranteed.
sse_event_name = ""
continue

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 Preserve Chat Completions frames instead of discarding them

When an OpenAI-compatible base URL responds to the Responses request with Chat Completions SSE, every content frame has choices but no Responses type, so this handler discards every token and eventually forwards only [DONE], presenting a successful but empty assistant response. The compatibility scenario described here therefore needs to forward or translate the Chat Completions frames rather than merely swallowing the validation error; the existing test at test_external_provider_usage_chunk.py:564-589 exercises exactly this response shape but never asserts that "ok" survives.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not taking this one. Two points:

  1. The empty-response behaviour is pre-existing, not introduced here. Before Unsloth Studio: add ChatGPT subscription chat with Codex tools #8511 the loop read event_type = event.get("type") and every branch below is an if event_type == ..., so a Chat Completions frame already fell through and was dropped. This PR restores exactly that; Unsloth Studio: add ChatGPT subscription chat with Codex tools #8511 turned the drop into a ValueError that killed the stream instead.

  2. The cited test at test_external_provider_usage_chunk.py:564-589 is test_streamed_usage_is_requested_only_where_documented, which drives stream_chat_completion, and openai is deliberately absent from its parametrization, so it never reaches the Responses handler.

Translating Chat Completions frames inside the Responses loop, or detecting the shape and re-routing, is a separate feature and a bigger change than a red-suite fix should carry. Filed as follow-up work.

An event name applies to its own event only. A named event whose data line is
empty or unparseable left the name set, so the next data-only frame inherited
it: a stale response.failed turned that frame into a 502 and broke the stream.
Clearing at the blank-line delimiter is the single place an event can end, so
the two per-frame resets go away.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 9ad6898983

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member Author

Cross-check, because this PR and the other one each fix half of the same main-wide red and neither can go green on its own. #8590's own CI still shows the Responses event type is missing failures, and #8608's still shows the openai_codex_auth_router ImportError, which makes each look incomplete when it is not.

Applied both on top of origin/main (dac7b51a0) in a clean worktree. They touch disjoint files and both apply with no conflict:

studio/backend/tests/test_desktop_auth.py                 (#8590)
studio/backend/core/inference/external_provider.py        (#8608)
studio/backend/tests/test_external_provider_usage_chunk.py (#8608)

Running the exact tests that are red across the repo:

tests/test_desktop_auth.py::test_health_response_reports_desktop_capability_fields
tests/test_external_provider_usage_chunk.py::test_other_providers_do_not_get_the_continuation_flags[openai-...]
tests/test_gemini_provider.py::test_openai_responses_forced_function_tool_choice_drops_hosted_tools
tests/test_warm_window_review_fixes.py

103 passed.

For the record, the third family seen on some boards is not part of this: test_warm_window_review_fixes.py::test_the_warm_epoch_is_retired_before_any_shutdown_await (asserting 781 < 771) passes on clean main unmodified. It is a line-number-coupled structural assertion, so it moves with whatever shifts lines in the file it inspects, and it is not caused by either PR.

Together these unblock #8440, #8489, #8496, #8500, #8548 and #8576, which are all red only on this shared set. They should land as a pair.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Aug 13, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

Cross-platform CI and a real before/after run, plus one correction to how this PR should be described.

Cross-OS

Replicated onto a staging repo so the org queue is spared, and run on the OS triple. I read the logs rather than trusting the green, because the generated per-OS jobs treat pytest exit 5 ("no tests ran") as a pass, which is exactly how a job goes vacuously green:

OS result
ubuntu-latest success, 23 passed
macos-14 success, 23 passed
windows-latest success, 23 passed

Real pass counts on all three, so these are genuine gates.

Before/after, two isolated installs

BEFORE is the merge base dac7b51a, AFTER is this PR's head e1dd893d. Never main, and never one install re-photographed. Two separate UNSLOTH_STUDIO_HOMEs, each venv's editable finder verified to point at its own worktree, and each listening port mapped back to its home from /proc/<pid>/environ before any screenshot. Each side got its own stub server, so every request is attributable to one side, and both stubs received the same 7 identical requests.

What the two servers logged:

BEFORE AFTER
external_provider.stream_error: "Responses event type is missing" 6 0
completions 0 6
usage chunk absent input_tokens=11, output_tokens=10

Visually: both sides carry the same model chip and the same prompt. BEFORE shows a "Generation failed / An internal error occurred" toast and a red error bubble with Retry where the assistant reply belongs. AFTER shows the completed reply at 1.60s. The stub's output carries a marker token specifically so a locally loaded model answering the same question could not be mistaken for a pass.

A scripted reproduction driving the real _stream_openai_responses agrees: merge base raises ValueError: Responses event type is missing with 0 chunks and no text; head returns the full text, [DONE], and 1 usage chunk.

Browsers

Same scene on both sides, five browsers, with the BEFORE column as the control:

Browser BEFORE AFTER
Chromium fails completes
Firefox fails completes
WebKit fails completes
Google Chrome (real system binary) fails completes
Microsoft Edge (real system binary) fails completes

5/5 fail before, 5/5 pass after. The control column is what makes this the backend fix rather than a browser quirk. Safari proper is not covered and I am not claiming it: WebKit is Safari's engine, not Safari, and Safari needs macOS.

Correction to the framing

response_event_type(event, event_name) already accepted the SSE event name on the merge base, and openai_codex_client.py already passed it. Only external_provider.py did not. So this PR closes a call-site gap, it does not add a new capability. That is a smaller and more accurate claim than "read the event type from the SSE field" implies.

Gaps, stated plainly

Staging runners are GPU-less, and the per-OS jobs scope to this PR's own test files rather than the full backend suite. The staging Playwright job is not a gate (its smoke step is || true), so the browser evidence above is local, not CI. Two incidental differences between the halves are local state, not code: BEFORE's Recents list holds three threads from earlier iterations against one in AFTER, and BEFORE's prompt bubble sits above the error in the scroll.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: e1dd893dcb

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member Author

CI status, since the red checks here are not this PR's.

Measured on main's own Backend CI at 75d983dcf (#8590's merge commit, no PR applied), main has 10 failures in two families: 8 in TestEstimateGgufRequiredGb, all off by the same 0.575732421875 GiB, and 2 ValueError: Responses event type is missing.

This PR clears the Responses family completely. The 8 that remain here are the GGUF family, which #8635 fixes. #8635 shows the mirror image: 0 GGUF failures, and the same 2 Responses failures this PR removes.

So the two are complementary and mutually blocking. Merging both should take Backend CI to zero.

The pip scan-packages :: studio red is not a finding. The job ends with exit code 143 and The runner has received a shutdown signal about 12 minutes into the scan, having emitted no findings. The same shard set passes on #8440 and #8489.

@danielhanchen
danielhanchen merged commit b337630 into main Aug 13, 2026
39 of 45 checks passed
@danielhanchen
danielhanchen deleted the fix-responses-sse-event-name branch August 13, 2026 06:40
danielhanchen added a commit that referenced this pull request Aug 13, 2026
* Surface an untyped error frame instead of skipping it

#8608 made the Responses reader skip a frame it cannot type, which is right for the
case it was written for: an OpenAI-compatible endpoint behind a Responses base URL
answering with Chat Completions chunks. An error payload is the exception. The bare
{"error": {...}} such a proxy emits mid-stream carries no type and no SSE event name
either, so it took the same path, and the generation ended with zero chunks and
nothing to explain it -- a blank answer where the endpoint had said "rate limited".
Measured on main: 0 chunks returned, no mention of the error.

Before the skip existed that frame raised, which was at least loud. This says what
happened instead, through the same _error_sse_line the typed response.failed and
error events already use. A frame with no type, no event name and no error key is
still skipped.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Check the frame is an object before reading its error field

data: null and data: [] are valid JSON and not dicts. response_event_type
rejects them, and the error check that followed then called .get() on them,
which raised AttributeError and killed the stream -- the exact failure the skip
exists to avoid. Guard on the type first.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Shorten the comments on the untyped error frame path

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

1 participant