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): coerce $ai_error to string in traces list (#57942)

Christiaan Hendriksen264a05157d12
diff --git a/products/llm_analytics/frontend/LLMAnalyticsTracesScene.tsx b/products/llm_analytics/frontend/LLMAnalyticsTracesScene.tsx
index f2396ce80df9..0221932bfb09 100644
--- a/products/llm_analytics/frontend/LLMAnalyticsTracesScene.tsx
+++ b/products/llm_analytics/frontend/LLMAnalyticsTracesScene.tsx
@@ -25,6 +25,7 @@ import { llmAnalyticsTracesTabLogic } from './tabs/llmAnalyticsTracesTabLogic'
import { TraceMessages, traceMessagesLazyLoaderLogic } from './traceMessagesLazyLoaderLogic'
import { traceReviewsLazyLoaderLogic } from './traceReviews/traceReviewsLazyLoaderLogic'
import {
+ formatAiErrorForDisplay,
formatLLMCost,
formatLLMLatency,
formatLLMUsage,
@@ -369,7 +370,7 @@ const OutputMessageColumn: QueryContextColumnComponent = ({ record }) => {
if (errorEventFound) {
return (
<LemonTag type="danger" className="font-mono max-w-50 truncate">
- {errorEventFound.properties?.$ai_error || 'Unknown error'}
+ {formatAiErrorForDisplay(errorEventFound.properties?.$ai_error)}
</LemonTag>
)
}
diff --git a/products/llm_analytics/frontend/utils.test.ts b/products/llm_analytics/frontend/utils.test.ts
index f33c8f5dfcc8..fd5ce398d3ae 100644
--- a/products/llm_analytics/frontend/utils.test.ts
+++ b/products/llm_analytics/frontend/utils.test.ts
@@ -4,6 +4,7 @@ import { AnthropicInputMessage, OpenAICompletionMessage } from './types'
import {
costContextFromProperties,
costContextFromTrace,
+ formatAiErrorForDisplay,
formatLLMEventTitle,
getSessionID,
getSessionStartTimestamp,
@@ -2160,4 +2161,31 @@ describe('LLM Analytics utils', () => {
expect(parseToolArgumentsForDisplay('"hello"')).toEqual({ kind: 'raw', value: '"hello"' })
})
})
+
+ describe('formatAiErrorForDisplay', () => {
+ it.each<[string, unknown, string]>([
+ ['string passes through', 'rate limit exceeded', 'rate limit exceeded'],
+ ['empty string falls back to Unknown error', '', 'Unknown error'],
+ ['null falls back to Unknown error', null, 'Unknown error'],
+ ['undefined falls back to Unknown error', undefined, 'Unknown error'],
+ [
+ 'plain object is JSON-stringified',
+ { message: 'boom', name: 'Error' },
+ '{"message":"boom","name":"Error"}',
+ ],
+ ['array is JSON-stringified', ['a', 'b'], '["a","b"]'],
+ ['number is JSON-stringified', 500, '500'],
+ ['boolean is JSON-stringified', true, 'true'],
+ ])('%s', (_, input, expected) => {
+ expect(formatAiErrorForDisplay(input)).toBe(expected)
+ })
+
+ it('falls back to String() when JSON.stringify throws (e.g. circular refs)', () => {
+ const circular: Record<string, unknown> = {}
+ circular.self = circular
+ // JSON.stringify throws on circular refs; helper should fall back to String() and not throw.
+ expect(() => formatAiErrorForDisplay(circular)).not.toThrow()
+ expect(formatAiErrorForDisplay(circular)).toBe('[object Object]')
+ })
+ })
})
diff --git a/products/llm_analytics/frontend/utils.ts b/products/llm_analytics/frontend/utils.ts
index a8f650952299..9ac0ad7e2a1a 100644
--- a/products/llm_analytics/frontend/utils.ts
+++ b/products/llm_analytics/frontend/utils.ts
@@ -221,6 +221,20 @@ export function formatTokens(tokens: number): string {
return tokens.toFixed(0)
}
+export function formatAiErrorForDisplay(value: unknown): string {
+ if (typeof value === 'string') {
+ return value || 'Unknown error'
+ }
+ if (value == null) {
+ return 'Unknown error'
+ }
+ try {
+ return JSON.stringify(value)
+ } catch {
+ return String(value)
+ }
+}
+
export function formatErrorRate(errorRate: number): string {
const percentage = errorRate * 100
if (percentage === 0) {