zen-free: a null name or arguments in a streamed function delta adds nothing, instead of failing the turn (#252, ChuHanModel)
A Chat Completions stream whose tool_calls or function_call delta carries `"name": null` or `"arguments": null` (some OpenAI-compatible vendors send these) failed the whole turn with "Invalid upstream function delta". appendFunction now treats a null/undefined name or arguments as no new bytes and keeps the fragments already buffered. Other non-string types still fail. The name is still checked on the whole concatenated value when the choice finishes, so a call whose name stays null is still left out with its note; skipping null can't let a call past the policy. Siblings: no other package vendors tool-response.mjs (grep for "Invalid upstream function delta" finds only zen-free); zed's tool-call merge already appends only truthy arguments. Run at the reviewed head 48a266070ed1ad3e63fa864e438af8163110f5f6: - Fix removed by hand: 4 new tests red (tool_calls / function_call "ignores null fields..." and "still drops calls whose name remains null", failing on `!values.some(value => value.error)`), 104 pass. Restored: node --test packages/zen-free/tool-response.test.mjs 108 pass, 0 fail. - HOME=$(mktemp -d) bun test: 1853 pass, 0 fail across 168 files. - bun scripts/check.mjs: passed. Version bump to zen-free 0.1.12 follows in its own commit.
This commit is contained in:
@@ -250,6 +250,8 @@ function appendFunction(target, fragment) {
|
|||||||
if (!object(fragment)) throw new GuardError("Invalid upstream function delta");
|
if (!object(fragment)) throw new GuardError("Invalid upstream function delta");
|
||||||
for (const [key, value] of Object.entries(fragment)) {
|
for (const [key, value] of Object.entries(fragment)) {
|
||||||
if (key === "name" || key === "arguments") {
|
if (key === "name" || key === "arguments") {
|
||||||
|
// Nullable stream fields carry no new bytes; keep prior fragments intact.
|
||||||
|
if (value == null) continue;
|
||||||
if (typeof value !== "string") throw new GuardError("Invalid upstream function delta");
|
if (typeof value !== "string") throw new GuardError("Invalid upstream function delta");
|
||||||
target[key] = (target[key] ?? "") + value;
|
target[key] = (target[key] ?? "") + value;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -994,6 +994,67 @@ test("chat: buffers names and arguments by choice/index, including arguments bef
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
for (const legacy of [false, true]) {
|
||||||
|
const kind = legacy ? "function_call" : "tool_calls";
|
||||||
|
const delta = (fragment) => legacy
|
||||||
|
? { function_call: fragment }
|
||||||
|
: { tool_calls: [{ index: 0, id: "call-0", type: "function", function: fragment }] };
|
||||||
|
const functions = (values) => values
|
||||||
|
.flatMap((value) => value.choices ?? [])
|
||||||
|
.flatMap((choice) => legacy
|
||||||
|
? (choice.delta?.function_call ? [choice.delta.function_call] : [])
|
||||||
|
: (choice.delta?.tool_calls ?? []).map((value) => value.function));
|
||||||
|
|
||||||
|
test(`chat: ${kind} ignores null fields while preserving name and argument fragments`, async () => {
|
||||||
|
const fragments = [
|
||||||
|
{ name: null, arguments: null },
|
||||||
|
{ name: "Re", arguments: null },
|
||||||
|
{ name: null, arguments: '{"path":' },
|
||||||
|
{ name: "ad", arguments: null },
|
||||||
|
{ name: null, arguments: '"example.txt"}' },
|
||||||
|
{ name: null, arguments: null },
|
||||||
|
];
|
||||||
|
const result = await guardToolResponse(
|
||||||
|
sse(fragments.map((fragment) => event(chat(delta(fragment)))).join("") +
|
||||||
|
event(chat({}, kind)) + terminal("chat"), { chunkSize: 1 }),
|
||||||
|
"chat",
|
||||||
|
policy,
|
||||||
|
);
|
||||||
|
const values = parse(await result.text()).map((frame) => frame.data);
|
||||||
|
assert.ok(!values.some((value) => value.error));
|
||||||
|
assert.deepEqual(functions(values), [{ name: "Read", arguments: '{"path":"example.txt"}' }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test(`chat: ${kind} still drops calls whose name remains null`, async () => {
|
||||||
|
const result = await guardToolResponse(
|
||||||
|
sse(event(chat(delta({ name: null, arguments: "{}" }), kind)) + terminal("chat")),
|
||||||
|
"chat",
|
||||||
|
policy,
|
||||||
|
);
|
||||||
|
const values = parse(await result.text()).map((frame) => frame.data);
|
||||||
|
assert.ok(!values.some((value) => value.error));
|
||||||
|
assert.deepEqual(functions(values), []);
|
||||||
|
assert.equal(textIn("chat", values), droppedNote(""));
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const field of ["name", "arguments"]) {
|
||||||
|
for (const invalid of [42, false, [], {}]) {
|
||||||
|
test(`chat: ${kind} rejects ${field} delta ${JSON.stringify(invalid)}`, async () => {
|
||||||
|
const result = await guardToolResponse(
|
||||||
|
sse(event(chat(delta({ name: "Read", arguments: "" }))) +
|
||||||
|
event(chat(delta({ [field]: invalid }), kind)) + terminal("chat")),
|
||||||
|
"chat",
|
||||||
|
policy,
|
||||||
|
);
|
||||||
|
const values = parse(await result.text()).map((frame) => frame.data);
|
||||||
|
assert.deepEqual(functions(values), []);
|
||||||
|
assertError("chat", values[0]);
|
||||||
|
assert.equal(values[0].error.message, "Invalid upstream function delta");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
test("chat: legacy fragments flush at DONE, but truncated pending calls never escape", async () => {
|
test("chat: legacy fragments flush at DONE, but truncated pending calls never escape", async () => {
|
||||||
const prefix =
|
const prefix =
|
||||||
event(chat({ function_call: { arguments: "{}" } })) +
|
event(chat({ function_call: { arguments: "{}" } })) +
|
||||||
|
|||||||
Reference in New Issue
Block a user