Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(sdk): keep tool names on Vercel tool-result parts through the OpenAI round trip (#3883)

Shubham Srivastavab7156714acf1
diff --git a/sdk/typescript/src/utils/format.ts b/sdk/typescript/src/utils/format.ts
index c2ac954b6..6a12061ae 100644
--- a/sdk/typescript/src/utils/format.ts
+++ b/sdk/typescript/src/utils/format.ts
@@ -328,6 +328,8 @@ export function vercelToOpenAI(messages: any[]): OpenAIMessage[] {
export function openAIToVercel(messages: OpenAIMessage[]): any[] {
const result: any[] = [];
+ // tool_call id -> function name, so tool-result parts carry the name the AI SDK expects.
+ const toolNames = new Map<string, string>();
for (const msg of messages) {
if (msg.role === "system") {
@@ -354,6 +356,7 @@ export function openAIToVercel(messages: OpenAIMessage[]): any[] {
if (msg.content) parts.push({ type: "text", text: msg.content });
if (msg.tool_calls) {
for (const tc of msg.tool_calls) {
+ toolNames.set(tc.id, tc.function.name);
let input: any;
try { input = JSON.parse(tc.function.arguments); } catch { input = tc.function.arguments ?? {}; }
parts.push({
@@ -380,7 +383,7 @@ export function openAIToVercel(messages: OpenAIMessage[]): any[] {
content: [{
type: "tool-result",
toolCallId: msg.tool_call_id,
- toolName: "unknown",
+ toolName: toolNames.get(msg.tool_call_id) ?? "unknown",
output,
}],
});
diff --git a/sdk/typescript/test/adapters/vercel-ai.test.ts b/sdk/typescript/test/adapters/vercel-ai.test.ts
index ac8634af9..cc4ab6e98 100644
--- a/sdk/typescript/test/adapters/vercel-ai.test.ts
+++ b/sdk/typescript/test/adapters/vercel-ai.test.ts
@@ -171,6 +171,39 @@ describe("compressVercelMessages", () => {
expect(result.messages[1].role).toBe("user");
expect(result.messages[1].content[0].type).toBe("text");
});
+
+ it("keeps tool names on tool-result parts after compression", async () => {
+ mockFetch.mockResolvedValueOnce(
+ mockCompressResponse([
+ {
+ role: "assistant",
+ content: null,
+ tool_calls: [
+ { id: "call_1", type: "function", function: { name: "getWeather", arguments: '{"city":"Paris"}' } },
+ ],
+ },
+ { role: "tool", content: '{"tempC":21}', tool_call_id: "call_1" },
+ ]),
+ );
+
+ const result = await compressVercelMessages(
+ [
+ {
+ role: "assistant",
+ content: [{ type: "tool-call", toolCallId: "call_1", toolName: "getWeather", input: { city: "Paris" } }],
+ },
+ {
+ role: "tool",
+ content: [
+ { type: "tool-result", toolCallId: "call_1", toolName: "getWeather", output: { type: "json", value: { tempC: 21 } } },
+ ],
+ },
+ ],
+ { model: "gpt-4o", baseUrl: "http://localhost:8787" },
+ );
+
+ expect(result.messages[1].content[0].toolName).toBe("getWeather");
+ });
});
describe("withHeadroom", () => {
diff --git a/sdk/typescript/test/utils/format.test.ts b/sdk/typescript/test/utils/format.test.ts
index f6763156a..b2a1676b1 100644
--- a/sdk/typescript/test/utils/format.test.ts
+++ b/sdk/typescript/test/utils/format.test.ts
@@ -638,3 +638,44 @@ describe("round-trip conversion", () => {
});
});
});
+
+describe("openAIToVercel tool names", () => {
+ it("carries the tool name from the matching tool-call onto the tool-result part", () => {
+ const msgs: OpenAIMessage[] = [
+ {
+ role: "assistant",
+ content: null,
+ tool_calls: [
+ { id: "tc_1", type: "function", function: { name: "getWeather", arguments: '{"city":"Paris"}' } },
+ ],
+ },
+ { role: "tool", content: '{"tempC":21}', tool_call_id: "tc_1" },
+ ];
+ const result = openAIToVercel(msgs);
+ expect(result[1].content[0]).toEqual({
+ type: "tool-result",
+ toolCallId: "tc_1",
+ toolName: "getWeather",
+ output: { type: "json", value: { tempC: 21 } },
+ });
+ });
+
+ it("preserves tool names through a Vercel -> OpenAI -> Vercel round trip", () => {
+ const original = [
+ { role: "user", content: [{ type: "text", text: "weather in Paris?" }] },
+ {
+ role: "assistant",
+ content: [{ type: "tool-call", toolCallId: "call_1", toolName: "getWeather", input: { city: "Paris" } }],
+ },
+ {
+ role: "tool",
+ content: [
+ { type: "tool-result", toolCallId: "call_1", toolName: "getWeather", output: { type: "json", value: { tempC: 21 } } },
+ ],
+ },
+ ];
+ const back = openAIToVercel(vercelToOpenAI(original));
+ expect(back[2].content[0].toolName).toBe("getWeather");
+ });
+});
+