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

fix(responses): add nullish item safety to toResponseInputItem helper

Open hsusul opened this issue 4 months ago • 0 comments

Problem

When toResponseInputItem or toResponseInputItems is passed null, undefined, a primitive, or an item object with a nullish type property (for instance, when mapping over sparse arrays or optional fields in stored response history), toResponseInputItem attempts to access item.type or execute 'created_by' in item directly. This causes an uncaught TypeError: Cannot read properties of null (reading 'type') or TypeError: Cannot use 'in' operator to search for 'created_by' in null. Additionally, ResponseStream.finalResponse() threw an error message referring to ChatCompletion instead of Response when called prior to receiving a response snapshot.

Root Cause

  1. ResponseInputItems.ts lacked a top-level nullish/object check before evaluating item.type and before invoking stripCreatedBy(item).
  2. ResponseStream.ts line 297 contained a copy-paste error in its exception message string, referencing ChatCompletion instead of Response.

Solution

  1. Added top-level nullish and non-object guards to toResponseInputItem and stripCreatedBy in src/lib/responses/ResponseInputItems.ts. Items that are nullish, primitives, or have type: null / type: undefined now safely evaluate to null so toResponseInputItems can filter them out without crashing.
  2. Corrected the exception string in ResponseStream.finalResponse() from 'stream ended without producing a ChatCompletion' to 'stream ended without producing a Response'.

Regression Coverage

  1. Added unit test in tests/lib/ResponseInputItems.test.ts verifying that null, undefined, numbers, strings, and objects with nullish type properties return null safely, and toResponseInputItems returns [].
  2. Added unit test in tests/lib/ResponseStream.test.ts verifying that finalResponse() throws 'stream ended without producing a Response' when invoked on an empty stream.

Validation

  • pnpm test (120 passed, 0 failed, 1464 tests passed)
  • pnpm build (Passed)
  • pnpm lint (Passed formatting, ESLint, TypeScript types, and publint)
  • git diff --check (Passed cleanly)

Generated Code Impact

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

hsusul avatar Jul 25 '26 18:07 hsusul