:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(llma): render OpenAI Responses API tool calls with metadata (#56019)
Richard Solomou69b29cce2608
diff --git a/products/llm_analytics/frontend/utils.test.ts b/products/llm_analytics/frontend/utils.test.tsindex abeda391dc0e..f33c8f5dfcc8 100644--- a/products/llm_analytics/frontend/utils.test.ts+++ b/products/llm_analytics/frontend/utils.test.ts@@ -639,25 +639,83 @@ describe('LLM Analytics utils', () => {})it.each([- ['web_search_call', 'ws_123', { type: 'web_search_call', id: 'ws_123', status: 'completed' }],+ [+ 'web_search_call',+ 'ws_123',+ 'web_search_call',+ { action: { type: 'search', query: 'weather today' } },+ {+ type: 'web_search_call',+ id: 'ws_123',+ status: 'completed',+ action: { type: 'search', query: 'weather today' },+ },+ ],['code_interpreter_call','ci_123',+ 'code_interpreter_call',+ { code: 'print("hello")' },{ type: 'code_interpreter_call', id: 'ci_123', status: 'completed', code: 'print("hello")' },],- ['image_generation_call', 'ig_123', { type: 'image_generation_call', id: 'ig_123', status: 'completed' }],- ['mcp_call', 'mcp_123', { type: 'mcp_call', id: 'mcp_123', status: 'completed' }],- ['file_search_call', 'fs_123', { type: 'file_search_call', id: 'fs_123', status: 'completed' }],- ['computer_call', 'cc_123', { type: 'computer_call', id: 'cc_123', status: 'completed' }],- ])('parses %s as an assistant tool call', (toolType, toolId, message) => {- expect(normalizeMessage(message, 'user')).toEqual([+ [+ 'image_generation_call',+ 'ig_123',+ 'image_generation_call',+ { prompt: 'a red cat' },+ { type: 'image_generation_call', id: 'ig_123', status: 'completed', prompt: 'a red cat' },+ ],+ [+ 'mcp_call',+ 'mcp_123',+ 'search_docs',+ { query: 'pricing' },{- role: 'assistant',- content: JSON.stringify(message),- tool_calls: [{ type: 'function', id: toolId, function: { name: toolType, arguments: {} } }],+ type: 'mcp_call',+ id: 'mcp_123',+ status: 'completed',+ name: 'search_docs',+ server_label: 'docs',+ arguments: '{"query":"pricing"}',},- ])- })+ ],+ [+ 'file_search_call',+ 'fs_123',+ 'file_search_call',+ { queries: ['q1', 'q2'] },+ { type: 'file_search_call', id: 'fs_123', status: 'completed', queries: ['q1', 'q2'] },+ ],+ [+ 'computer_call',+ 'cc_123',+ 'computer_call',+ { action: { type: 'click', x: 10, y: 20 } },+ {+ type: 'computer_call',+ id: 'cc_123',+ status: 'completed',+ action: { type: 'click', x: 10, y: 20 },+ },+ ],+ ])(+ 'parses %s as an assistant tool call preserving metadata',+ (_toolType, toolId, expectedName, expectedArguments, message) => {+ expect(normalizeMessage(message, 'user')).toEqual([+ {+ role: 'assistant',+ content: '',+ tool_calls: [+ {+ type: 'function',+ id: toolId,+ function: { name: expectedName, arguments: expectedArguments },+ },+ ],+ },+ ])+ }+ )it('handles function_call with unparseable arguments', () => {const message = {@@ -686,7 +744,7 @@ describe('LLM Analytics utils', () => {})})- it('normalizeMessage: handles new array-based content format', () => {+ it('normalizeMessage: lifts function items out of array-based content into tool_calls', () => {const message = {role: 'assistant',content: [@@ -713,6 +771,8 @@ describe('LLM Analytics utils', () => {type: 'text',text: "I'll check the weather for you.",},+ ],+ tool_calls: [{type: 'function',id: 'call_123',@@ -726,6 +786,129 @@ describe('LLM Analytics utils', () => {])})+ describe('normalizeMessage: array-based content with function items', () => {+ it('leaves content empty when every item is a function', () => {+ const message = {+ role: 'assistant',+ content: [+ {+ type: 'function',+ id: 'call_1',+ function: { name: 'get_weather', arguments: { location: 'Berlin' } },+ },+ {+ type: 'function',+ id: 'call_2',+ function: { name: 'search_docs', arguments: { query: 'foo' } },+ },+ ],+ }++ expect(normalizeMessage(message, 'assistant')).toEqual([+ {+ role: 'assistant',+ content: '',+ tool_calls: [+ {+ type: 'function',+ id: 'call_1',+ function: { name: 'get_weather', arguments: { location: 'Berlin' } },+ },+ {+ type: 'function',+ id: 'call_2',+ function: { name: 'search_docs', arguments: { query: 'foo' } },+ },+ ],+ },+ ])+ })++ it.each([+ ['pre-parsed object', { location: 'Berlin', unit: 'celsius' }, { location: 'Berlin', unit: 'celsius' }],+ ['JSON-encoded string', '{"location":"Berlin","unit":"celsius"}', { location: 'Berlin', unit: 'celsius' }],+ ])('parses function arguments provided as %s', (_label, rawArguments, expectedArguments) => {+ const message = {+ role: 'assistant',+ content: [+ {+ type: 'function',+ id: 'call_1',+ function: { name: 'get_weather', arguments: rawArguments },+ },+ ],+ }++ expect(normalizeMessage(message, 'assistant')).toEqual([+ {+ role: 'assistant',+ content: '',+ tool_calls: [+ {+ type: 'function',+ id: 'call_1',+ function: { name: 'get_weather', arguments: expectedArguments },+ },+ ],+ },+ ])+ })++ it('lifts function items into tool_calls and keeps non-function items as content', () => {+ const message = {+ role: 'assistant',+ content: [+ {+ type: 'function',+ id: 'fs_1',+ function: { name: 'file_search', arguments: { queries: ['q'] } },+ },+ { type: 'text', text: 'hi' },+ ],+ }++ expect(normalizeMessage(message, 'assistant')).toEqual([+ {+ role: 'assistant',+ content: [{ type: 'text', text: 'hi' }],+ tool_calls: [+ {+ type: 'function',+ id: 'fs_1',+ function: { name: 'file_search', arguments: { queries: ['q'] } },+ },+ ],+ },+ ])+ })+ })++ describe('normalizeMessage: built-in tool call with explicit name', () => {+ it('uses rawMessage.name for mcp_call, not the "mcp_call" type string', () => {+ const message = {+ type: 'mcp_call',+ id: 'mcp_123',+ name: 'search_docs',+ server_label: 'docs',+ arguments: '{"query":"pricing"}',+ }++ expect(normalizeMessage(message, 'user')).toEqual([+ {+ role: 'assistant',+ content: '',+ tool_calls: [+ {+ type: 'function',+ id: 'mcp_123',+ function: { name: 'search_docs', arguments: { query: 'pricing' } },+ },+ ],+ },+ ])+ })+ })+describe('role preservation in nested content', () => {it('preserves assistant role when recursing into nested array content with output_text', () => {// This is the bug we fixed - messages with role + array content should preserve the rolediff --git a/products/llm_analytics/frontend/utils.ts b/products/llm_analytics/frontend/utils.tsindex 97355c12249f..a8f650952299 100644--- a/products/llm_analytics/frontend/utils.ts+++ b/products/llm_analytics/frontend/utils.ts@@ -931,6 +931,44 @@ export function normalizeMessage(rawMessage: unknown, defaultRole: string): Compitem.type === 'document'))) {+ const toolCalls: CompatToolCall[] = []+ const remainingContent: MultiModalContentItem[] = []+ for (const item of rawMessage.content) {+ if (+ item &&+ typeof item === 'object' &&+ 'type' in item &&+ item.type === 'function' &&+ 'function' in item &&+ item.function &&+ typeof item.function === 'object' &&+ 'name' in item.function &&+ typeof item.function.name === 'string'+ ) {+ const fn = item.function as { name: string; arguments?: string | Record<string, unknown> }+ toolCalls.push({+ type: 'function',+ id: 'id' in item && typeof item.id === 'string' ? item.id : undefined,+ function: {+ name: fn.name,+ arguments: parseToolArguments(fn.arguments ?? {}),+ },+ })+ } else {+ remainingContent.push(item as MultiModalContentItem)+ }+ }++ if (toolCalls.length > 0) {+ return [+ {+ role: roleToUse,+ content: remainingContent.length > 0 ? remainingContent : '',+ tool_calls: toolCalls,+ },+ ]+ }+return [{role: roleToUse,@@ -1160,17 +1198,37 @@ export function normalizeMessage(rawMessage: unknown, defaultRole: string): Comp}// Built-in tool calls (web_search_call, code_interpreter_call, etc.)if (isOpenAIResponsesBuiltinToolCall(rawMessage)) {+ // Prefer an explicit name (e.g. mcp_call.name), fall back to the item type.+ const name =+ 'name' in rawMessage && typeof rawMessage.name === 'string' && rawMessage.name+ ? rawMessage.name+ : rawMessage.type++ let functionArguments: Record<string, unknown> | string+ if ('arguments' in rawMessage && rawMessage.arguments !== undefined && rawMessage.arguments !== null) {+ const rawArgs = rawMessage.arguments+ if (typeof rawArgs === 'string' || (typeof rawArgs === 'object' && !Array.isArray(rawArgs))) {+ functionArguments = parseToolArguments(rawArgs as string | Record<string, unknown>)+ } else {+ functionArguments = { arguments: rawArgs }+ }+ } else {+ // Strip `arguments` too in case it's explicitly null — it would otherwise leak into rest.+ const { id: _id, type: _type, status: _status, name: _name, arguments: _args, ...rest } = rawMessage+ functionArguments = rest+ }+return [{role: 'assistant',- content: JSON.stringify(rawMessage),+ content: '',tool_calls: [{type: 'function',id: rawMessage.id,function: {- name: rawMessage.type,- arguments: {},+ name,+ arguments: functionArguments,},},],