Skip to content

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

fix(playground): stringify gemini tool results (#9982)

Nimar0e3f8b4f84af
diff --git a/web/src/utils/chatml/adapters/gemini.clienttest.ts b/web/src/utils/chatml/adapters/gemini.clienttest.ts
index 82b80cb6f7..b6ef67fcb6 100644
--- a/web/src/utils/chatml/adapters/gemini.clienttest.ts
+++ b/web/src/utils/chatml/adapters/gemini.clienttest.ts
@@ -114,6 +114,39 @@ describe("geminiAdapter", () => {
expect(result[1].tool_call_id).toBe("call_123");
});
+ it("should stringify object content in tool result messages", () => {
+ const input = [
+ {
+ role: "assistant",
+ content: [{ type: "text", text: "Let me check." }],
+ },
+ {
+ role: "tool",
+ content: {
+ ok: true,
+ data: { userId: 12345 },
+ request: { unitId: 1, name: "Test User" },
+ },
+ tool_call_id: "call_xyz789",
+ },
+ ];
+
+ const result = geminiAdapter.preprocess(input, "input", {}) as any[];
+
+ expect(result.length).toBe(2);
+ expect(result[1].role).toBe("tool");
+ // Tool result content should be 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" },
+ }),
+ );
+ expect(result[1].tool_call_id).toBe("call_xyz789");
+ });
+
it("should handle input wrapped in messages field", () => {
const input = {
messages: [
@@ -140,5 +173,47 @@ describe("geminiAdapter", () => {
expect(result.messages[1].content).toBe("User message");
expect(result.extra).toBe("metadata");
});
+
+ it("should normalize tool_calls from Gemini format to OpenAI format", () => {
+ const input = [
+ {
+ role: "assistant",
+ content: "",
+ tool_calls: [
+ {
+ name: "get_weather",
+ args: {
+ location: "San Francisco",
+ unit: "celsius",
+ },
+ id: "call_abc123",
+ type: "tool_call",
+ },
+ ],
+ },
+ ];
+
+ const result = geminiAdapter.preprocess(input, "input", {}) as any[];
+
+ expect(result.length).toBe(1);
+ expect(result[0].role).toBe("assistant");
+ expect(result[0].tool_calls).toBeDefined();
+ expect(result[0].tool_calls.length).toBe(1);
+
+ // Should be normalized to our ChatML format
+ const toolCall = result[0].tool_calls[0];
+ expect(toolCall.id).toBe("call_abc123");
+ expect(toolCall.type).toBe("function");
+ expect(toolCall.function).toBeDefined();
+ expect(toolCall.function.name).toBe("get_weather");
+ expect(typeof toolCall.function.arguments).toBe("string");
+ expect(toolCall.function.arguments).toBe(
+ JSON.stringify({ location: "San Francisco", unit: "celsius" }),
+ );
+
+ // Old fields should be removed
+ expect(toolCall.name).toBeUndefined();
+ expect(toolCall.args).toBeUndefined();
+ });
});
});
diff --git a/web/src/utils/chatml/adapters/gemini.ts b/web/src/utils/chatml/adapters/gemini.ts
index 259d2ee316..bbd3db7738 100644
--- a/web/src/utils/chatml/adapters/gemini.ts
+++ b/web/src/utils/chatml/adapters/gemini.ts
@@ -1,5 +1,5 @@
import type { NormalizerContext, ProviderAdapter } from "../types";
-import { parseMetadata } from "../helpers";
+import { parseMetadata, stringifyToolResultContent } from "../helpers";
export function isGeminiToolDefinition(msg: unknown): boolean {
if (!msg || typeof msg !== "object") return false;
@@ -37,12 +37,38 @@ export function extractGeminiToolDefinitions(messages: unknown[]): Array<{
});
}
+function normalizeToolCall(toolCall: unknown): Record<string, unknown> {
+ if (!toolCall || typeof toolCall !== "object") return {};
+
+ const tc = toolCall as Record<string, unknown>;
+
+ // is Gemini format?: {name, args, id, type: "tool_call"}
+ if (tc.type === "tool_call" && tc.name && "args" in tc) {
+ // Convert to our ChatML / OpenAI format: {id, type: "function", function: {name, arguments}}
+ return {
+ id: tc.id,
+ type: "function",
+ function: {
+ name: tc.name,
+ arguments:
+ typeof tc.args === "string" ? tc.args : JSON.stringify(tc.args ?? {}),
+ },
+ };
+ }
+
+ return tc;
+}
+
function normalizeGeminiMessage(msg: unknown): Record<string, unknown> {
if (!msg || typeof msg !== "object") return {};
const message = msg as Record<string, unknown>;
const normalized = { ...message };
+ if (normalized.tool_calls && Array.isArray(normalized.tool_calls)) {
+ normalized.tool_calls = normalized.tool_calls.map(normalizeToolCall);
+ }
+
// Gemini structured content: content: [{type: "text", text: "..."}]
// Convert to plain string for ChatML compatibility
if (Array.isArray(message.content)) {
@@ -73,6 +99,17 @@ function normalizeGeminiMessage(msg: unknown): Record<string, unknown> {
}
}
+ // Stringify object content for tool result messages, results should be strings in playground
+ // NOTE: this will probably change down the line as we introduce structured tool results
+ if (
+ normalized.role === "tool" &&
+ typeof normalized.content === "object" &&
+ !Array.isArray(normalized.content) &&
+ !isGeminiToolDefinition(msg)
+ ) {
+ normalized.content = stringifyToolResultContent(normalized.content);
+ }
+
return normalized;
}