🪢 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(trace-ui): don't crash UI when null item in message array (#10451)
Nimarddd1085db8ec
diff --git a/web/src/utils/chatml/adapters/aisdk.clienttest.ts b/web/src/utils/chatml/adapters/aisdk.clienttest.tsindex b19b01bf64..528506e027 100644--- a/web/src/utils/chatml/adapters/aisdk.clienttest.ts+++ b/web/src/utils/chatml/adapters/aisdk.clienttest.ts@@ -1,3 +1,6 @@+// BaseChatMlMessageSchema was extracted to shared package in IO read-time extraction+// now ChatMlSchema.ts imports it, so our test import chain pulls it in:+// aisdk.clienttest → adapters/index → core → ChatMlSchema → @langfuse/sharedjest.mock("@langfuse/shared", () => {const { z } = require("zod/v4");@@ -15,18 +18,11 @@ jest.mock("@langfuse/shared", () => {role: z.string().optional(),name: z.string().optional(),content: z- .union([- z.record(z.string(), z.any()),- z.string(),- z.array(z.any()),- z.any(), // Simplified - was OpenAIContentSchema- ])- .nullish(),- audio: z.any().optional(),- additional_kwargs: z.record(z.string(), z.any()).optional(),- tools: z.array(z.any()).optional(),+ .union([z.record(z.string(), z.any()), z.string(), z.array(z.any())])+ .optional(),tool_calls: z.array(z.any()).optional(),tool_call_id: z.string().optional(),+ additional_kwargs: z.record(z.string(), z.any()).optional(),}).passthrough(),};diff --git a/web/src/utils/chatml/adapters/generic.clienttest.ts b/web/src/utils/chatml/adapters/generic.clienttest.tsindex 9ed5a234e9..5b79a05855 100644--- a/web/src/utils/chatml/adapters/generic.clienttest.ts+++ b/web/src/utils/chatml/adapters/generic.clienttest.ts@@ -120,4 +120,53 @@ describe("Generic Adapter", () => {expect(messages[0].content).toBe("Hello World");expect(messages[0].parts).toBeUndefined();});++ it("should not crash when parts array contains null items", () => {+ // Bug: hasFunctionCall/hasFunctionResponse checked properties without null guards+ const input = [+ {+ role: "user",+ parts: [+ { type: "text", content: "Hello" },+ null, // This null item should not crash+ { type: "text", content: "World" },+ ],+ },+ ];++ // Should not throw TypeError+ expect(() => genericAdapter.preprocess(input, "input", {})).not.toThrow();++ const preprocessed = genericAdapter.preprocess(input, "input", {});+ const messages = preprocessed as any[];+ expect(messages[0].content).toContain("Hello");+ expect(messages[0].content).toContain("World");+ });++ it("should not crash when parts has null before function_call", () => {+ // Test hasFunctionCall code path with null+ const input = [+ {+ role: "assistant",+ parts: [+ null, // Null before function check+ { function_call: { name: "test", arguments: "{}" } },+ ],+ },+ ];++ expect(() => genericAdapter.preprocess(input, "input", {})).not.toThrow();+ });++ it("should not crash when parts has null before tool_call", () => {+ // Test Microsoft Agent tool_call type with null+ const input = [+ {+ role: "assistant",+ parts: [null, { type: "tool_call", id: "123", name: "test" }],+ },+ ];++ expect(() => genericAdapter.preprocess(input, "input", {})).not.toThrow();+ });});diff --git a/web/src/utils/chatml/adapters/generic.ts b/web/src/utils/chatml/adapters/generic.tsindex e05f8d681b..5a45536864 100644--- a/web/src/utils/chatml/adapters/generic.ts+++ b/web/src/utils/chatml/adapters/generic.ts@@ -15,10 +15,18 @@ function normalizeGoogleMessage(msg: unknown): Record<string, unknown> {if (message.parts && Array.isArray(message.parts)) {// Check if parts contain function_call, function_response, or Microsoft Agent tool callsconst hasFunctionCall = message.parts.some((p: unknown) => {+ // Null guard: ensure p exists and is an object before accessing properties+ if (!p || typeof p !== "object") {+ return false;+ }const part = p as Record<string, unknown>;return part.function_call || part.type === "tool_call";});const hasFunctionResponse = message.parts.some((p: unknown) => {+ // Null guard: ensure p exists and is an object before accessing properties+ if (!p || typeof p !== "object") {+ return false;+ }const part = p as Record<string, unknown>;return part.function_response || part.type === "tool_call_response";});diff --git a/web/src/utils/chatml/adapters/openai.clienttest.ts b/web/src/utils/chatml/adapters/openai.clienttest.tsindex 37663e36f0..92eb4f2d62 100644--- a/web/src/utils/chatml/adapters/openai.clienttest.ts+++ b/web/src/utils/chatml/adapters/openai.clienttest.ts@@ -110,6 +110,25 @@ describe("OpenAI Adapter", () => {// Should also reject when passed as dataexpect(openAIAdapter.detect({ metadata: {}, data: input })).toBe(false);});++ it("should not crash when detecting messages array with null items", () => {+ // tests for bug when detection code tried to access .type on null items+ const messagesWithNull = {+ messages: [+ { role: "user", content: "Hello" },+ null, // This null item should not crash detection+ { role: "assistant", content: "Hi there!" },+ ],+ };++ // Should not throw TypeError when detecting+ expect(() =>+ openAIAdapter.detect({ metadata: messagesWithNull }),+ ).not.toThrow();++ // Should still successfully detect as OpenAI format despite null items+ expect(openAIAdapter.detect({ metadata: messagesWithNull })).toBe(true);+ });});it("should normalize tool_calls arguments to JSON strings", () => {diff --git a/web/src/utils/chatml/adapters/openai.ts b/web/src/utils/chatml/adapters/openai.tsindex 4bccb13d49..7a82773ecf 100644--- a/web/src/utils/chatml/adapters/openai.ts+++ b/web/src/utils/chatml/adapters/openai.ts@@ -332,6 +332,10 @@ export const openAIAdapter: ProviderAdapter = {const messages = (ctx.metadata as Record<string, unknown>).messages;if (Array.isArray(messages)) {const hasLangChainType = messages.some((msg: unknown) => {+ // Null guard: ensure msg exists and is an object before accessing properties+ if (!msg || typeof msg !== "object") {+ return false;+ }const message = msg as Record<string, unknown>;return (message.type &&