Skip to content

: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.

fix(llma): skip tool calls when picking last trace output message (#68309)

Sean Mallia9edd75725df9
diff --git a/products/ai_observability/frontend/AIObservabilityTracesScene.tsx b/products/ai_observability/frontend/AIObservabilityTracesScene.tsx
index 688a6b211e42..bf202acf75f5 100644
--- a/products/ai_observability/frontend/AIObservabilityTracesScene.tsx
+++ b/products/ai_observability/frontend/AIObservabilityTracesScene.tsx
@@ -31,6 +31,10 @@ import {
formatLLMLatency,
formatLLMUsage,
getTraceTimestamp,
+ INTERNAL_TOOL_RESULT_ROLE,
+ isInternalToolResultUserMessage,
+ isToolResult,
+ isToolStepItem,
LLM_TRACES_PAGE_SIZE,
sanitizeTraceUrlSearchParams,
} from './utils'
@@ -444,12 +448,39 @@ export function pickLastInputMessage(
}
/**
- * Preferred → fallback cascade for the trace output column. We prefer the
- * last assistant message with real content, but fall back to the last
- * displayable message (e.g. tool_calls) so tool-calling traces still show
- * something useful instead of a dash.
+ * A message is "tool traffic" (a tool call or a tool result) rather than a
+ * user-facing turn. Tool calls frequently end an agent chain, but the traces
+ * list should surface the last human-readable answer, not the machinery that
+ * produced it. Covers the explicit `tool_calls` field, tool-result roles, and
+ * content arrays made up entirely of tool-call / tool-result parts — while
+ * still treating a message that mixes real text with a tool call as
+ * user-facing.
*/
-function pickLastOutputMessage(
+function isToolMessage(message: NormalizedMessage): boolean {
+ const { role, content, tool_calls } = message as NormalizedMessage & { tool_calls?: unknown }
+ const hasText =
+ (typeof content === 'string' && content.trim().length > 0) ||
+ (Array.isArray(content) && content.some((item) => !isToolStepItem(item) && !isToolResult(item)))
+ if (Array.isArray(tool_calls) && tool_calls.length > 0) {
+ return !hasText
+ }
+ if (role === 'tool' || role === INTERNAL_TOOL_RESULT_ROLE || isInternalToolResultUserMessage(message)) {
+ return true
+ }
+ if (Array.isArray(content) && content.length > 0 && !hasText) {
+ return true
+ }
+ return false
+}
+
+/**
+ * Preferred → fallback cascade for the trace output column. We prefer the last
+ * assistant message that carries user-facing content, skipping pure tool calls
+ * and tool results so a tool-calling agent chain still shows its last readable
+ * answer. Only when a trace has nothing but tool traffic do we fall back to the
+ * last displayable message, so those traces still show something instead of a dash.
+ */
+export function pickLastOutputMessage(
raw: unknown,
{ strict }: { strict: boolean } = { strict: false }
): NormalizedMessage | null {
@@ -457,15 +488,15 @@ function pickLastOutputMessage(
if (normalized.length === 0) {
return null
}
- for (let i = normalized.length - 1; i >= 0; i--) {
- if (normalized[i].role === 'assistant' && hasDisplayableContent(normalized[i])) {
- return normalized[i]
- }
+ const lastAssistant = normalized.findLast(
+ (m) => m.role === 'assistant' && hasDisplayableContent(m) && !isToolMessage(m)
+ )
+ if (lastAssistant) {
+ return lastAssistant
}
- for (let i = normalized.length - 1; i >= 0; i--) {
- if (hasDisplayableContent(normalized[i])) {
- return normalized[i]
- }
+ const lastDisplayable = normalized.findLast(hasDisplayableContent)
+ if (lastDisplayable) {
+ return lastDisplayable
}
return normalized[normalized.length - 1]
}
diff --git a/products/ai_observability/frontend/tracesOutputPreview.test.ts b/products/ai_observability/frontend/tracesOutputPreview.test.ts
new file mode 100644
index 000000000000..85d89d5ef6ba
--- /dev/null
+++ b/products/ai_observability/frontend/tracesOutputPreview.test.ts
@@ -0,0 +1,77 @@
+import { pickLastOutputMessage } from './AIObservabilityTracesScene'
+
+describe('pickLastOutputMessage', () => {
+ it('returns null for empty or unusable input', () => {
+ expect(pickLastOutputMessage(null)).toBeNull()
+ expect(pickLastOutputMessage([])).toBeNull()
+ })
+
+ it('skips a trailing tool call and picks the last user-facing assistant answer', () => {
+ const conversation = [
+ { role: 'user', content: 'what is the weather?' },
+ { role: 'assistant', content: 'It is 72F.' },
+ {
+ role: 'assistant',
+ content: '',
+ tool_calls: [{ id: 'c1', type: 'function', function: { name: 'get_weather', arguments: '{}' } }],
+ },
+ ]
+ expect(pickLastOutputMessage(conversation)).toMatchObject({ role: 'assistant', content: 'It is 72F.' })
+ })
+
+ it('skips a trailing tool result and picks the last assistant answer', () => {
+ const conversation = [
+ { role: 'assistant', content: 'Let me check.' },
+ {
+ role: 'assistant',
+ content: '',
+ tool_calls: [{ id: 'c1', type: 'function', function: { name: 'get_weather', arguments: '{}' } }],
+ },
+ { role: 'tool', content: '72F', tool_call_id: 'c1' },
+ ]
+ expect(pickLastOutputMessage(conversation)).toMatchObject({ role: 'assistant', content: 'Let me check.' })
+ })
+
+ it('keeps an assistant message that mixes real text with a tool call', () => {
+ const conversation = [
+ { role: 'user', content: 'book a table' },
+ {
+ role: 'assistant',
+ content: 'Booking that now.',
+ tool_calls: [{ id: 'c1', type: 'function', function: { name: 'book', arguments: '{}' } }],
+ },
+ ]
+ expect(pickLastOutputMessage(conversation)).toMatchObject({
+ role: 'assistant',
+ content: 'Booking that now.',
+ })
+ })
+
+ it('falls back to the tool call when the trace has nothing but tool traffic', () => {
+ const conversation = [
+ { role: 'user', content: 'what is the weather?' },
+ {
+ role: 'assistant',
+ content: '',
+ tool_calls: [{ id: 'c1', type: 'function', function: { name: 'get_weather', arguments: '{}' } }],
+ },
+ ]
+ const picked = pickLastOutputMessage(conversation)
+ expect(picked).not.toBeNull()
+ expect(picked?.tool_calls?.[0]?.function?.name).toBe('get_weather')
+ })
+
+ it('unwraps a { messages } state container before picking the output', () => {
+ const state = {
+ messages: [
+ { role: 'assistant', content: 'final answer' },
+ { role: 'tool', content: 'tool output', tool_call_id: 'c1' },
+ ],
+ }
+ expect(pickLastOutputMessage(state)).toMatchObject({ role: 'assistant', content: 'final answer' })
+ })
+
+ it('rejects unknown state-wrapper shapes in strict mode', () => {
+ expect(pickLastOutputMessage({ current_step: 3 }, { strict: true })).toBeNull()
+ })
+})