Skip to content

Commit 5b1fe94

Browse files
kksingh000claude
andcommitted
fix(ai): accept null namespace on function_call items
Open Responses-compatible gateways may serialize the optional `namespace` field of a function_call item as JSON `null` instead of omitting it. The schema only accepted `string | undefined`, so the first tool call failed with SchemaError: Expected string | undefined at ["item"]["namespace"], breaking every tool call (including subagent fan-out) for affected providers. Decode namespace with optionalNull(Schema.String) in the streaming StreamItem schema (the only place gateway responses are decoded), and normalize null to undefined at the ToolStream.start/toolInputStart call sites that read item.namespace. Fixes anomalyco#55557 Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01G5nxz4vZmpdQ2ZMp5dfdAu
1 parent 13afea6 commit 5b1fe94

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export const StreamItem = Schema.StructWithRest(
334334
id: Schema.optional(Schema.String),
335335
call_id: Schema.optional(Schema.String),
336336
name: Schema.optional(Schema.String),
337-
namespace: Schema.optional(Schema.String),
337+
namespace: optionalNull(Schema.String),
338338
arguments: Schema.optional(Schema.String),
339339
encrypted_content: optionalNull(Schema.String),
340340
}),
@@ -1107,7 +1107,7 @@ const onOutputItemAdded = (state: ParserState, event: NormalizedEvent): StepResu
11071107
tools: ToolStream.start(state.tools, item.id, {
11081108
id: item.call_id,
11091109
name: item.name ?? "",
1110-
namespace: item.namespace,
1110+
namespace: item.namespace ?? undefined,
11111111
input: item.arguments ?? "",
11121112
providerMetadata: metadata,
11131113
}),
@@ -1117,7 +1117,7 @@ const onOutputItemAdded = (state: ParserState, event: NormalizedEvent): StepResu
11171117
LLMEvent.toolInputStart({
11181118
id: item.call_id,
11191119
name: item.name ?? "",
1120-
namespace: item.namespace,
1120+
namespace: item.namespace ?? undefined,
11211121
providerMetadata: metadata,
11221122
}),
11231123
],
@@ -1242,7 +1242,7 @@ const onOutputItemDone = Effect.fnUntraced(function* (
12421242
: ToolStream.start(state.tools, item.id, {
12431243
id: item.call_id,
12441244
name: item.name,
1245-
namespace: item.namespace,
1245+
namespace: item.namespace ?? undefined,
12461246
providerMetadata: metadata,
12471247
})
12481248
const result =
@@ -1259,7 +1259,7 @@ const onOutputItemDone = Effect.fnUntraced(function* (
12591259
LLMEvent.toolInputStart({
12601260
id: item.call_id,
12611261
name: item.name,
1262-
namespace: item.namespace,
1262+
namespace: item.namespace ?? undefined,
12631263
providerMetadata: metadata,
12641264
}),
12651265
...finished,

‎packages/ai/test/provider/open-responses-lifecycle.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,35 @@ describe("Open Responses basic-item lifecycles", () => {
377377
}),
378378
)
379379

380+
// Some Responses-compatible gateways serialize the optional `namespace` field as
381+
// JSON `null` rather than omitting it. That must decode like an absent namespace.
382+
it.effect("treats a null namespace on a function_call item as absent", () =>
383+
Effect.gen(function* () {
384+
const item = {
385+
type: "function_call",
386+
id: "fc_1",
387+
call_id: "call_1",
388+
name: "lookup",
389+
namespace: null,
390+
arguments: '{"query":"weather"}',
391+
}
392+
const events = yield* collect({ type: "response.output_item.done", item }, completed)
393+
const providerMetadata = { "openai-compatible": { itemId: "fc_1" } }
394+
expect(events.filter((event) => event.type.startsWith("tool-"))).toEqual([
395+
{ type: "tool-input-start", id: "call_1", name: "lookup", namespace: undefined, providerMetadata },
396+
{ type: "tool-input-end", id: "call_1", name: "lookup", namespace: undefined, providerMetadata },
397+
{
398+
type: "tool-call",
399+
id: "call_1",
400+
name: "lookup",
401+
namespace: undefined,
402+
input: { query: "weather" },
403+
providerMetadata,
404+
},
405+
])
406+
}),
407+
)
408+
380409
it.effect("recovers pending calls without reconciling terminal reasoning", () =>
381410
Effect.gen(function* () {
382411
const events = yield* collect(

0 commit comments

Comments
 (0)