openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

fix(responses): correct error message in finalResponse and safely accumulate string deltas

Open hsusul opened this issue 4 months ago • 0 comments

  • [x] I understand that this repository is auto-generated and my pull request may not be merged

Changes being requested

  • Fix error message thrown by ResponseStream.finalResponse() when a stream ends prematurely without producing a response (stream ended without producing a Response instead of stream ended without producing a ChatCompletion).
  • Fix typo in ResponseStream.finalResponse() docstring (REsponse -> Response).
  • Safely handle nullish initial values when accumulating string deltas in ResponseAccumulator.ts across output_text, refusal, function_call_arguments, reasoning_text, reasoning_summary_text, custom_tool_call_input, and mcp_call_arguments events using (target ?? '') + event.delta.
  • Add unit tests for finalResponse() error message and ResponseAccumulator string delta accumulation.

Additional context & links

Problem

  1. ResponseStream belongs to the Responses API. When finalResponse() is called on a stream that ended prematurely without producing a Response, it previously threw OpenAIError('stream ended without producing a ChatCompletion').
  2. When stream delta events (response.function_call_arguments.delta, response.refusal.delta, etc.) arrive on uninitialized string properties, += event.delta resulted in "undefined" prefixing the delta content.

Root cause

  1. Copy-paste typo from ChatCompletionStream.ts in ResponseStream.ts.
  2. Missing nullish fallback (str ?? '') on string property delta accumulation in ResponseAccumulator.ts.

Verification

  • pnpm test tests/lib/ResponseStream.test.ts (Pass)
  • pnpm build (Pass)
  • pnpm lint (Pass)

hsusul avatar Jul 27 '26 13:07 hsusul