🪢 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(chatml): render OpenAI Responses API request input in trace chat view (#15966)
David Trainad0bc1474acee
diff --git a/packages/shared/src/utils/chatml/adapters/openai.ts b/packages/shared/src/utils/chatml/adapters/openai.tsindex 692d917d2f..ffa19d8540 100644--- a/packages/shared/src/utils/chatml/adapters/openai.ts+++ b/packages/shared/src/utils/chatml/adapters/openai.ts@@ -40,6 +40,91 @@ const OpenAIInputMessagesSchema = z{ message: "Messages with top-level parts are not OpenAI format" },);+// Role-less Responses input item types. normalizeMessage converts the common+// conversation items and preserves built-in tool items as JSON passthrough.+// Reference: https://platform.openai.com/docs/api-reference/responses/create+const RESPONSES_ITEM_TYPES = new Set([+ "reasoning",+ "function_call",+ "tool_call",+ "function_call_output",+ "file_search_call",+ "computer_call",+ "computer_call_output",+ "web_search_call",+ "code_interpreter_call",+ "image_generation_call",+ "local_shell_call",+ "local_shell_call_output",+ "shell_call",+ "shell_call_output",+ "apply_patch_call",+ "apply_patch_call_output",+ "mcp_list_tools",+ "mcp_approval_request",+ "mcp_approval_response",+ "mcp_call",+ "custom_tool_call",+ "custom_tool_call_output",+ "tool_search_call",+ "tool_search_output",+ "compaction",+ "item_reference",+]);++/**+ * Check that a Responses API request `input` array holds role-based messages+ * or typed Responses items normalizeMessage converts or preserves. Rejects string items+ * (embeddings-style input) and messages with top-level parts (Microsoft+ * Agent/Gemini format).+ */+function isResponsesApiInputArray(value: unknown): value is unknown[] {+ return (+ Array.isArray(value) &&+ value.length > 0 &&+ value.every((item) => {+ if (!item || typeof item !== "object") return false;+ const msg = item as Record<string, unknown>;+ if (Array.isArray(msg.parts)) return false;+ if (typeof msg.role === "string") return true;+ return typeof msg.type === "string" && RESPONSES_ITEM_TYPES.has(msg.type);+ })+ );+}++// Bare {input: "..."} is too generic to claim; a string input needs a+// distinctive Responses API request sibling.+const RESPONSES_REQUEST_SIBLINGS = [+ "tools",+ "tool_choice",+ "parallel_tool_calls",+ "instructions",+];++/**+ * Responses API request: {input: [...] | "...", tools?, instructions?, ...}+ * Shared by detect() and preprocessData so detection cannot drift from what+ * preprocessing consumes. Also used by extractAdditionalInput to hide `input`+ * from additional input only when it holds the conversation.+ */+export function isOpenAIResponsesRequest(+ data: unknown,+): data is Record<string, unknown> {+ if (!data || typeof data !== "object" || Array.isArray(data)) return false;+ if ("messages" in data || "output" in data) return false;+ const obj = data as Record<string, unknown>;+ if (isResponsesApiInputArray(obj.input)) return true;+ return (+ typeof obj.input === "string" &&+ obj.input.length > 0 &&+ RESPONSES_REQUEST_SIBLINGS.some((key) => key in obj)+ );+}++const OpenAIInputResponsesSchema = z.custom<Record<string, unknown>>(+ isOpenAIResponsesRequest,+);+// OUTPUT SCHEMAS (responses)const OpenAIOutputResponsesSchema = z.looseObject({output: z.array(z.any()),@@ -68,7 +153,8 @@ const OpenAIOutputSingleMessageSchema = z.looseObject({* Format: { type: "reasoning", content: [...], summary: [...] }*/function extractReasoningContent(item: Record<string, unknown>): {- thinking: Array<{ type: "thinking"; content: string; summary?: string }>;+ thinking?: Array<{ type: "thinking"; content: string; summary?: string }>;+ redacted_thinking?: Array<{ type: "redacted_thinking"; data: string }>;} | null {if (item.type !== "reasoning") return null;@@ -99,7 +185,19 @@ function extractReasoningContent(item: Record<string, unknown>): {.filter(Boolean).join("\n");- if (!contentText && !summaryText) return null;+ if (!contentText && !summaryText) {+ return typeof item.encrypted_content === "string" &&+ item.encrypted_content !== ""+ ? {+ redacted_thinking: [+ {+ type: "redacted_thinking",+ data: item.encrypted_content,+ },+ ],+ }+ : null;+ }return {thinking: [@@ -286,6 +384,17 @@ function flattenToolDefinition(tool: unknown): Record<string, unknown> {return toolDef;}+function normalizeMessagesWithTools(+ messages: unknown[],+ tools: unknown[],+): Array<Record<string, unknown>> {+ const normalizedTools = tools.map(flattenToolDefinition);+ return messages.map((message) => ({+ ...normalizeMessage(message),+ tools: normalizedTools,+ }));+}+function preprocessData(data: unknown): unknown {if (!data) return data;@@ -304,10 +413,7 @@ function preprocessData(data: unknown): unknown {if (Array.isArray(messagesArray) && Array.isArray(obj.tools)) {// Attach tools to all messages- return messagesArray.map((msg) => ({- ...normalizeMessage(msg),- tools: (obj.tools as unknown[]).map(flattenToolDefinition),- }));+ return normalizeMessagesWithTools(messagesArray, obj.tools);}}@@ -325,6 +431,26 @@ function preprocessData(data: unknown): unknown {}}+ // Responses API request: {input: [...] | "...", tools?, instructions?, ...}+ // Reference: https://platform.openai.com/docs/api-reference/responses/create+ if (isOpenAIResponsesRequest(data)) {+ const items =+ typeof data.input === "string"+ ? [{ role: "user", content: data.input }]+ : (data.input as unknown[]);+ const messages =+ typeof data.instructions === "string" && data.instructions !== ""+ ? (+ [{ role: "system", content: data.instructions }] as unknown[]+ ).concat(items)+ : items;+ if (Array.isArray(data.tools)) {+ // Attach tools to all messages+ return normalizeMessagesWithTools(messages, data.tools);+ }+ return messages.map(normalizeMessage);+ }+// Chat Completions response: {choices: [{message: {...}}]}if (typeof data === "object" && !Array.isArray(data) && "choices" in data) {const obj = data as Record<string, unknown>;@@ -482,6 +608,7 @@ export const openAIAdapter: ProviderAdapter = {if (OpenAIInputChatCompletionsSchema.safeParse(ctx.metadata).success)return true;if (OpenAIInputMessagesSchema.safeParse(ctx.metadata).success) return true;+ if (OpenAIInputResponsesSchema.safeParse(ctx.metadata).success) return true;if (OpenAIOutputResponsesSchema.safeParse(ctx.metadata).success)return true;if (OpenAIOutputChoicesSchema.safeParse(ctx.metadata).success) return true;@@ -493,6 +620,7 @@ export const openAIAdapter: ProviderAdapter = {if (OpenAIInputChatCompletionsSchema.safeParse(ctx.data).success)return true;if (OpenAIInputMessagesSchema.safeParse(ctx.data).success) return true;+ if (OpenAIInputResponsesSchema.safeParse(ctx.data).success) return true;if (OpenAIOutputResponsesSchema.safeParse(ctx.data).success) return true;if (OpenAIOutputChoicesSchema.safeParse(ctx.data).success) return true;if (OpenAIOutputSingleMessageSchema.safeParse(ctx.data).success)@@ -508,4 +636,12 @@ export const openAIAdapter: ProviderAdapter = {): unknown {return preprocessData(data);},++ getConsumedInputKeys(data: unknown): string[] {+ if (!isOpenAIResponsesRequest(data)) return [];++ return typeof data.instructions === "string" && data.instructions !== ""+ ? ["input", "instructions"]+ : ["input"];+ },};diff --git a/packages/shared/src/utils/chatml/core.ts b/packages/shared/src/utils/chatml/core.tsindex cbffa16375..064f8e8752 100644--- a/packages/shared/src/utils/chatml/core.ts+++ b/packages/shared/src/utils/chatml/core.ts@@ -84,10 +84,16 @@ export function cleanLegacyOutput(output: unknown, fallback?: unknown) {export function extractAdditionalInput(input: unknown,): Record<string, unknown> | undefined {+ const adapter = selectAdapter({ metadata: input, data: input });+ const consumedInputKeys = new Set(+ ["messages"].concat(adapter.getConsumedInputKeys?.(input) ?? []),+ );const additionalInput =typeof input === "object" && input !== null && !Array.isArray(input)? Object.fromEntries(- Object.entries(input as object).filter(([key]) => key !== "messages"),+ Object.entries(input as object).filter(+ ([key]) => !consumedInputKeys.has(key),+ ),): undefined;diff --git a/packages/shared/src/utils/chatml/types.ts b/packages/shared/src/utils/chatml/types.tsindex 12a72e5afe..61bcb5db9d 100644--- a/packages/shared/src/utils/chatml/types.ts+++ b/packages/shared/src/utils/chatml/types.ts@@ -30,5 +30,6 @@ export interface ProviderAdapter {kind: "input" | "output",ctx: NormalizerContext,): unknown;+ getConsumedInputKeys?(data: unknown): string[];extractToolEvents?(message: Record<string, unknown>): ToolEvent[];}diff --git a/worker/src/__tests__/chatml/adapters/openai.test.ts b/worker/src/__tests__/chatml/adapters/openai.test.tsindex 3c7c3fa406..5e7a91705b 100644--- a/worker/src/__tests__/chatml/adapters/openai.test.ts+++ b/worker/src/__tests__/chatml/adapters/openai.test.ts@@ -3,6 +3,7 @@ import {openAIAdapter,normalizeInput,normalizeOutput,+ extractAdditionalInput,} from "@langfuse/shared";describe("OpenAI Adapter", () => {@@ -315,6 +316,281 @@ describe("OpenAI Adapter", () => {});});+ describe("Responses API request format", () => {+ // Shape logged by SDK wrappers for Responses API calls,+ // e.g. langfuse.openai (langfuse-python >= 4.9.1)+ const responsesRequestInput = {+ input: [+ {+ role: "system",+ content: "# Expense Policy Assistant\nAnswer using policy documents.",+ },+ {+ role: "user",+ content: [+ { type: "input_text", text: "Can I expense a $50 team lunch?" },+ ],+ },+ {+ id: "rs_123",+ summary: [],+ type: "reasoning",+ content: [],+ encrypted_content: "gAAAAA...",+ },+ {+ arguments: '{"query":"team lunch limit"}',+ call_id: "call_123",+ name: "SearchExpensePolicy",+ type: "function_call",+ id: "fc_123",+ status: "completed",+ },+ {+ call_id: "call_123",+ output: "Meals with clients are capped at $75 per person.",+ type: "function_call_output",+ },+ ],+ tools: [+ {+ name: "SearchExpensePolicy",+ parameters: {+ type: "object",+ properties: { query: { type: "string" } },+ required: ["query"],+ },+ strict: true,+ type: "function",+ description: "Search the expense policy",+ },+ ],+ // Junk value logged by an SDK bug; must not break parsing+ tool_choice: "<openai.Omit object at 0x104f0e900>",+ parallel_tool_calls: false,+ };++ it("should detect Responses API request format structurally", () => {+ expect(+ openAIAdapter.detect({ metadata: {}, data: responsesRequestInput }),+ ).toBe(true);++ expect(openAIAdapter.detect({ metadata: responsesRequestInput })).toBe(+ true,+ );++ // Embeddings-style {input: [...strings]} is not a chat request+ expect(+ openAIAdapter.detect({+ metadata: {},+ data: {+ input: ["some text", "other text"],+ model: "text-embedding-3-small",+ },+ }),+ ).toBe(false);++ // Top-level parts identify Microsoft Agent/Gemini, not OpenAI+ expect(+ openAIAdapter.detect({+ metadata: {},+ data: {+ input: [{ role: "user", parts: [{ type: "text", content: "Hi" }] }],+ },+ }),+ ).toBe(false);++ // Role-less items with LangChain types are not Responses API items+ expect(+ openAIAdapter.detect({+ metadata: {},+ data: {+ input: [+ { type: "human", content: "hi" },+ { type: "ai", content: "hello" },+ ],+ },+ }),+ ).toBe(false);+ });++ it("should treat input array as messages and attach tools (real-world SDK payload)", () => {+ // No framework/name hints: exercises structural detection end-to-end+ const result = normalizeInput(responsesRequestInput, {});++ expect(result.success).toBe(true);+ expect(result.data).toHaveLength(5);++ expect(result.data?.[0].role).toBe("system");+ expect(result.data?.[1].role).toBe("user");+ expect(Array.isArray(result.data?.[1].content)).toBe(true);++ // Encrypted-only reasoning renders as redacted thinking, not a roleless+ // JSON bubble.+ expect(result.data?.[2].role).toBe("assistant");+ expect(result.data?.[2].content).toBe("");+ expect(result.data?.[2].redacted_thinking).toEqual([+ { type: "redacted_thinking", data: "gAAAAA..." },+ ]);++ // function_call item converted to assistant message with tool_calls+ expect(result.data?.[3].role).toBe("assistant");+ expect(result.data?.[3].tool_calls?.[0]).toEqual({+ id: "call_123",+ name: "SearchExpensePolicy",+ arguments: '{"query":"team lunch limit"}',+ type: "function",+ });++ // function_call_output item converted to tool message+ expect(result.data?.[4].role).toBe("tool");+ expect(result.data?.[4].tool_call_id).toBe("call_123");+ expect(result.data?.[4].content).toBe(+ "Meals with clients are capped at $75 per person.",+ );++ // Tool definitions attached to all messages+ expect(result.data?.[0].tools).toHaveLength(1);+ expect(result.data?.[0].tools?.[0].name).toBe("SearchExpensePolicy");+ expect(result.data?.[0].tools?.[0].description).toBe(+ "Search the expense policy",+ );+ });++ it("should not duplicate the conversation in additional input", () => {+ const additionalInput = extractAdditionalInput(responsesRequestInput);++ expect(additionalInput?.input).toBeUndefined();+ expect(additionalInput?.tool_choice).toBe(+ "<openai.Omit object at 0x104f0e900>",+ );+ expect(additionalInput?.parallel_tool_calls).toBe(false);+ });++ it("should keep an unconsumed input sibling in additional input", () => {+ // e.g. LangGraph state: `messages` holds the conversation, not `input`+ const additionalInput = extractAdditionalInput({+ messages: [{ role: "user", content: "Hi" }],+ input: "user question",+ });++ expect(additionalInput?.input).toBe("user question");+ expect(additionalInput?.messages).toBeUndefined();+ });++ it("should handle input array without tools", () => {+ const input = {+ input: [+ { role: "user", content: "What is the meaning of life?" },+ {+ type: "reasoning",+ summary: [{ type: "summary_text", text: "Considering philosophy" }],+ content: [],+ },+ ],+ };++ const result = normalizeInput(input, { framework: "openai" });++ expect(result.success).toBe(true);+ expect(result.data?.[0].role).toBe("user");+ expect(result.data?.[1].role).toBe("assistant");+ expect(result.data?.[1].thinking?.[0].content).toBe(+ "Considering philosophy",+ );+ });++ it("should handle built-in tool items in a Responses API conversation", () => {+ const input = {+ input: [+ { role: "user", content: "Search for the latest policy" },+ {+ type: "web_search_call",+ id: "ws_123",+ status: "completed",+ action: { type: "search", query: "latest expense policy" },+ },+ ],+ };++ expect(openAIAdapter.detect({ metadata: {}, data: input })).toBe(true);++ const result = normalizeInput(input, {});++ expect(result.success).toBe(true);+ expect(result.data).toHaveLength(2);+ expect(result.data?.[0].role).toBe("user");+ expect(result.data?.[1]).toMatchObject({+ type: "web_search_call",+ json: {+ id: "ws_123",+ status: "completed",+ action: { type: "search", query: "latest expense policy" },+ },+ });+ });++ it("should map string input with a tools sibling to a user message", () => {+ // langfuse-python logs {input: "...", tools, ...} when tool fields are set+ const input = {+ input: "Can I expense a $50 team lunch?",+ tools: [{ name: "get_weather", type: "function", description: "" }],+ tool_choice: "auto",+ };++ expect(openAIAdapter.detect({ metadata: {}, data: input })).toBe(true);++ const result = normalizeInput(input, {});++ expect(result.success).toBe(true);+ expect(result.data).toHaveLength(1);+ expect(result.data?.[0].role).toBe("user");+ expect(result.data?.[0].content).toBe("Can I expense a $50 team lunch?");+ expect(result.data?.[0].tools?.[0].name).toBe("get_weather");+ });++ it("should prepend instructions as a system message for string input", () => {+ const input = {+ instructions: "You are an expense policy assistant.",+ input: "Can I expense a $50 team lunch?",+ };++ expect(openAIAdapter.detect({ metadata: {}, data: input })).toBe(true);++ const result = normalizeInput(input, {});++ expect(result.success).toBe(true);+ expect(result.data).toHaveLength(2);+ expect(result.data?.[0].role).toBe("system");+ expect(result.data?.[0].content).toBe(+ "You are an expense policy assistant.",+ );+ expect(result.data?.[1].role).toBe("user");+ expect(result.data?.[1].content).toBe("Can I expense a $50 team lunch?");++ expect(extractAdditionalInput(input)).toEqual({});+ });++ it("should not claim bare string input without request siblings", () => {+ const input = { input: "just some text" };++ expect(openAIAdapter.detect({ metadata: {}, data: input })).toBe(false);++ const result = normalizeInput(input, { framework: "openai" });++ expect(result.success).toBe(false);+ });++ it("should not treat embeddings-style string input as chat", () => {+ const result = normalizeInput(+ { input: ["some text", "other text"], model: "text-embedding-3-small" },+ { framework: "openai" },+ );++ expect(result.success).toBe(false);+ });+ });+describe("Chat Completions API", () => {it("should handle request format with tools and flatten tool_calls", () => {const input = {