Skip to content

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

fix(playground): parse tools for ai sdk correctly (#10439)

Nimar8d1d6a2ee013
diff --git a/web/src/components/trace/IOPreview.tsx b/web/src/components/trace/IOPreview.tsx
index c95897106f..bbfdfebb70 100644
--- a/web/src/components/trace/IOPreview.tsx
+++ b/web/src/components/trace/IOPreview.tsx
@@ -129,12 +129,13 @@ export const IOPreview: React.FC<{
for (const toolCall of message.tool_calls) {
if (toolCall.name && typeof toolCall.name === "string") {
- toolCallCounts.set(
- toolCall.name,
- (toolCallCounts.get(toolCall.name) || 0) + 1,
- );
-
+ // count tool calls from OUTPUT messages only, only those were called
+ // in this generation
if (isOutputMessage) {
+ toolCallCounts.set(
+ toolCall.name,
+ (toolCallCounts.get(toolCall.name) || 0) + 1,
+ );
toolCallCounter++;
messageToolNumbers.push(toolCallCounter);
}
diff --git a/web/src/features/playground/page/components/JumpToPlaygroundButton.tsx b/web/src/features/playground/page/components/JumpToPlaygroundButton.tsx
index 111a5f6deb..dcb7b95c18 100644
--- a/web/src/features/playground/page/components/JumpToPlaygroundButton.tsx
+++ b/web/src/features/playground/page/components/JumpToPlaygroundButton.tsx
@@ -268,6 +268,7 @@ const parseGeneration = (
generation.output,
generation.metadata,
);
+
const structuredOutputSchema = parseStructuredOutputSchema(generation);
const providerOptions = parseLitellmMetadataFromGeneration(generation);
diff --git a/web/src/utils/chatml/adapters/aisdk.clienttest.ts b/web/src/utils/chatml/adapters/aisdk.clienttest.ts
index 74bf86ae12..6380d1f1a7 100644
--- a/web/src/utils/chatml/adapters/aisdk.clienttest.ts
+++ b/web/src/utils/chatml/adapters/aisdk.clienttest.ts
@@ -447,4 +447,123 @@ describe("AI SDK Adapter", () => {
expect(result.data?.[1].tool_calls).toBeDefined();
});
});
+
+ describe("raw tool call array handling (OUTPUT format)", () => {
+ it("should convert raw tool call array to assistant message with tool_calls", () => {
+ // This is the OUTPUT format from ai.generateText.doGenerate when model makes tool calls
+ const rawToolCallArray = [
+ {
+ toolCallId: "call_abc",
+ toolName: "get_weather",
+ input: { city: "NYC" },
+ },
+ {
+ toolCallId: "call_xyz",
+ toolName: "get_time",
+ input: { timezone: "EST" },
+ },
+ ];
+
+ const result = normalizeInput(rawToolCallArray, { framework: "aisdk" });
+
+ expect(result.success).toBe(true);
+ expect(result.data?.length).toBe(1);
+
+ // Should be assistant message
+ const msg = result.data?.[0];
+ expect(msg?.role).toBe("assistant");
+ expect(msg?.content).toBe("");
+
+ // Should have tool_calls array
+ expect(msg?.tool_calls).toBeDefined();
+ expect(msg?.tool_calls?.length).toBe(2);
+
+ // Verify first tool call structure
+ expect(msg?.tool_calls?.[0].id).toBe("call_abc");
+ expect(msg?.tool_calls?.[0].name).toBe("get_weather");
+ expect(msg?.tool_calls?.[0].arguments).toBe('{"city":"NYC"}');
+ expect(msg?.tool_calls?.[0].type).toBe("function");
+
+ // Verify second tool call
+ expect(msg?.tool_calls?.[1].id).toBe("call_xyz");
+ expect(msg?.tool_calls?.[1].name).toBe("get_time");
+ expect(msg?.tool_calls?.[1].arguments).toBe('{"timezone":"EST"}');
+ });
+
+ it("should handle Bedrock's args field (instead of input)", () => {
+ const bedrockToolCallArray = [
+ {
+ toolCallId: "tooluse_123",
+ toolName: "agentResponse",
+ args: {
+ response: "Processing your request",
+ reasoning: "Need to respond to user",
+ },
+ },
+ ];
+
+ const result = normalizeInput(bedrockToolCallArray, {
+ framework: "aisdk",
+ });
+
+ expect(result.success).toBe(true);
+ const msg = result.data?.[0];
+
+ expect(msg?.tool_calls?.length).toBe(1);
+ expect(msg?.tool_calls?.[0].id).toBe("tooluse_123");
+ expect(msg?.tool_calls?.[0].name).toBe("agentResponse");
+ expect(msg?.tool_calls?.[0].arguments).toContain("response");
+ expect(msg?.tool_calls?.[0].arguments).toContain("reasoning");
+ });
+
+ it("should attach tools from metadata context to raw tool call array", () => {
+ const rawToolCallArray = [
+ {
+ toolCallId: "call_123",
+ toolName: "calculator",
+ input: { operation: "add", a: 1, b: 2 },
+ },
+ ];
+
+ const metadata = {
+ tools: [
+ {
+ type: "function",
+ name: "calculator",
+ description: "Perform math operations",
+ inputSchema: {
+ type: "object",
+ properties: {
+ operation: { type: "string" },
+ a: { type: "number" },
+ b: { type: "number" },
+ },
+ },
+ },
+ ],
+ scope: {
+ name: "ai",
+ },
+ };
+
+ const result = normalizeInput(rawToolCallArray, {
+ metadata,
+ framework: "aisdk",
+ });
+
+ expect(result.success).toBe(true);
+ const msg = result.data?.[0];
+
+ // Tools should be attached from metadata
+ expect(msg?.tools).toBeDefined();
+ expect(msg?.tools?.length).toBe(1);
+ expect(msg?.tools?.[0].name).toBe("calculator");
+ expect(msg?.tools?.[0].description).toBe("Perform math operations");
+ expect(msg?.tools?.[0].parameters).toBeDefined();
+
+ // Tool call should still be present
+ expect(msg?.tool_calls?.length).toBe(1);
+ expect(msg?.tool_calls?.[0].name).toBe("calculator");
+ });
+ });
});
diff --git a/web/src/utils/chatml/adapters/aisdk.ts b/web/src/utils/chatml/adapters/aisdk.ts
index 5dae7825e3..2acfccfdbb 100644
--- a/web/src/utils/chatml/adapters/aisdk.ts
+++ b/web/src/utils/chatml/adapters/aisdk.ts
@@ -73,6 +73,20 @@ const AISDKMessagesArraySchema = z
{ message: "Must have AI SDK v5 tool-call or tool-result patterns" },
);
+// Array of raw tool call objects, format as seen on OUTPUTs
+// Example: [{toolCallId: "...", toolName: "...", input: {...}}, ...]
+const AISDKRawToolCallArraySchema = z
+ .array(
+ z.looseObject({
+ toolCallId: z.string(),
+ toolName: z.string(),
+ // Can have either 'input' (OpenAI) or 'args' (Bedrock)
+ }),
+ )
+ .refine((arr) => arr.length > 0, {
+ message: "Must have at least one tool call",
+ });
+
// normalize a single AI SDK v5 message to ChatML format
// we don't want additional fields here to get clean rendering
function normalizeMessage(msg: unknown): Record<string, unknown> {
@@ -256,6 +270,15 @@ function normalizeMessage(msg: unknown): Record<string, unknown> {
}
function flattenToolDefinition(tool: unknown): Record<string, unknown> {
+ // handle stringified tools (e.g. from metadata.tools with bedrock)
+ if (typeof tool === "string") {
+ try {
+ tool = JSON.parse(tool);
+ } catch {
+ return {};
+ }
+ }
+
if (typeof tool !== "object" || !tool) return {};
const t = tool as Record<string, unknown>;
@@ -362,6 +385,40 @@ function preprocessData(data: unknown, ctx?: NormalizerContext): unknown {
}
if (Array.isArray(data)) {
+ // is an array of raw tool call objects?
+ // Example: [{toolCallId: "...", toolName: "...", input: {...}}, ...]
+ const rawToolCallArrayResult = AISDKRawToolCallArraySchema.safeParse(data);
+
+ if (rawToolCallArrayResult.success) {
+ // Convert raw tool calls into a single assistant message with tool_calls
+ const toolCalls = data.map((item) => {
+ const tc = item as Record<string, unknown>;
+ const args = tc.args ?? tc.input;
+
+ return {
+ id: tc.toolCallId,
+ name: tc.toolName,
+ arguments:
+ typeof args === "string" ? args : JSON.stringify(args ?? {}),
+ type: "function",
+ };
+ });
+
+ const assistantMessage: Record<string, unknown> = {
+ role: "assistant",
+ content: "",
+ tool_calls: toolCalls,
+ };
+
+ // Attach tools from context if available
+ if (toolsFromContext && toolsFromContext.length > 0) {
+ assistantMessage.tools = toolsFromContext;
+ }
+
+ return [assistantMessage];
+ }
+
+ // otherwise it's an array of messages
const normalized = data.map(normalizeMessage);
const split = splitToolResultMessages(normalized);
@@ -422,6 +479,9 @@ export const aisdkAdapter: ProviderAdapter = {
if (AISDKToolResultMessageSchema.safeParse(ctx.data).success) return true;
if (AISDKMessagesArraySchema.safeParse(ctx.data).success) return true;
+ // Raw tool call array detection (OUTPUT format)
+ if (AISDKRawToolCallArraySchema.safeParse(ctx.data).success) return true;
+
return false;
},
diff --git a/web/src/utils/chatml/jumptoplayground.clienttest.ts b/web/src/utils/chatml/jumptoplayground.clienttest.ts
index 2180ae28aa..a40a6272ba 100644
--- a/web/src/utils/chatml/jumptoplayground.clienttest.ts
+++ b/web/src/utils/chatml/jumptoplayground.clienttest.ts
@@ -778,4 +778,60 @@ describe("Playground Jump Full Pipeline", () => {
// Verify tools extracted
expect(extractTools(input)).toHaveLength(1);
});
+
+ it("should handle stringified tools in metadata (like from vercel AI SDK v5 + bedrock)", () => {
+ // metadata contains stringified tools instead of actual objects, causing flattenToolDefinition to return {}
+ // and ChatML validation to fail with "normalizeInput success: false"
+
+ const input = [
+ {
+ role: "system",
+ content: "You are a helpful assistant.",
+ },
+ {
+ role: "user",
+ content: [{ type: "text", text: "Test" }],
+ },
+ ];
+
+ // metadata.tools array has STRINGIFIED elements
+ const metadata = {
+ tools: [
+ JSON.stringify({
+ type: "function",
+ name: "get_weather",
+ description: "Get weather info",
+ inputSchema: {
+ type: "object",
+ properties: { city: { type: "string" } },
+ required: ["city"],
+ },
+ }),
+ JSON.stringify({
+ type: "function",
+ name: "search_web",
+ description: "Search the web",
+ inputSchema: {
+ type: "object",
+ properties: { query: { type: "string" } },
+ },
+ }),
+ ],
+ scope: { name: "ai" },
+ };
+
+ const inResult = normalizeInput(input, { metadata });
+
+ // Should succeed (flattenToolDefinition parses strings)
+ expect(inResult.success).toBe(true);
+ expect(inResult.data).toBeDefined();
+ expect(inResult.data!.length).toBeGreaterThan(0);
+
+ // Verify tools are attached to messages
+ const firstMessage = inResult.data![0];
+ expect(firstMessage.tools).toBeDefined();
+ expect(firstMessage.tools!.length).toBe(2);
+ expect(firstMessage.tools![0].name).toBe("get_weather");
+ expect(firstMessage.tools![1].name).toBe("search_web");
+ });
});