Skip to content

fix: emit stream finalization errors#1972

Open
HAYDEN-OAI wants to merge 2 commits into
mainfrom
codex/emit-final-stream-errors
Open

fix: emit stream finalization errors#1972
HAYDEN-OAI wants to merge 2 commits into
mainfrom
codex/emit-final-stream-errors

Conversation

@HAYDEN-OAI

Copy link
Copy Markdown
Contributor

Summary

  • route errors thrown while emitting final stream events through the normal stream error handler
  • cover malformed OpenAI-compatible streams that finish without an assistant message

Root cause

EventStream._run() handled executor rejections, but _emitFinal() ran inside a fulfilled .then() callback whose returned promise was ignored. If finalization threw, such as when no assistant message was produced, the rejection escaped instead of emitting the stream's error event.

Impact

Consumers using .on('error', ...) now receive finalization failures consistently, and awaiting done() rejects with the same error rather than leaving an unhandled rejection that can crash the process.

Validation

  • pnpm run format
  • pnpm exec tsc --noEmit
  • pnpm exec jest tests/lib/ChatCompletionStream.test.ts --runInBand
  • git diff --check

Resolves #553

@HAYDEN-OAI HAYDEN-OAI marked this pull request as ready for review July 6, 2026 18:45
@HAYDEN-OAI HAYDEN-OAI requested a review from a team as a code owner July 6, 2026 18:45
@HAYDEN-OAI HAYDEN-OAI requested a review from apcha-oai July 6, 2026 18:45
@openai-sdks

openai-sdks Bot commented Jul 6, 2026

Copy link
Copy Markdown

OkTest Summary

237/237 SDK tests passed in 10.695s for Node SDK PR #1972.

Test results — 42 files
Test Result Time
tests/chat-completions-complex-body.test.ts ✅ Passed 198ms
tests/chat-completions-create.test.ts ✅ Passed 254ms
tests/chat-completions-stream.test.ts ✅ Passed 230ms
tests/files-content-binary.test.ts ✅ Passed 168ms
tests/files-create-multipart.test.ts ✅ Passed 163ms
tests/files-list-pagination.test.ts ✅ Passed 202ms
tests/initialize-config.test.ts ✅ Passed 190ms
tests/instance-isolation.test.ts ✅ Passed 103ms
tests/models-list.test.ts ✅ Passed 133ms
tests/responses-background-lifecycle.test.ts ✅ Passed 232ms
tests/responses-body-method-errors.test.ts ✅ Passed 324ms
tests/responses-cancel-timeout.test.ts ✅ Passed 216ms
tests/responses-cancel.test.ts ✅ Passed 185ms
tests/responses-compact-retries.test.ts ✅ Passed 263ms
tests/responses-compact.test.ts ✅ Passed 289ms
tests/responses-create-advanced-stream.test.ts ✅ Passed 116ms
tests/responses-create-advanced.test.ts ✅ Passed 169ms
tests/responses-create-disconnect.test.ts ✅ Passed 1.13s
tests/responses-create-errors.test.ts ✅ Passed 230ms
tests/responses-create-malformed-api-responses.test.ts ✅ Passed 191ms
tests/responses-create-retries.test.ts ✅ Passed 311ms
tests/responses-create-stream-failures.test.ts ✅ Passed 234ms
tests/responses-create-stream-timeout.test.ts ✅ Passed 2.177s
tests/responses-create-stream-wire.test.ts ✅ Passed 2.621s
tests/responses-create-stream.test.ts ✅ Passed 80ms
tests/responses-create-terminal-states.test.ts ✅ Passed 294ms
tests/responses-create-timeout.test.ts ✅ Passed 202ms
tests/responses-create.test.ts ✅ Passed 147ms
tests/responses-delete.test.ts ✅ Passed 212ms
tests/responses-input-items-errors.test.ts ✅ Passed 207ms
tests/responses-input-items-list.test.ts ✅ Passed 168ms
tests/responses-input-items-options.test.ts ✅ Passed 215ms
tests/responses-input-tokens-count-timeout.test.ts ✅ Passed 234ms
tests/responses-input-tokens-count.test.ts ✅ Passed 241ms
tests/responses-malformed-inputs.test.ts ✅ Passed 2.293s
tests/responses-not-found-errors.test.ts ✅ Passed 248ms
tests/responses-parse.test.ts ✅ Passed 161ms
tests/responses-retrieve-retries.test.ts ✅ Passed 256ms
tests/responses-retrieve.test.ts ✅ Passed 241ms
tests/responses-stored-method-errors.test.ts ✅ Passed 600ms
tests/retry-behavior.test.ts ✅ Passed 3.011s
tests/sdk-error-shape.test.ts ✅ Passed 292ms

View OkTest run #28817115989

SDK merge (1a9d8e41924f) · head (1e9094e8c3d6) · base (95b54e589491) · OkTest (7fc645091b1a)

@HAYDEN-OAI HAYDEN-OAI changed the title [codex] emit stream finalization errors fix: emit stream finalization errors Jul 6, 2026

@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: 13a871bdba

ℹ️ 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/lib/EventStream.ts Outdated

@jbeckwith-oai jbeckwith-oai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comprehensive review completed; no blocking or nonblocking findings.

Reproduction: I applied this PR’s regression test to base commit 95b54e58 and ran jest tests/lib/ChatCompletionStream.test.ts --runInBand -t "emits finalization failures as errors". The original behavior reproduced exactly: _emitFinal() threw OpenAIError: stream ended without producing a ChatCompletionMessage with role=assistant, the error did not reach the stream error path, done() remained pending, and the test failed after the 30s timeout.

Validation on head 1e9094e8:

  • jest tests/lib/ChatCompletionStream.test.ts --runInBand: 6/6 passed, including the malformed user-role-only stream regression.
  • jest tests/lib --runInBand: 215/215 passed across 15 suites.
  • Direct EventStream probes confirmed synchronous executor throws, asynchronous executor rejections, and _emitFinal() throws each emit exactly one error, then end, and reject done() with the matching message; the success path still emits end and resolves done().
  • A throwing end listener still surfaces independently while done() remains resolved and errored remains false, so follow-up commit 1e9094e8 correctly preserves pre-existing end-listener behavior and fixes the concern raised on the first revision.
  • CI is green across Node 20/22/24/26, build, lint, examples, ecosystem, CodeQL, Agents SDK regression, and detect-breaking-changes; OkTest reports 237/237 SDK tests passed.

Compatibility: the patch changes no public API, exports, types, or data shapes. The shared EventStream change is behaviorally scoped to routing executor/finalization failures through the already documented error lifecycle. The extra microtask remains inside the existing deferred setTimeout startup and does not reorder observable stream events. Existing error normalization/cause preservation is retained. The only newly handled cases are the reported finalization rejection and synchronous executor throws; end listener exceptions are intentionally outside the handler. This is an appropriate bug fix with low breaking-change risk.

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.

on('error', () => ...) event does not fire on "OpenAIError: stream ended without producing a ChatCompletionMessage with role=assistant"

2 participants