Skip to content

Commit ea31d78

Browse files
committed
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 fails closed (#1531). Fixes the deterministic 400 'invalid tool calls' observed on deepseek-v4-flash stream requests through the proxy.
1 parent b4336b7 commit ea31d78

2 files changed

Lines changed: 103 additions & 17 deletions

File tree

‎src/adapters/openai-chat.ts‎

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,20 +1614,9 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
16141614
// name or arguments value fails closed through the #1325 channel here rather
16151615
// than escaping later as a TypeError from string handling at flush time.
16161616
const rawFunction = (rawToolCall as { function?: unknown }).function;
1617-
if (rawFunction !== undefined && rawFunction !== null) {
1618-
if (!isRecord(rawFunction)) {
1619-
logInvalidToolCalls("stream", rawToolCalls);
1620-
return yield* terminateWithError(invalidToolCallsEvent(rawToolCalls, "stream", pendingUsage));
1621-
}
1622-
const rawName = rawFunction.name;
1623-
const rawArguments = rawFunction.arguments;
1624-
// Some OpenAI-compatible streamers repeat already-sent fields as null on
1625-
// continuation deltas. Treat only null/undefined as absent; every other
1626-
// non-string value still fails closed before entering the accumulator.
1627-
if (isInvalidStreamStringField(rawName) || isInvalidStreamStringField(rawArguments)) {
1628-
logInvalidToolCalls("stream", rawToolCalls);
1629-
return yield* terminateWithError(invalidToolCallsEvent(rawToolCalls, "stream", pendingUsage));
1630-
}
1617+
if (rawFunction !== undefined && rawFunction !== null && !isRecord(rawFunction)) {
1618+
logInvalidToolCalls("stream", rawToolCalls);
1619+
return yield* terminateWithError(invalidToolCallsEvent(rawToolCalls, "stream", pendingUsage));
16311620
}
16321621
if (isInvalidStreamStringField(tc.id)) {
16331622
logInvalidToolCalls("stream", rawToolCalls);
@@ -1646,14 +1635,33 @@ export function createOpenAIChatAdapter(provider: OcxProviderConfig): ProviderAd
16461635
budget.openCall(call.key);
16471636
}
16481637
if (tc.id && !call.id) call.id = tc.id;
1638+
// #1731 generalization: some gateways repeat the function envelope on
1639+
// continuation deltas with wrong JSON types (an object `name` observed from
1640+
// opencode.ai/zen) instead of null. Once the pending call carries its
1641+
// canonical name, a non-string repeat adds nothing — ignore it, and let only
1642+
// string fragments reach the accumulator. A non-string value while the call is
1643+
// still unnamed fails closed (#1531): it was the field's only chance to be
1644+
// established, and nothing else can name or parameterize the call.
1645+
const fnRecord = isRecord(rawFunction) ? rawFunction : undefined;
1646+
if (isInvalidStreamStringField(fnRecord?.name) && !call.name) {
1647+
logInvalidToolCalls("stream", rawToolCalls);
1648+
return yield* terminateWithError(invalidToolCallsEvent(rawToolCalls, "stream", pendingUsage));
1649+
}
1650+
if (isInvalidStreamStringField(fnRecord?.arguments) && !call.name) {
1651+
logInvalidToolCalls("stream", rawToolCalls);
1652+
return yield* terminateWithError(invalidToolCallsEvent(rawToolCalls, "stream", pendingUsage));
1653+
}
16491654
if (tc.function?.name && !call.name) call.name = tc.function.name;
1650-
if (tc.function?.arguments) {
1655+
// Only string fragments enter the accumulator: an accepted non-string repeat
1656+
// (padding, guarded above) must not concatenate "[object Object]" into args.
1657+
const argsDelta = typeof tc.function?.arguments === "string" ? tc.function.arguments : "";
1658+
if (argsDelta) {
16511659
const previousBytes = call.argsBytes;
1652-
const nextBytes = previousBytes + budgetEncoder.encode(tc.function.arguments).byteLength;
1660+
const nextBytes = previousBytes + budgetEncoder.encode(argsDelta).byteLength;
16531661
const scope = { kind: "tool_args" as const, callId: call.key };
16541662
const reservation = budget.reserveTransient(nextBytes, scope);
16551663
try {
1656-
call.args += tc.function.arguments;
1664+
call.args += argsDelta;
16571665
reservation.commitRetained();
16581666
budget.releaseRetained(previousBytes, scope);
16591667
call.argsBytes = nextBytes;

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

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,84 @@ 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+
});
479557
});
480558

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

0 commit comments

Comments
 (0)