fix(responses): add nullish item safety to toResponseInputItem helper
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
ResponseInputItems.tslacked a top-level nullish/object check before evaluatingitem.typeand before invokingstripCreatedBy(item).ResponseStream.tsline 297 contained a copy-paste error in its exception message string, referencingChatCompletioninstead ofResponse.
Solution
- Added top-level nullish and non-object guards to
toResponseInputItemandstripCreatedByinsrc/lib/responses/ResponseInputItems.ts. Items that are nullish, primitives, or havetype: null/type: undefinednow safely evaluate tonullsotoResponseInputItemscan filter them out without crashing. - Corrected the exception string in
ResponseStream.finalResponse()from'stream ended without producing a ChatCompletion'to'stream ended without producing a Response'.
Regression Coverage
- Added unit test in
tests/lib/ResponseInputItems.test.tsverifying thatnull,undefined, numbers, strings, and objects with nullishtypeproperties returnnullsafely, andtoResponseInputItemsreturns[]. - Added unit test in
tests/lib/ResponseStream.test.tsverifying thatfinalResponse()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/.