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

fix(responses): safely initialize reasoning summary array during event accumulation

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

Problem

When processing streaming events in accumulateResponse, accumulating reasoning summary events (response.reasoning_summary_part.added, response.reasoning_summary_part.done, response.reasoning_summary_text.delta, and response.reasoning_summary_text.done) on reasoning output items where summary was omitted or uninitialized (e.g. when an output item of type reasoning is added without a pre-initialized summary array) causes an uncaught JavaScript runtime error: TypeError: Cannot read properties of undefined (reading 'push') or TypeError: Cannot read properties of undefined (reading '0').

Root Cause

  1. ResponseAccumulator.ts attempted to call output.summary.push(...) in response.reasoning_summary_part.added without checking if output.summary was defined, unlike response.content_part.added which explicitly initializes output.content = [] when nullish.
  2. response.reasoning_summary_part.done, response.reasoning_summary_text.delta, and response.reasoning_summary_text.done passed output.summary directly to getContent(output.summary, event.summary_index). When output.summary was undefined, getContent accessed content[contentIndex], throwing an unhandled TypeError.

Solution

  1. Added nullish array initialization (if (!output.summary) output.summary = [];) to response.reasoning_summary_part.added, response.reasoning_summary_part.done, response.reasoning_summary_text.delta, and response.reasoning_summary_text.done in src/lib/responses/ResponseAccumulator.ts.
  2. Updated getContent to check for nullish content arrays and throw a clear OpenAIError instead of throwing a raw JavaScript TypeError.

Regression Coverage

Added a unit test in tests/lib/ResponseAccumulator.test.ts ('handles reasoning summary events on output items with uninitialized summary') verifying that reasoning output items with uninitialized summary arrays safely accumulate summary_text parts and deltas.

Validation

  • npx jest tests/lib/ResponseAccumulator.test.ts (Passed 8/8)
  • npx jest tests/lib (Passed 16/16 test suites, 400/400 tests)
  • pnpm build (Passed with zero errors)
  • pnpm lint (Passed formatting, ESLint, and TypeScript type checks)
  • git diff --check (Passed cleanly)

Generated Code Impact

None. Changes are strictly confined to manually maintained files under src/lib/responses/ and tests/lib/.

hsusul avatar Jul 26 '26 16:07 hsusul