Skip to content

Commit 6c10672

Browse files
fix(ai): replace streamed function name on continuation (#130537)
Use the latest nonempty function-name snapshot for a resolved tool call so fragmented direct OpenAI-compatible streams do not publish stale tool names. Preserve the first tool identity when a later fragment supplies an explicitly conflicting ID. Co-authored-by: SunnyShu0925 <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent a3eb069 commit 6c10672

3 files changed

Lines changed: 177 additions & 1 deletion

File tree

‎packages/ai/src/providers/openai-completions.test.ts‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,120 @@ describe("openai-completions stop-reason tool-call guard", () => {
16991699
});
17001700
});
17011701

1702+
it("replaces the stored function name on a same-id fragmented continuation", async () => {
1703+
// A stable tool-call id that repeats across deltas signals a continuation of
1704+
// the same call, so the latest nonempty function-name snapshot must replace
1705+
// the stored name — mirroring the pinned OpenAI SDK and the managed
1706+
// transport. Without this the first fragment (e.g. "get_") freezes and the
1707+
// call is published under a stale, partial name.
1708+
mockChunksRef.chunks = [
1709+
makeToolCallChunk("call_name", "get_", '{"city":'),
1710+
makeToolCallChunk("call_name", "weather", '"Paris"}'),
1711+
makeFinishChunk("tool_calls"),
1712+
];
1713+
1714+
const stream = streamOpenAICompletions(model, context, { apiKey: "sk-test" });
1715+
const result = await stream.result();
1716+
1717+
expect(result.content).toContainEqual({
1718+
type: "toolCall",
1719+
id: "call_name",
1720+
name: "weather",
1721+
arguments: { city: "Paris" },
1722+
});
1723+
});
1724+
1725+
it("replaces the stored name when an index-resolved continuation omits the id", async () => {
1726+
// A continuation delta that re-uses the established index but omits the id
1727+
// is still the same tool call — the block was already resolved by index
1728+
// above, so an absent id is a continuation, not a conflicting identity. The
1729+
// latest nonempty function-name snapshot must replace the stored name,
1730+
// matching the pinned OpenAI SDK accumulator. Only an explicitly conflicting
1731+
// id (next test) keeps the first name.
1732+
mockChunksRef.chunks = [
1733+
makeToolCallChunk("call_A", "first", '{"city":'),
1734+
{
1735+
id: "chatcmpl-test",
1736+
choices: [
1737+
{
1738+
index: 0,
1739+
delta: {
1740+
tool_calls: [
1741+
{ index: 0, function: { name: "second", arguments: '"Paris"}' }, type: "function" },
1742+
],
1743+
},
1744+
finish_reason: "tool_calls",
1745+
},
1746+
],
1747+
},
1748+
];
1749+
1750+
const stream = streamOpenAICompletions(model, context, { apiKey: "sk-test" });
1751+
const result = await stream.result();
1752+
1753+
expect(result.content).toContainEqual({
1754+
type: "toolCall",
1755+
id: "call_A",
1756+
name: "second",
1757+
arguments: { city: "Paris" },
1758+
});
1759+
});
1760+
1761+
it("keeps the first name when a continuation carries a conflicting id", async () => {
1762+
// A continuation whose id explicitly conflicts with the established block
1763+
// id is not the same call, so in direct mode the first tool identity wins
1764+
// and the stored name is preserved.
1765+
mockChunksRef.chunks = [
1766+
makeToolCallChunk("call_A", "first", '{"city":'),
1767+
makeToolCallChunk("call_B", "second", '"Paris"}'),
1768+
makeFinishChunk("tool_calls"),
1769+
];
1770+
1771+
const stream = streamOpenAICompletions(model, context, { apiKey: "sk-test" });
1772+
const result = await stream.result();
1773+
1774+
expect(result.content).toContainEqual({
1775+
type: "toolCall",
1776+
id: "call_A",
1777+
name: "first",
1778+
arguments: { city: "Paris" },
1779+
});
1780+
});
1781+
1782+
it("does not erase the stored name when a continuation carries an empty name", async () => {
1783+
// An empty function-name snapshot must not replace the stored name — the
1784+
// guard checks truthiness, so "" is skipped. This mirrors the pinned SDK
1785+
// (which only replaces on nonempty fn.name) and satisfies the issue's
1786+
// "empty-name continuation" assertion requirement.
1787+
mockChunksRef.chunks = [
1788+
makeToolCallChunk("call_name", "get_weather", '{"city":'),
1789+
{
1790+
id: "chatcmpl-test",
1791+
choices: [
1792+
{
1793+
index: 0,
1794+
delta: {
1795+
tool_calls: [
1796+
{ index: 0, function: { name: "", arguments: '"Paris"}' }, type: "function" },
1797+
],
1798+
},
1799+
finish_reason: "tool_calls",
1800+
},
1801+
],
1802+
},
1803+
];
1804+
1805+
const stream = streamOpenAICompletions(model, context, { apiKey: "sk-test" });
1806+
const result = await stream.result();
1807+
1808+
expect(result.content).toContainEqual({
1809+
type: "toolCall",
1810+
id: "call_name",
1811+
name: "get_weather",
1812+
arguments: { city: "Paris" },
1813+
});
1814+
});
1815+
17021816
it("publishes post-tool text immediately and closes blocks in their original order", async () => {
17031817
mockChunksRef.chunks = [
17041818
makeToolCallChunk("call_1", "lookup", '{"value":1}'),

‎packages/ai/src/transports/openai-completions-stream.tool-calls.test.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,4 +488,57 @@ describe("openai completions stream", () => {
488488
expect(output.content).toHaveLength(1);
489489
expect((output.content[0] as { type?: string }).type).toBe("text");
490490
});
491+
492+
it("replaces the stored function name on a fragmented continuation (managed parity)", async () => {
493+
// The managed path (no mode: "direct") invokes the same processCompletionsStream
494+
// assembler. A fragmented function name arriving in two nonempty snapshots on
495+
// the same index must resolve to the latest snapshot, matching the direct
496+
// provider and the pinned OpenAI SDK. This satisfies the issue's
497+
// "managed-parity assertions" requirement.
498+
const model = makeCompletionsModel({
499+
id: "qwen3.6-27b",
500+
name: "Qwen 3.6 27B",
501+
provider: "vllm",
502+
baseUrl: "http://localhost:8000/v1",
503+
reasoning: false,
504+
contextWindow: 131072,
505+
});
506+
507+
const output = createAssistantOutput(model);
508+
const stream = { push: () => {} };
509+
510+
const chunks = [
511+
makeCompletionsChunk({
512+
tool_calls: [
513+
{
514+
index: 0,
515+
id: "call_managed",
516+
function: { name: "get_", arguments: '{"city":' },
517+
type: "function",
518+
},
519+
],
520+
}),
521+
makeCompletionsChunk({
522+
tool_calls: [
523+
{
524+
index: 0,
525+
function: { name: "weather", arguments: '"Paris"}' },
526+
type: "function",
527+
},
528+
],
529+
}),
530+
makeCompletionsChunk({}, "tool_calls"),
531+
];
532+
533+
await processCompletionsStream(streamChunks(chunks), output, model, stream);
534+
535+
const toolCalls = output.content.filter(
536+
(block) => (block as { type?: string }).type === "toolCall",
537+
);
538+
expect(toolCalls).toHaveLength(1);
539+
const toolCall = toolCalls[0] as { id?: string; name?: string; arguments?: unknown };
540+
expect(toolCall.id).toBe("call_managed");
541+
expect(toolCall.name).toBe("weather");
542+
expect(toolCall.arguments).toEqual({ city: "Paris" });
543+
});
491544
});

‎packages/ai/src/transports/openai-completions-stream.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,16 @@ export async function processCompletionsStream(
610610
}
611611
}
612612
currentBlock = block;
613-
if (toolCall.function?.name && (!directMode || !block.name)) {
613+
// Mirror the pinned OpenAI SDK and the managed transport: a nonempty
614+
// function-name snapshot replaces the stored name so fragmented or
615+
// corrected streamed names cannot freeze on the first fragment. In
616+
// direct mode the first tool identity is authoritative, so only a
617+
// continuation whose id explicitly conflicts with the established
618+
// block keeps the first name; an absent id is treated as a
619+
// continuation (the block was already resolved by index or id above),
620+
// matching how the pinned SDK accumulates a later name-only frame.
621+
const conflictingId = directMode && block.id && toolCall.id && block.id !== toolCall.id;
622+
if (toolCall.function?.name && !conflictingId) {
614623
block.name = toolCall.function.name;
615624
}
616625
const deltaSig = directMode ? undefined : extractToolCallThoughtSignature(toolCall);

0 commit comments

Comments
 (0)