Skip to content

Commit 60a5a6c

Browse files
authored
fix(ai): treat null Responses stream fields as absent (#56669)
1 parent 63c1a27 commit 60a5a6c

3 files changed

Lines changed: 91 additions & 33 deletions

File tree

‎packages/ai/src/protocols/open-responses.ts‎

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ import {
1818
type ToolResultPart,
1919
} from "../schema/index.js"
2020
import 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"
2230
import { classifyProviderFailure } from "../provider-error.js"
2331
import { effortUpdate } from "../effort-updates.js"
2432
import { OpenResponsesOptions } from "./utils/open-responses-options.js"
@@ -303,21 +311,21 @@ export type OpenResponsesBody = Schema.Schema.Type<typeof OpenResponsesBody>
303311

304312
export 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>
331339
export 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
)
343351
export 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) =>
354362
const 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

855864
const 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]) ??

‎packages/ai/src/protocols/shared.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Tool } from "@opencode/schema/tool"
2-
import { Effect, Option, Schema, SchemaGetter } from "effect"
2+
import { Effect, Option, Predicate, Schema, SchemaGetter, SchemaTransformation } from "effect"
33
import { Headers, HttpClientRequest } from "effect/http"
44
import { Media } from "../media.js"
55
import {
@@ -27,6 +27,17 @@ const isJson = Schema.is(Schema.Json)
2727
export const JsonObject = Schema.Record(Schema.String, Schema.Unknown)
2828
export const optionalArray = <const S extends Schema.Top>(schema: S) => Schema.optional(Schema.Array(schema))
2929
export const optionalNull = <const S extends Schema.Top>(schema: S) => Schema.optional(Schema.NullOr(schema))
30+
/** Optional provider field where an explicit `null` means absent, so decoding never yields `null`. */
31+
export const optionalNullish = <const S extends Schema.Top>(schema: S) =>
32+
Schema.optional(Schema.NullOr(schema)).pipe(
33+
Schema.decodeTo(
34+
Schema.optional(Schema.toType(schema)),
35+
SchemaTransformation.transformOptional({
36+
decode: Option.filter(Predicate.isNotNullish),
37+
encode: (value) => value,
38+
}),
39+
),
40+
)
3041
/** Optional field whose malformed value decodes to `undefined` instead of failing the enclosing struct. */
3142
export const lenient = <const S extends Schema.Top>(schema: S) =>
3243
Schema.optionalKey(

‎packages/ai/test/provider/openai-compatible-responses.test.ts‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -644,24 +644,64 @@ describe("Open Responses-compatible route", () => {
644644
}),
645645
)
646646

647-
it.effect("treats a null function call namespace as absent", () =>
647+
it.effect("treats null optional fields as absent", () =>
648648
Effect.gen(function* () {
649-
const item = { type: "function_call", id: "fc_1", call_id: "call_1", name: "lookup", namespace: null }
649+
const call = { type: "function_call", id: null, call_id: "call_1", name: "lookup", namespace: null }
650+
const message = { type: "message", id: "msg_1", role: "assistant" }
650651
const response = yield* LLMClient.generate(request).pipe(
651652
Effect.provide(
652653
fixedResponse(
653654
sseEvents(
654-
{ type: "response.output_item.added", output_index: 0, item: { ...item, arguments: "" } },
655-
{ type: "response.function_call_arguments.done", item_id: "fc_1", output_index: 0, arguments: "{}" },
656-
{ type: "response.output_item.done", output_index: 0, item: { ...item, arguments: "{}" } },
657-
{ type: "response.completed", response: { id: "resp_1", output: [{ ...item, arguments: "{}" }] } },
655+
{ type: "response.created", response: null },
656+
{ type: "response.output_item.added", output_index: 0, item: { ...message, content: [] } },
657+
{ type: "response.output_text.delta", item_id: "msg_1", output_index: null, delta: "Hi" },
658+
{ type: "response.output_text.delta", item_id: "msg_1", output_index: 0, delta: null },
659+
{ type: "response.output_text.done", item_id: "msg_1", output_index: 0, text: null },
660+
{ type: "response.output_item.done", output_index: 0, item: message },
661+
{ type: "response.output_item.added", output_index: 1, item: { ...call, arguments: null } },
662+
{ type: "response.function_call_arguments.done", item_id: "fc", output_index: 1, arguments: null },
663+
{ type: "response.output_item.done", output_index: 1, item: { ...call, arguments: "{}" } },
664+
{
665+
type: "response.output_item.done",
666+
output_index: 2,
667+
item: { type: "function_call", id: "fc_2", call_id: null, name: null, arguments: "{}" },
668+
},
669+
{ type: "response.output_item.added", output_index: 3, item: { type: "reasoning", id: "rs_1" } },
670+
{
671+
type: "response.reasoning_summary_part.added",
672+
item_id: "rs_1",
673+
output_index: 3,
674+
summary_index: null,
675+
},
676+
{ type: "response.output_item.done", output_index: 3, item: { type: "reasoning", id: "rs_1" } },
677+
{
678+
type: "response.completed",
679+
response: {
680+
id: null,
681+
output: null,
682+
incomplete_details: { reason: null },
683+
usage: {
684+
input_tokens: 5,
685+
input_tokens_details: { cached_tokens: null, cache_write_tokens: null },
686+
output_tokens: null,
687+
output_tokens_details: { reasoning_tokens: null },
688+
total_tokens: "7",
689+
},
690+
},
691+
},
658692
),
659693
),
660694
),
661695
)
662696

697+
expect(response.text).toBe("Hi")
663698
expect(response.toolCalls).toEqual([expect.objectContaining({ id: "call_1", name: "lookup", input: {} })])
664699
expect(response.toolCalls[0]?.namespace).toBeUndefined()
700+
expect(response.usage).toMatchObject({
701+
inputTokens: 5,
702+
outputTokens: undefined,
703+
cacheReadInputTokens: undefined,
704+
})
665705
}),
666706
)
667707

@@ -705,7 +745,7 @@ describe("Open Responses-compatible route", () => {
705745
Effect.gen(function* () {
706746
yield* Effect.forEach(["response.output_item.added", "response.output_item.done"], (type) =>
707747
Effect.forEach(fixtures, (fixture) =>
708-
Effect.forEach([null, 0, false, {}, []], (id) =>
748+
Effect.forEach([0, false, {}, []], (id) =>
709749
Effect.gen(function* () {
710750
const error = yield* LLMClient.generate(request).pipe(
711751
Effect.provide(

0 commit comments

Comments
 (0)