Skip to content

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

fix(trace-ui): render more complex tool calls pretty as table (#10310)

Nimar87ef0ac5ddba
diff --git a/web/src/components/trace/IOPreview.tsx b/web/src/components/trace/IOPreview.tsx
index 74d1c64369..c95897106f 100644
--- a/web/src/components/trace/IOPreview.tsx
+++ b/web/src/components/trace/IOPreview.tsx
@@ -458,10 +458,7 @@ export const OpenAiMessageView: React.FC<{
<>
<div
key={originalIndex}
- className={cn(
- "transition-colors hover:bg-muted",
- !isShowingTable && "group",
- )}
+ className={cn("transition-colors hover:bg-muted")}
>
{isPlaceholderMessage(message) ? (
<>
diff --git a/web/src/utils/chatml/adapters/gemini.clienttest.ts b/web/src/utils/chatml/adapters/gemini.clienttest.ts
index e6ea8e6b84..3dfd6e4df8 100644
--- a/web/src/utils/chatml/adapters/gemini.clienttest.ts
+++ b/web/src/utils/chatml/adapters/gemini.clienttest.ts
@@ -70,7 +70,7 @@ describe("geminiAdapter", () => {
expect(result[1].tool_call_id).toBe("call_123");
});
- it("should stringify object content in tool result messages", () => {
+ it("should stringify simple object content in tool result messages (1-2 scalar keys)", () => {
const input = [
{
role: "model",
@@ -79,9 +79,8 @@ describe("geminiAdapter", () => {
{
role: "tool",
content: {
- ok: true,
- data: { userId: 12345 },
- request: { unitId: 1, name: "Test User" },
+ temperature: 72,
+ conditions: "sunny",
},
tool_call_id: "call_xyz789",
},
@@ -91,18 +90,47 @@ describe("geminiAdapter", () => {
expect(result.length).toBe(2);
expect(result[1].role).toBe("tool");
- // Tool result content should be stringified
+ // Simple objects (1-2 scalar keys) get stringified
expect(typeof result[1].content).toBe("string");
expect(result[1].content).toBe(
JSON.stringify({
- ok: true,
- data: { userId: 12345 },
- request: { unitId: 1, name: "Test User" },
+ temperature: 72,
+ conditions: "sunny",
}),
);
expect(result[1].tool_call_id).toBe("call_xyz789");
});
+ it("should spread rich object content (3+ keys or nested) in tool result messages", () => {
+ const input = [
+ {
+ role: "model",
+ content: [{ type: "text", text: "Let me check." }],
+ },
+ {
+ role: "tool",
+ content: {
+ PatientNo: "123",
+ Firstname: "John",
+ Lastname: "Doe",
+ Email: "[email protected]",
+ Mobile: "1234567890",
+ },
+ tool_call_id: "call_abc123",
+ },
+ ];
+
+ const result = geminiAdapter.preprocess(input, "input", {}) as any[];
+
+ expect(result.length).toBe(2);
+ expect(result[1].role).toBe("tool");
+ // Rich objects get spread into message
+ expect(result[1].content).toBeUndefined();
+ expect(result[1].PatientNo).toBe("123");
+ expect(result[1].Firstname).toBe("John");
+ expect(result[1].tool_call_id).toBe("call_abc123");
+ });
+
it("should normalize tool_calls from Gemini format to flat ChatML format", () => {
const input = [
{
diff --git a/web/src/utils/chatml/adapters/gemini.ts b/web/src/utils/chatml/adapters/gemini.ts
index 3ef48bd844..abba207a3b 100644
--- a/web/src/utils/chatml/adapters/gemini.ts
+++ b/web/src/utils/chatml/adapters/gemini.ts
@@ -1,5 +1,9 @@
import type { NormalizerContext, ProviderAdapter } from "../types";
-import { parseMetadata, stringifyToolResultContent } from "../helpers";
+import {
+ parseMetadata,
+ stringifyToolResultContent,
+ isRichToolResult,
+} from "../helpers";
import { z } from "zod/v4";
/**
@@ -100,8 +104,11 @@ function extractFromParts(parts: unknown[]): {
}
// {text: "..."} or {type: "text", text: "..."}
- if (typeof p.text === "string") {
- textParts.push(p.text);
+ // text can be a string (normal response) or an object (when responseMimeType: "application/json")
+ if (p.text !== undefined && p.text !== null) {
+ textParts.push(
+ typeof p.text === "string" ? p.text : JSON.stringify(p.text, null, 2),
+ );
continue;
}
@@ -271,13 +278,23 @@ function normalizeGeminiMessage(msg: unknown): Record<string, unknown> {
}
}
- // stringify object content for tool result messages
+ // For tool messages with rich object content, spread into message
+ // so it goes to json passthrough field and renders as PrettyJsonView.
+ // Rich = nested structure OR 3+ keys. Simple <=2 scalar keys.
if (
normalized.role === "tool" &&
typeof normalized.content === "object" &&
+ normalized.content !== null &&
!Array.isArray(normalized.content)
) {
- normalized.content = stringifyToolResultContent(normalized.content);
+ if (isRichToolResult(normalized.content)) {
+ // Rich object: spread for table rendering
+ const { content, ...rest } = normalized;
+ return { ...rest, ...content };
+ } else {
+ // Simple object: stringify for text rendering
+ normalized.content = stringifyToolResultContent(normalized.content);
+ }
}
return normalized;
diff --git a/web/src/utils/chatml/adapters/langgraph.ts b/web/src/utils/chatml/adapters/langgraph.ts
index dba952bb4f..3ec216f8ec 100644
--- a/web/src/utils/chatml/adapters/langgraph.ts
+++ b/web/src/utils/chatml/adapters/langgraph.ts
@@ -3,6 +3,7 @@ import {
removeNullFields,
stringifyToolResultContent,
parseMetadata,
+ isRichToolResult,
} from "../helpers";
import { z } from "zod/v4";
@@ -184,13 +185,23 @@ function normalizeMessage(msg: unknown): Record<string, unknown> {
});
}
- // Stringify object/array content for tool messages
+ // For tool messages with rich object content, spread into message
+ // so it goes to json passthrough field → renders as PrettyJsonView.
+ // Rich = nested structure OR 3+ keys. Simple <=2 scalar keys.
if (
normalized.role === "tool" &&
typeof normalized.content === "object" &&
- normalized.content !== null
+ normalized.content !== null &&
+ !Array.isArray(normalized.content)
) {
- normalized.content = stringifyToolResultContent(normalized.content);
+ if (isRichToolResult(normalized.content)) {
+ // Rich object: spread for table rendering
+ const { content, ...rest } = normalized;
+ return { ...rest, ...content };
+ } else {
+ // Simple object: stringify for text rendering
+ normalized.content = stringifyToolResultContent(normalized.content);
+ }
}
return normalized;
diff --git a/web/src/utils/chatml/adapters/microsoft-agent.ts b/web/src/utils/chatml/adapters/microsoft-agent.ts
index 040de45814..b4d935cc64 100644
--- a/web/src/utils/chatml/adapters/microsoft-agent.ts
+++ b/web/src/utils/chatml/adapters/microsoft-agent.ts
@@ -3,6 +3,7 @@ import {
parseMetadata,
getNestedProperty,
stringifyToolResultContent,
+ isRichToolResult,
} from "../helpers";
import { z } from "zod/v4";
@@ -174,13 +175,23 @@ function normalizeMicrosoftAgentMessage(msg: unknown): Record<string, unknown> {
delete normalized.parts;
}
- // Stringify object content for tool result messages
+ // For tool messages with rich object content, spread into message
+ // so it goes to json passthrough field → renders as PrettyJsonView.
+ // Rich = nested structure OR 3+ keys. Simple <=2 scalar keys.
if (
normalized.role === "tool" &&
typeof normalized.content === "object" &&
+ normalized.content !== null &&
!Array.isArray(normalized.content)
) {
- normalized.content = stringifyToolResultContent(normalized.content);
+ if (isRichToolResult(normalized.content)) {
+ // Rich object: spread for table rendering
+ const { content, ...rest } = normalized;
+ return { ...rest, ...content };
+ } else {
+ // Simple object: stringify for text rendering
+ normalized.content = stringifyToolResultContent(normalized.content);
+ }
}
return normalized;
diff --git a/web/src/utils/chatml/adapters/openai.clienttest.ts b/web/src/utils/chatml/adapters/openai.clienttest.ts
index ee6a8ab46f..670db77617 100644
--- a/web/src/utils/chatml/adapters/openai.clienttest.ts
+++ b/web/src/utils/chatml/adapters/openai.clienttest.ts
@@ -160,7 +160,7 @@ describe("OpenAI Adapter", () => {
expect(result.data?.[0].content).toBe("Hello");
});
- it("should stringify tool message object content", () => {
+ it("should stringify simple tool message object content (1-2 scalar keys)", () => {
const input = {
messages: [
{
@@ -173,10 +173,38 @@ describe("OpenAI Adapter", () => {
const result = normalizeInput(input, { framework: "openai" });
expect(result.success).toBe(true);
+ // Simple objects (< 5 keys) get stringified, not spread
expect(typeof result.data?.[0].content).toBe("string");
expect(result.data?.[0].content).toBe(
'{"temperature":72,"conditions":"sunny"}',
);
+ expect(result.data?.[0].tool_call_id).toBe("call_123");
+ });
+
+ it("should spread rich tool message object content (3+ keys or nested) for table rendering", () => {
+ const input = {
+ messages: [
+ {
+ role: "tool",
+ content: {
+ PatientNo: "123",
+ Firstname: "John",
+ Lastname: "Doe",
+ Email: "[email protected]",
+ Mobile: "1234567890",
+ },
+ tool_call_id: "call_456",
+ },
+ ],
+ };
+
+ const result = normalizeInput(input, { framework: "openai" });
+ expect(result.success).toBe(true);
+ // Rich objects (5+ keys) get spread for passthrough rendering
+ expect(result.data?.[0].content).toBeUndefined();
+ expect(result.data?.[0].json?.json?.PatientNo).toBe("123");
+ expect(result.data?.[0].json?.json?.Firstname).toBe("John");
+ expect(result.data?.[0].tool_call_id).toBe("call_456");
});
describe("Chat Completions API", () => {
diff --git a/web/src/utils/chatml/adapters/openai.ts b/web/src/utils/chatml/adapters/openai.ts
index 9b219194e9..f972257e3f 100644
--- a/web/src/utils/chatml/adapters/openai.ts
+++ b/web/src/utils/chatml/adapters/openai.ts
@@ -3,6 +3,7 @@ import {
removeNullFields,
stringifyToolResultContent,
parseMetadata,
+ isRichToolResult,
} from "../helpers";
import { z } from "zod/v4";
@@ -66,6 +67,12 @@ function normalizeMessage(msg: unknown): Record<string, unknown> {
let normalized = removeNullFields(msg);
+ // Normalize camelCase toolCalls to snake_case tool_calls (VAPI framework does this, otherwise it matches OpenAI)
+ if ("toolCalls" in normalized && Array.isArray(normalized.toolCalls)) {
+ normalized.tool_calls = normalized.toolCalls;
+ delete normalized.toolCalls;
+ }
+
// Convert direct function call message to tool_calls array format
// Format: { type: "function_call", name: "...", arguments: {...}, call_id: "..." }
// Convert to: { role: "assistant", tool_calls: [{ id, name, arguments, type }] }
@@ -132,13 +139,23 @@ function normalizeMessage(msg: unknown): Record<string, unknown> {
});
}
- // Stringify object content for tool messages
+ // For tool messages with rich object content, spread into message
+ // so it goes to json passthrough field and renders as PrettyJsonView.
+ // Rich = nested structure OR 3+ keys. Simple <=2 scalar keys.
if (
normalized.role === "tool" &&
typeof normalized.content === "object" &&
+ normalized.content !== null &&
!Array.isArray(normalized.content)
) {
- normalized.content = stringifyToolResultContent(normalized.content);
+ if (isRichToolResult(normalized.content)) {
+ // Rich object: spread for table rendering
+ const { content, ...rest } = normalized;
+ return { ...rest, ...content };
+ } else {
+ // Simple object: stringify for text rendering
+ normalized.content = stringifyToolResultContent(normalized.content);
+ }
}
return normalized;
diff --git a/web/src/utils/chatml/helpers.ts b/web/src/utils/chatml/helpers.ts
index e35881e1ba..b6adc84a2d 100644
--- a/web/src/utils/chatml/helpers.ts
+++ b/web/src/utils/chatml/helpers.ts
@@ -36,6 +36,36 @@ export function stringifyToolResultContent(content: unknown): string {
return JSON.stringify(content);
}
+/**
+ * used to check if a tool call is "complex" to render as PrettyJsonView table
+ * or stringify it and render it as pure string. only used for role: tool type
+ * calls. used for tool calls themselves, not for tool selections by an LLM.
+ *
+ * Rich = has nested structure OR has more than 2 top-level keys
+ * Simple <= 2 keys with only scalar values (strings, numbers, booleans, null)
+ */
+export function isRichToolResult(content: unknown): boolean {
+ if (!content || typeof content !== "object" || Array.isArray(content)) {
+ return false;
+ }
+
+ const keys = Object.keys(content);
+
+ // More than 2 keys → probably rich/structured data
+ if (keys.length > 2) return true;
+
+ // Check if any value is an object or array (nested structure)
+ for (const key of keys) {
+ const value = (content as Record<string, unknown>)[key];
+ if (value && typeof value === "object") {
+ return true; // Has nested structure
+ }
+ }
+
+ // 1-2 keys with only scalar values: simple
+ return false;
+}
+
export function parseMetadata(
metadata: unknown,
): Record<string, unknown> | null {
diff --git a/web/src/utils/chatml/jumptoplayground.clienttest.ts b/web/src/utils/chatml/jumptoplayground.clienttest.ts
index c24354fd2a..c00c3edb01 100644
--- a/web/src/utils/chatml/jumptoplayground.clienttest.ts
+++ b/web/src/utils/chatml/jumptoplayground.clienttest.ts
@@ -617,4 +617,110 @@ describe("Playground Jump Full Pipeline", () => {
expect(tools[1].name).toBe("get_time");
expect(tools[1].description).toBe("Get the current time in a timezone.");
});
+
+ it("should preserve rich tool results when jumping to playground", () => {
+ // Regression test: tool results with many keys(6+) are spread into json passthrough by adapters
+ // (for PrettyJsonView table rendering in trace view). playgroundConverter must fallback
+ // to jsonData when content is undefined to preserve tool result data for playground.
+ const input = {
+ messages: [
+ {
+ role: "assistant",
+ content: "",
+ tool_calls: [
+ {
+ id: "c1",
+ type: "function",
+ function: { name: "verify", arguments: {} },
+ },
+ ],
+ },
+ {
+ role: "tool",
+ content: {
+ PatientNo: "123",
+ First: "J",
+ Last: "D",
+ Email: "[email protected]",
+ Mobile: "555",
+ Active: true,
+ },
+ tool_call_id: "c1",
+ },
+ ],
+ };
+
+ const playgroundMessages = normalizeInput(input, { metadata: input })
+ .data?.map(convertChatMlToPlayground)
+ .filter((m) => m !== null);
+
+ const secondMsg = playgroundMessages?.[1];
+ expect(secondMsg).toBeDefined();
+ expect(secondMsg && "role" in secondMsg ? secondMsg.role : null).toBe(
+ "tool",
+ );
+ expect(
+ secondMsg && "content" in secondMsg ? secondMsg.content : "",
+ ).toContain("PatientNo");
+ });
+
+ it("should handle VAPI camelCase toolCalls and preserve IDs", () => {
+ // VAPI uses camelCase toolCalls instead of tool_calls
+ // Critical: Tool call IDs must be preserved for OpenAI API compatibility
+ const input = {
+ tools: [
+ {
+ type: "function",
+ function: {
+ name: "verify_user",
+ description: "Verify user",
+ parameters: { type: "object", properties: {}, required: [] },
+ },
+ },
+ ],
+ messages: [
+ {
+ role: "assistant",
+ content: "Checking...",
+ toolCalls: [
+ {
+ id: "call_123",
+ type: "function",
+ function: { name: "verify_user", arguments: "{}" },
+ },
+ ],
+ },
+ {
+ role: "tool",
+ content: '{"verified": true}',
+ tool_call_id: "call_123",
+ },
+ ],
+ };
+
+ const inResult = normalizeInput(input, {
+ observationName: "chat-completion",
+ });
+ expect(inResult.success).toBe(true);
+
+ const playgroundMessages = inResult
+ .data!.map(convertChatMlToPlayground)
+ .filter((msg) => msg !== null);
+
+ // Critical assertions: IDs must match
+ const toolCallMsg = playgroundMessages[0];
+ expect(toolCallMsg?.type).toBe("assistant-tool-call");
+ if (toolCallMsg?.type === "assistant-tool-call") {
+ expect(toolCallMsg.toolCalls[0].id).toBe("call_123");
+ }
+
+ const toolResultMsg = playgroundMessages[1];
+ expect(toolResultMsg?.type).toBe("tool-result");
+ if (toolResultMsg?.type === "tool-result") {
+ expect(toolResultMsg.toolCallId).toBe("call_123");
+ }
+
+ // Verify tools extracted
+ expect(extractTools(input)).toHaveLength(1);
+ });
});
diff --git a/web/src/utils/chatml/playgroundConverter.ts b/web/src/utils/chatml/playgroundConverter.ts
index 190fd40481..07879e2737 100644
--- a/web/src/utils/chatml/playgroundConverter.ts
+++ b/web/src/utils/chatml/playgroundConverter.ts
@@ -114,9 +114,17 @@ export function convertChatMlToPlayground(
const toolCallId =
msg.tool_call_id || jsonData?.tool_call_id || jsonData?.toolCallId;
if (toolCallId) {
+ // If content is undefined but we have rich data in json.json (spread tool result),
+ // use that for playground display
+ // this happens if for complex tool calls isRichToolResult applies
+ const toolContent =
+ msg.content !== undefined && msg.content !== null
+ ? msg.content
+ : jsonData;
+
return {
role: ChatMessageRole.Tool,
- content: contentToString(msg.content),
+ content: contentToString(toolContent),
type: ChatMessageType.ToolResult,
toolCallId: toolCallId as string,
};