@@ -18,7 +18,15 @@ import {
1818 type ToolResultPart ,
1919} from "../schema/index.js"
2020import type { Media } from "../media.js"
21- import { JsonObject , optionalArray , optionalNull , ProviderShared , withUntypedFrames } from "./shared.js"
21+ import {
22+ JsonObject ,
23+ lenient ,
24+ optionalArray ,
25+ optionalNull ,
26+ optionalNullish ,
27+ ProviderShared ,
28+ withUntypedFrames ,
29+ } from "./shared.js"
2230import { classifyProviderFailure } from "../provider-error.js"
2331import { effortUpdate } from "../effort-updates.js"
2432import { OpenResponsesOptions } from "./utils/open-responses-options.js"
@@ -303,21 +311,21 @@ export type OpenResponsesBody = Schema.Schema.Type<typeof OpenResponsesBody>
303311
304312export const OpenResponsesUsage = Schema . StructWithRest (
305313 Schema . Struct ( {
306- input_tokens : Schema . optional ( Schema . Number ) ,
314+ input_tokens : lenient ( Schema . Number ) ,
307315 input_tokens_details : optionalNull (
308316 Schema . StructWithRest (
309317 Schema . Struct ( {
310- cached_tokens : Schema . optional ( Schema . Number ) ,
311- cache_write_tokens : Schema . optional ( Schema . Number ) ,
318+ cached_tokens : lenient ( Schema . Number ) ,
319+ cache_write_tokens : lenient ( Schema . Number ) ,
312320 } ) ,
313321 [ JsonObject ] ,
314322 ) ,
315323 ) ,
316- output_tokens : Schema . optional ( Schema . Number ) ,
324+ output_tokens : lenient ( Schema . Number ) ,
317325 output_tokens_details : optionalNull (
318- Schema . StructWithRest ( Schema . Struct ( { reasoning_tokens : Schema . optional ( Schema . Number ) } ) , [ JsonObject ] ) ,
326+ Schema . StructWithRest ( Schema . Struct ( { reasoning_tokens : lenient ( Schema . Number ) } ) , [ JsonObject ] ) ,
319327 ) ,
320- total_tokens : Schema . optional ( Schema . Number ) ,
328+ total_tokens : lenient ( Schema . Number ) ,
321329 } ) ,
322330 [ JsonObject ] ,
323331)
@@ -331,17 +339,17 @@ type OpenResponsesUsage = Schema.Schema.Type<typeof OpenResponsesUsage>
331339export const StreamItem = Schema . StructWithRest (
332340 Schema . Struct ( {
333341 type : Schema . String ,
334- id : Schema . optional ( Schema . String ) ,
335- call_id : Schema . optional ( Schema . String ) ,
336- name : Schema . optional ( Schema . String ) ,
337- namespace : optionalNull ( Schema . String ) ,
338- arguments : Schema . optional ( Schema . String ) ,
342+ id : optionalNullish ( Schema . String ) ,
343+ call_id : optionalNullish ( Schema . String ) ,
344+ name : optionalNullish ( Schema . String ) ,
345+ namespace : optionalNullish ( Schema . String ) ,
346+ arguments : optionalNullish ( Schema . String ) ,
339347 encrypted_content : optionalNull ( Schema . String ) ,
340348 } ) ,
341349 [ Schema . Record ( Schema . String , Schema . Unknown ) ] ,
342350)
343351export type StreamItem = Schema . Schema . Type < typeof StreamItem >
344- export type OutputItem = StreamItem & { readonly id : string ; readonly namespace ?: string }
352+ export type OutputItem = StreamItem & { readonly id : string }
345353
346354// Responses-compatible providers put error details at the top level, under `error`, or under
347355// `response.error`, and gateways reshape them freely: strings, numeric codes, extra fields. Those
@@ -354,21 +362,22 @@ const asText = (value: unknown) =>
354362const TypedEvent = Schema . StructWithRest (
355363 Schema . Struct ( {
356364 type : Schema . String ,
357- delta : Schema . optional ( Schema . String ) ,
358- arguments : Schema . optional ( Schema . String ) ,
359- text : Schema . optional ( Schema . String ) ,
365+ delta : optionalNullish ( Schema . String ) ,
366+ arguments : optionalNullish ( Schema . String ) ,
367+ text : optionalNullish ( Schema . String ) ,
368+ // Kept strict: item-scoped events always carry an id, so null means a broken stream.
360369 item_id : Schema . optional ( Schema . String ) ,
361- output_index : Schema . optional ( Schema . Number ) ,
362- summary_index : Schema . optional ( Schema . Number ) ,
370+ output_index : optionalNullish ( Schema . Number ) ,
371+ summary_index : optionalNullish ( Schema . Number ) ,
363372 // OutputItemAdded/Done permit a null item in the Open Responses OpenAPI schema.
364373 item : optionalNull ( StreamItem ) ,
365- response : Schema . optional (
374+ response : optionalNullish (
366375 Schema . StructWithRest (
367376 Schema . Struct ( {
368- id : Schema . optional ( Schema . String ) ,
377+ id : optionalNullish ( Schema . String ) ,
369378 service_tier : optionalNull ( Schema . String ) ,
370- incomplete_details : optionalNull ( Schema . Struct ( { reason : Schema . optional ( Schema . String ) } ) ) ,
371- output : Schema . optional ( Schema . Array ( StreamItem ) ) ,
379+ incomplete_details : optionalNull ( Schema . Struct ( { reason : optionalNullish ( Schema . String ) } ) ) ,
380+ output : optionalNullish ( Schema . Array ( StreamItem ) ) ,
372381 usage : optionalNull ( OpenResponsesUsage ) ,
373382 error : Schema . optional ( Schema . Unknown ) ,
374383 } ) ,
@@ -854,7 +863,7 @@ export const mapUsage = (usage: OpenResponsesUsage | null | undefined, providerM
854863
855864const mapFinishReason = ( event : Event , hasFunctionCall : boolean ) : FinishReason => {
856865 const reason = event . response ?. incomplete_details ?. reason
857- if ( reason === undefined || reason === null ) {
866+ if ( reason === undefined ) {
858867 if ( hasFunctionCall ) return "tool-calls"
859868 if ( event . type === "response.incomplete" ) return "unknown"
860869 return "stop"
@@ -931,8 +940,6 @@ const resolveItem = (
931940 index : number | undefined ,
932941) : OutputItem => ( {
933942 ...item ,
934- // Some gateways serialize an absent namespace as null.
935- namespace : item . namespace ?? undefined ,
936943 id :
937944 item . id ??
938945 ( index === undefined ? undefined : state . outputItems [ index ] ) ??
0 commit comments