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
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"

1 participant