🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix: handle malformed tool calls in chatml adapters (#10847)
Michael Fröhlich01001dd1909b
diff --git a/web/src/utils/chatml/adapters/langgraph.clienttest.ts b/web/src/utils/chatml/adapters/langgraph.clienttest.tsindex 591fb9d113..8e33dcf49a 100644--- a/web/src/utils/chatml/adapters/langgraph.clienttest.ts+++ b/web/src/utils/chatml/adapters/langgraph.clienttest.ts@@ -182,6 +182,34 @@ describe("LangGraph Adapter", () => {expect(result.data?.[1].tools?.[0].name).toBe("Web-Search");});+ it("should skip null entries in tool_calls arrays", () => {+ const input = [+ {+ role: "assistant",+ content: "",+ tool_calls: [+ null,+ {+ id: "call_valid",+ type: "function",+ function: {+ name: "search_docs",+ arguments: { topic: "Langfuse" },+ },+ },+ ],+ },+ ];++ const result = normalizeInput(input, { framework: "langgraph" });+ expect(result.success).toBe(true);++ const toolCalls = result.data?.[0].tool_calls;+ expect(toolCalls?.length).toBe(1);+ expect(toolCalls?.[0].name).toBe("search_docs");+ expect(toolCalls?.[0].arguments).toBe('{"topic":"Langfuse"}');+ });+it("should clean up additional_kwargs with null values", () => {const output = {role: "assistant",diff --git a/web/src/utils/chatml/adapters/langgraph.ts b/web/src/utils/chatml/adapters/langgraph.tsindex d138f2edd2..ec4726b12f 100644--- a/web/src/utils/chatml/adapters/langgraph.ts+++ b/web/src/utils/chatml/adapters/langgraph.ts@@ -154,9 +154,12 @@ function normalizeMessage(msg: unknown): Record<string, unknown> {// LangGraph can have OpenAI-style nested format: {id, type, function: {name, arguments}}// Convert to flat format: {id, name, arguments, type}if (normalized.tool_calls && Array.isArray(normalized.tool_calls)) {- normalized.tool_calls = (- normalized.tool_calls as Record<string, unknown>[]- ).map((tc) => {+ const sanitizedToolCalls = (normalized.tool_calls as unknown[]).filter(+ (tc): tc is Record<string, unknown> =>+ Boolean(tc) && typeof tc === "object",+ );++ normalized.tool_calls = sanitizedToolCalls.map((tc) => {// If nested format (has function.name), flatten itif (tc.function && typeof tc.function === "object") {const func = tc.function as Record<string, unknown>;diff --git a/web/src/utils/chatml/adapters/openai.clienttest.ts b/web/src/utils/chatml/adapters/openai.clienttest.tsindex 92eb4f2d62..6554663c46 100644--- a/web/src/utils/chatml/adapters/openai.clienttest.ts+++ b/web/src/utils/chatml/adapters/openai.clienttest.ts@@ -161,6 +161,36 @@ describe("OpenAI Adapter", () => {expect(toolCalls?.[0].arguments).toBe('{"city":"NYC","units":"celsius"}');});+ it("should skip null entries in tool_calls arrays", () => {+ const input = {+ messages: [+ {+ role: "assistant",+ content: "",+ tool_calls: [+ null,+ {+ id: "call_valid",+ type: "function",+ function: {+ name: "get_balance",+ arguments: { account: "123" },+ },+ },+ ],+ },+ ],+ };++ const result = normalizeInput(input, { framework: "openai" });+ expect(result.success).toBe(true);++ const toolCalls = result.data?.[0].tool_calls;+ expect(toolCalls?.length).toBe(1);+ expect(toolCalls?.[0].name).toBe("get_balance");+ expect(toolCalls?.[0].arguments).toBe('{"account":"123"}');+ });+it("should handle multimodal content (array of parts)", () => {const input = {messages: [diff --git a/web/src/utils/chatml/adapters/openai.ts b/web/src/utils/chatml/adapters/openai.tsindex 7a82773ecf..61558e37b6 100644--- a/web/src/utils/chatml/adapters/openai.ts+++ b/web/src/utils/chatml/adapters/openai.ts@@ -158,9 +158,12 @@ function normalizeMessage(msg: unknown): Record<string, unknown> {// Format: { id, type: "function", function: { name, arguments } }// Convert to: { id, name, arguments, type }if (normalized.tool_calls && Array.isArray(normalized.tool_calls)) {- normalized.tool_calls = (- normalized.tool_calls as Record<string, unknown>[]- ).map((tc) => {+ const sanitizedToolCalls = (normalized.tool_calls as unknown[]).filter(+ (tc): tc is Record<string, unknown> =>+ Boolean(tc) && typeof tc === "object",+ );++ normalized.tool_calls = sanitizedToolCalls.map((tc) => {if (tc.function && typeof tc.function === "object") {const func = tc.function as Record<string, unknown>;return {