Skip to content

Commit b321c66

Browse files
committed
fix(openai-chat): tolerate non-string padding repeats on resolved stream tool calls
opencode.ai/zen continuation deltas can repeat the function envelope with wrong JSON types (object name) instead of the null #1731 describes. Once the pending call carries its canonical name, a non-string repeat adds nothing: ignore it and let only string fragments reach the accumulator. A non-string value while the call is still unnamed (or whitespace-only) fails closed (#1531).
1 parent 70e8bab commit b321c66

2 files changed

Lines changed: 132 additions & 18 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,20 +1628,9 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
16281628
// name or arguments value fails closed through the #1325 channel here rather
16291629
// than escaping later as a TypeError from string handling at flush time.
16301630
const rawFunction = (rawToolCall as { function?: unknown }).function;
1631-
if (rawFunction !== undefined && rawFunction !== null) {
1632-
if (!isRecord(rawFunction)) {
1633-
logInvalidToolCalls("stream", rawToolCalls);
1634-
return yield* terminateWithError(invalidToolCallsEvent(rawToolCalls, "stream", pendingUsage));
1635-
}
1636-
const rawName = rawFunction.name;
1637-
const rawArguments = rawFunction.arguments;
1638-
// Some OpenAI-compatible streamers repeat already-sent fields as null on
1639-
// continuation deltas. Treat only null/undefined as absent; every other
1640-
// non-string value still fails closed before entering the accumulator.
1641-
if (isInvalidStreamStringField(rawName) || isInvalidStreamStringField(rawArguments)) {
1642-
logInvalidToolCalls("stream", rawToolCalls);
1643-
return yield* terminateWithError(invalidToolCallsEvent(rawToolCalls, "stream", pendingUsage));
1644-
}
1631+
if (rawFunction !== undefined && rawFunction !== null && !isRecord(rawFunction)) {
1632+
logInvalidToolCalls("stream", rawToolCalls);
1633+
return yield* terminateWithError(invalidToolCallsEvent(rawToolCalls, "stream", pendingUsage));
16451634
}
16461635
if (isInvalidStreamStringField(tc.id)) {
16471636
logInvalidToolCalls("stream", rawToolCalls);
@@ -1660,14 +1649,34 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
16601649
budget.openCall(call.key);
16611650
}
16621651
if (tc.id && !call.id) call.id = tc.id;
1663-
if (tc.function?.name && !call.name) call.name = tc.function.name;
1664-
if (tc.function?.arguments) {
1652+
// #1731 generalization: some gateways repeat the function envelope on
1653+
// continuation deltas with wrong JSON types (an object `name` observed from
1654+
// opencode.ai/zen) instead of null. Once the pending call carries its
1655+
// canonical name, a non-string repeat adds nothing — ignore it, and let only
1656+
// string fragments reach the accumulator. A non-string value while the call is
1657+
// still unnamed fails closed (#1531): it was the field's only chance to be
1658+
// established, and nothing else can name or parameterize the call.
1659+
const fnRecord = isRecord(rawFunction) ? rawFunction : undefined;
1660+
const hasCanonicalName = typeof call.name === "string" && call.name.trim().length > 0;
1661+
if (isInvalidStreamStringField(fnRecord?.name) && !hasCanonicalName) {
1662+
logInvalidToolCalls("stream", rawToolCalls);
1663+
return yield* terminateWithError(invalidToolCallsEvent(rawToolCalls, "stream", pendingUsage));
1664+
}
1665+
if (isInvalidStreamStringField(fnRecord?.arguments) && !hasCanonicalName) {
1666+
logInvalidToolCalls("stream", rawToolCalls);
1667+
return yield* terminateWithError(invalidToolCallsEvent(rawToolCalls, "stream", pendingUsage));
1668+
}
1669+
if (typeof tc.function?.name === "string" && !hasCanonicalName) call.name = tc.function.name;
1670+
// Only string fragments enter the accumulator: an accepted non-string repeat
1671+
// (padding, guarded above) must not concatenate "[object Object]" into args.
1672+
const argsDelta = typeof tc.function?.arguments === "string" ? tc.function.arguments : "";
1673+
if (argsDelta) {
16651674
const previousBytes = call.argsBytes;
1666-
const nextBytes = previousBytes + budgetEncoder.encode(tc.function.arguments).byteLength;
1675+
const nextBytes = previousBytes + budgetEncoder.encode(argsDelta).byteLength;
16671676
const scope = { kind: "tool_args" as const, callId: call.key };
16681677
const reservation = budget.reserveTransient(nextBytes, scope);
16691678
try {
1670-
call.args += tc.function.arguments;
1679+
call.args += argsDelta;
16711680
reservation.commitRetained();
16721681
budget.releaseRetained(previousBytes, scope);
16731682
call.argsBytes = nextBytes;

‎tests/openai-chat-hardening.test.ts‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,111 @@ describe("openai-chat stream response hardening", () => {
476476
expect(lines).toContain('"callIndex":1');
477477
expect(lines).not.toContain('"tool_call_function_name_invalid"');
478478
});
479+
480+
test("non-string repeated name/arguments on a resolved call are padding, not terminal", async () => {
481+
// opencode.ai/zen continuation deltas: the first chunk carries id+name; later repeats
482+
// can arrive with wrong JSON types (object name) instead of the null #1731 describes.
483+
const adapter = createOpenAIChatAdapter(provider());
484+
const response = new Response([
485+
`data: ${JSON.stringify({ choices: [{ delta: { tool_calls: [{
486+
index: 0,
487+
id: "chatcmpl-tool-abc",
488+
type: "function",
489+
function: { name: "terminal", arguments: "" },
490+
}] } }] })}\n\n`,
491+
`data: ${JSON.stringify({ choices: [{ delta: { tool_calls: [{
492+
index: 0,
493+
id: null,
494+
type: null,
495+
function: { name: { unexpected: true }, arguments: { no: "string" } },
496+
}] } }] })}\n\n`,
497+
`data: ${JSON.stringify({ choices: [{ delta: { tool_calls: [{
498+
index: 0,
499+
id: null,
500+
type: null,
501+
function: { name: null, arguments: "{\"command\":\"ls\"}" },
502+
}] } }] })}\n\n`,
503+
`data: ${JSON.stringify({ choices: [{ delta: {}, finish_reason: "tool_calls" }], usage: { prompt_tokens: 7, completion_tokens: 3 } })}\n\n`,
504+
"data: [DONE]\n\n",
505+
].join(""));
506+
507+
const events = await collect(adapter.parseStream(response));
508+
expect(events).toEqual([
509+
{ type: "tool_call_start", id: "chatcmpl-tool-abc", name: "terminal" },
510+
{ type: "tool_call_delta", arguments: '{"command":"ls"}' },
511+
{ type: "tool_call_end" },
512+
{ type: "done", usage: { inputTokens: 7, outputTokens: 3 } },
513+
]);
514+
});
515+
516+
test("non-string name on a call without its canonical value still fails closed", async () => {
517+
const adapter = createOpenAIChatAdapter(provider());
518+
const response = new Response([
519+
`data: ${JSON.stringify({ choices: [{ delta: { tool_calls: [{
520+
index: 0,
521+
id: "chatcmpl-tool-abc",
522+
type: "function",
523+
function: { name: { unexpected: true }, arguments: "" },
524+
}] } }] })}\n\n`,
525+
"data: [DONE]\n\n",
526+
].join(""));
527+
528+
const events = await collect(adapter.parseStream(response));
529+
expect(events).toEqual([{
530+
type: "error",
531+
status: 502,
532+
errorType: "upstream_error",
533+
message: "upstream response contained invalid tool calls (tool_call_function_name_invalid; callIndex=0; valueType=object)",
534+
}]);
535+
});
536+
537+
test("non-string arguments on a call that has accumulated nothing still fails closed", async () => {
538+
const adapter = createOpenAIChatAdapter(provider());
539+
const response = new Response([
540+
`data: ${JSON.stringify({ choices: [{ delta: { tool_calls: [{
541+
index: 0,
542+
id: "chatcmpl-tool-abc",
543+
type: "function",
544+
function: { name: "terminal", arguments: { command: "ls" } },
545+
}] } }] })}\n\n`,
546+
"data: [DONE]\n\n",
547+
].join(""));
548+
549+
const events = await collect(adapter.parseStream(response));
550+
expect(events).toEqual([{
551+
type: "error",
552+
status: 502,
553+
errorType: "upstream_error",
554+
message: "upstream response contained invalid tool calls (tool_call_function_arguments_invalid; callIndex=0; valueType=object)",
555+
}]);
556+
});
557+
558+
test("whitespace-only initial name followed by non-string delta fails closed", async () => {
559+
const adapter = createOpenAIChatAdapter(provider());
560+
const response = new Response([
561+
`data: ${JSON.stringify({ choices: [{ delta: { tool_calls: [{
562+
index: 0,
563+
id: "chatcmpl-tool-abc",
564+
type: "function",
565+
function: { name: " ", arguments: "" },
566+
}] } }] })}\n\n`,
567+
`data: ${JSON.stringify({ choices: [{ delta: { tool_calls: [{
568+
index: 0,
569+
id: null,
570+
type: null,
571+
function: { name: { unexpected: true }, arguments: { no: "string" } },
572+
}] } }] })}\n\n`,
573+
"data: [DONE]\n\n",
574+
].join(""));
575+
576+
const events = await collect(adapter.parseStream(response));
577+
expect(events).toEqual([{
578+
type: "error",
579+
status: 502,
580+
errorType: "upstream_error",
581+
message: "upstream response contained invalid tool calls (tool_call_function_name_invalid; callIndex=0; valueType=object)",
582+
}]);
583+
});
479584
});
480585

481586
describe("openai-chat credential hardening", () => {

0 commit comments

Comments
 (0)