Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(tool-parsing): ai sdk parsing of tools (#13533)

Nimarcb0aa342914e
diff --git a/packages/shared/src/server/ingestion/extractToolsBackend.ts b/packages/shared/src/server/ingestion/extractToolsBackend.ts
index 27bf847077..cf41280de3 100644
--- a/packages/shared/src/server/ingestion/extractToolsBackend.ts
+++ b/packages/shared/src/server/ingestion/extractToolsBackend.ts
@@ -76,7 +76,7 @@ function flattenToolCall(call: unknown): {
// Handle nested {function: {name, arguments}} format
const func = c.function as Record<string, unknown> | undefined;
const name = (func?.name ?? c.name ?? c.toolName) as string | undefined;
- const rawArgs = func?.arguments ?? c.arguments ?? c.args;
+ const rawArgs = func?.arguments ?? c.arguments ?? c.args ?? c.input;
const args =
typeof rawArgs === "string" ? rawArgs : JSON.stringify(rawArgs ?? {});
@@ -129,6 +129,28 @@ function addToolArgument(args: ClickhouseToolArgument[], call: unknown): void {
});
}
+/**
+ * Helper to add a tool call from content-array parts.
+ * Handles Anthropic `tool_use` and AI SDK `tool-call` parts.
+ */
+function addToolArgumentFromContentPart(
+ args: ClickhouseToolArgument[],
+ part: Record<string, unknown>,
+): void {
+ if (part.type === "tool_use") {
+ addToolArgument(args, {
+ id: part.id,
+ name: part.name,
+ arguments: JSON.stringify(part.input ?? {}),
+ type: "tool_use",
+ });
+ }
+
+ if (part.type === "tool-call") {
+ addToolArgument(args, part);
+ }
+}
+
/**
* Extract tool definitions from raw input data (top-level tools array).
*/
@@ -218,21 +240,17 @@ function extractToolCallsFromRawOutput(
}
}
- // Anthropic tool_use in content array: {content: [{type: "tool_use", ...}]}
+ // Tool calls in content arrays: Anthropic `tool_use`, AI SDK `tool-call`
if (Array.isArray(obj.content)) {
for (const part of obj.content) {
if (
part &&
typeof part === "object" &&
- (part as Record<string, unknown>).type === "tool_use"
+ ["tool_use", "tool-call"].includes(
+ (part as Record<string, unknown>).type as string,
+ )
) {
- const p = part as Record<string, unknown>;
- addToolArgument(args, {
- id: p.id,
- name: p.name,
- arguments: JSON.stringify(p.input ?? {}),
- type: "tool_use",
- });
+ addToolArgumentFromContentPart(args, part as Record<string, unknown>);
}
}
}
@@ -261,21 +279,17 @@ function extractToolCallsFromMessage(
}
}
- // Anthropic content array
+ // Tool calls in content arrays: Anthropic `tool_use`, AI SDK `tool-call`
if (Array.isArray(msg.content)) {
for (const part of msg.content) {
if (
part &&
typeof part === "object" &&
- (part as Record<string, unknown>).type === "tool_use"
+ ["tool_use", "tool-call"].includes(
+ (part as Record<string, unknown>).type as string,
+ )
) {
- const p = part as Record<string, unknown>;
- addToolArgument(args, {
- id: p.id,
- name: p.name,
- arguments: JSON.stringify(p.input ?? {}),
- type: "tool_use",
- });
+ addToolArgumentFromContentPart(args, part as Record<string, unknown>);
}
}
}
diff --git a/worker/src/__tests__/extractToolsBackend.test.ts b/worker/src/__tests__/extractToolsBackend.test.ts
index 805a12a15a..6488896f46 100644
--- a/worker/src/__tests__/extractToolsBackend.test.ts
+++ b/worker/src/__tests__/extractToolsBackend.test.ts
@@ -294,6 +294,63 @@ describe("extractToolsFromObservation", () => {
});
});
+ it("extracts AI SDK tool-call content parts and input arguments from ingestion output", () => {
+ const output = [
+ {
+ role: "assistant",
+ content: [
+ { type: "text", text: "I'll look that up." },
+ {
+ type: "tool-call",
+ toolCallId: "tooluse_123",
+ toolName: "getWeather",
+ input: {
+ location: "Berlin",
+ unit: "celsius",
+ },
+ },
+ ],
+ },
+ {
+ role: "assistant",
+ content: "",
+ tool_calls: [
+ {
+ type: "tool-call",
+ toolCallId: "tooluse_456",
+ toolName: "getForecast",
+ input: {
+ location: "San Francisco",
+ days: 3,
+ },
+ },
+ ],
+ },
+ ];
+
+ const { toolArguments } = extractToolsFromObservation(null, output);
+
+ expect(toolArguments).toHaveLength(2);
+ expect(toolArguments[0]).toMatchObject({
+ id: "tooluse_123",
+ name: "getWeather",
+ arguments: JSON.stringify({
+ location: "Berlin",
+ unit: "celsius",
+ }),
+ type: "tool-call",
+ });
+ expect(toolArguments[1]).toMatchObject({
+ id: "tooluse_456",
+ name: "getForecast",
+ arguments: JSON.stringify({
+ location: "San Francisco",
+ days: 3,
+ }),
+ type: "tool-call",
+ });
+ });
+
it("preserves index field for parallel tool calls", () => {
const output = {
tool_calls: [