Skip to content

fix(stream): handle audio completion markers#1991

Merged
HAYDEN-OAI merged 2 commits into
mainfrom
dev/hayden/issue-1206-audio-stream-finalization
Jul 22, 2026
Merged

fix(stream): handle audio completion markers#1991
HAYDEN-OAI merged 2 commits into
mainfrom
dev/hayden/issue-1206-audio-stream-finalization

Conversation

@HAYDEN-OAI

Copy link
Copy Markdown
Contributor
  • I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

  • accumulate streamed chat-completion audio fields instead of replacing earlier id, data, and transcript fragments with each later delta
  • allow finalChatCompletion() to finalize audio streams that end with a terminal expires_at-only chunk and no separate finish_reason
  • keep the existing missing finish_reason error for non-audio, incomplete, malformed, or continued-after-expiry streams
  • add regression coverage for the reported audio chunk sequence and the safety boundaries

Additional context & links

Fixes #1206.

gpt-4o-audio-preview can finish streaming with an audio delta containing only expires_at. The stream helper currently requires an explicit finish_reason, so finalChatCompletion() throws even though the audio response is complete. The existing generic delta merge also overwrites earlier audio fragments, leaving only the final audio object.

This change tracks whether the last meaningful choice delta is an expires_at-only audio completion marker and only infers finish_reason: stop when the accumulated audio has all required fields. It does not add a general missing-finish fallback.

Verification:

  • pnpm test --runInBand tests/lib/ChatCompletionStream.test.ts
  • pnpm lint
  • pnpm test --runInBand

@HAYDEN-OAI
HAYDEN-OAI marked this pull request as ready for review July 13, 2026 20:12
@HAYDEN-OAI
HAYDEN-OAI requested a review from a team as a code owner July 13, 2026 20:12
@openai-sdks

openai-sdks Bot commented Jul 13, 2026

Copy link
Copy Markdown

OkTest Summary

237/237 SDK tests passed in 10.62s for Node SDK PR #1991.

Test results — 42 files
Test Result Time
tests/chat-completions-complex-body.test.ts ✅ Passed 151ms
tests/chat-completions-create.test.ts ✅ Passed 206ms
tests/chat-completions-stream.test.ts ✅ Passed 123ms
tests/files-content-binary.test.ts ✅ Passed 159ms
tests/files-create-multipart.test.ts ✅ Passed 159ms
tests/files-list-pagination.test.ts ✅ Passed 176ms
tests/initialize-config.test.ts ✅ Passed 171ms
tests/instance-isolation.test.ts ✅ Passed 105ms
tests/models-list.test.ts ✅ Passed 122ms
tests/responses-background-lifecycle.test.ts ✅ Passed 242ms
tests/responses-body-method-errors.test.ts ✅ Passed 357ms
tests/responses-cancel-timeout.test.ts ✅ Passed 252ms
tests/responses-cancel.test.ts ✅ Passed 208ms
tests/responses-compact-retries.test.ts ✅ Passed 224ms
tests/responses-compact.test.ts ✅ Passed 195ms
tests/responses-create-advanced-stream.test.ts ✅ Passed 111ms
tests/responses-create-advanced.test.ts ✅ Passed 311ms
tests/responses-create-disconnect.test.ts ✅ Passed 1.061s
tests/responses-create-errors.test.ts ✅ Passed 205ms
tests/responses-create-malformed-api-responses.test.ts ✅ Passed 141ms
tests/responses-create-retries.test.ts ✅ Passed 284ms
tests/responses-create-stream-failures.test.ts ✅ Passed 192ms
tests/responses-create-stream-timeout.test.ts ✅ Passed 2.183s
tests/responses-create-stream-wire.test.ts ✅ Passed 2.548s
tests/responses-create-stream.test.ts ✅ Passed 140ms
tests/responses-create-terminal-states.test.ts ✅ Passed 258ms
tests/responses-create-timeout.test.ts ✅ Passed 219ms
tests/responses-create.test.ts ✅ Passed 203ms
tests/responses-delete.test.ts ✅ Passed 236ms
tests/responses-input-items-errors.test.ts ✅ Passed 166ms
tests/responses-input-items-list.test.ts ✅ Passed 188ms
tests/responses-input-items-options.test.ts ✅ Passed 207ms
tests/responses-input-tokens-count-timeout.test.ts ✅ Passed 238ms
tests/responses-input-tokens-count.test.ts ✅ Passed 231ms
tests/responses-malformed-inputs.test.ts ✅ Passed 2.274s
tests/responses-not-found-errors.test.ts ✅ Passed 280ms
tests/responses-parse.test.ts ✅ Passed 169ms
tests/responses-retrieve-retries.test.ts ✅ Passed 274ms
tests/responses-retrieve.test.ts ✅ Passed 199ms
tests/responses-stored-method-errors.test.ts ✅ Passed 644ms
tests/retry-behavior.test.ts ✅ Passed 3.213s
tests/sdk-error-shape.test.ts ✅ Passed 295ms

View OkTest run #29772649840

SDK merge (c0d803b02d59) · head (5ee5d86efdb8) · base (39a15b412fc1) · OkTest (fd7a761c81c1)

@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.

Reviewed head 5ee5d86. The audio fields are accumulated correctly, inferred completion is narrowly gated on a complete audio payload plus a terminal expires_at-only delta, and later deltas invalidate that marker. I also checked multi-choice behavior, explicit finish reasons, and incomplete audio. Local targeted tests, four additional boundary probes, the full test suite (1,198 passed), pnpm lint, and pnpm build all passed; GitHub CI is green.

@HAYDEN-OAI
HAYDEN-OAI merged commit bd86817 into main Jul 22, 2026
19 checks passed
@stainless-app stainless-app Bot mentioned this pull request Jul 22, 2026
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.

openai.beta.chat.completions.stream(chatRequest).finalChatCompletion() throws missing finish_reason error for gpt-4o-audio-preview streaming.

2 participants