fix(responses): safely initialize reasoning summary array during event accumulation - #2018
Open
hsusul wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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, andresponse.reasoning_summary_text.done) on reasoning output items wheresummarywas omitted or uninitialized (e.g. when an output item of typereasoningis added without a pre-initializedsummaryarray) causes an uncaught JavaScript runtime error:TypeError: Cannot read properties of undefined (reading 'push')orTypeError: Cannot read properties of undefined (reading '0').Root Cause
ResponseAccumulator.tsattempted to calloutput.summary.push(...)inresponse.reasoning_summary_part.addedwithout checking ifoutput.summarywas defined, unlikeresponse.content_part.addedwhich explicitly initializesoutput.content = []when nullish.response.reasoning_summary_part.done,response.reasoning_summary_text.delta, andresponse.reasoning_summary_text.donepassedoutput.summarydirectly togetContent(output.summary, event.summary_index). Whenoutput.summarywas undefined,getContentaccessedcontent[contentIndex], throwing an unhandledTypeError.Solution
if (!output.summary) output.summary = [];) toresponse.reasoning_summary_part.added,response.reasoning_summary_part.done,response.reasoning_summary_text.delta, andresponse.reasoning_summary_text.doneinsrc/lib/responses/ResponseAccumulator.ts.getContentto check for nullishcontentarrays and throw a clearOpenAIErrorinstead of throwing a raw JavaScriptTypeError.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 uninitializedsummaryarrays safely accumulatesummary_textparts 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/andtests/lib/.