: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
fix(aio): show trace-level evaluation runs in trace view evals tab (#69915)
Carlos Marchal12cef4922ffa
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 97b724bbf886..9cdb7a50d333 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -4979,9 +4979,9 @@ snapshots:scenes-app-ai-observability-skills--skill-detail-stacked-below-2-xl-breakpoint--wide--light:hash: v1.k794b7964.c11f045bc7ed6084b7f7f19bd99149c5111512aeb77416ef125fb1a41aeb77e8.98xhehD94Y0U5Psv6_cm1LcBdamkWuJOZUCT_LcPTlsscenes-app-ai-observability-trace--full--dark:- hash: v1.k794b7964.055ce63e7fdedc902cecaab975aa4e19aef0027f888eb836e188b9037ec175d3.z5k4nHzXbs9Vku0Lc-p3cmJ062oLPC8pjZWYYNW8VAU+ hash: v1.k794b7964.3ea0677df8e51f08cfd46ad1957c78cb0e0c14dff003b15959f9dc9434a239ca.GUZTGarCld-XKdmLqsfKRE1ZIWdWvfojoa2vHPGVVOAscenes-app-ai-observability-trace--full--light:- hash: v1.k794b7964.94ccb1c0676dfe56f2dfe450522e796570997788f5fd24f01b44230b64b5c31b.6nCJBKNf3JV1b9HV5LjK20lGJTLTfJFk72reYi3zw4o+ hash: v1.k794b7964.ccfd240291b68c038036220ca359b4987c4ac7323a1e000037cc4ca5b82d8ec9.i9mrpnbsbZ1684VkG-73iX07RcINQT0AeFz9Dpcb9dAscenes-app-ai-observability-trace--full-specific-event--dark:hash: v1.k794b7964.9e2b03887812835f8023784604140d29f446039730a7dc25b79e36cde1ed1b6d.QHqBYIGjK-VR1vwG9_SzRgH3BeHv6k00HcHn9RAnM10scenes-app-ai-observability-trace--full-specific-event--light:diff --git a/products/ai_observability/frontend/AIObservabilityTraceScene.tsx b/products/ai_observability/frontend/AIObservabilityTraceScene.tsxindex 8f0f27cc5ea8..60a77a66e0ee 100644--- a/products/ai_observability/frontend/AIObservabilityTraceScene.tsx+++ b/products/ai_observability/frontend/AIObservabilityTraceScene.tsx@@ -612,7 +612,7 @@ function UsageChip({ event }: { event: LLMTraceEvent | LLMTrace }): JSX.Elementfunction CreateSentimentEvaluationButton({ traceId }: { traceId: string }): JSX.Element | null {const { generationEvaluationRuns, generationEvaluationRunsLoading } = useValues(- generationEvaluationRunsLogic({ lookupBy: 'trace', traceId })+ generationEvaluationRunsLogic({ traceId }))const { hasLoadedSentimentEvaluations, hasSentimentEvaluations, sentimentEvaluationsLoading } = useValues(sentimentEvaluationAvailabilityLogic@@ -1433,10 +1433,9 @@ const EventContent = React.memo(const isGenerationEvent = event && isLLMEvent(event) && event.event === '$ai_generation'- // Check if the originally selected event (effectiveEventId) is a generation event- // This ensures the Evaluations tab stays visible even when viewing Summary at trace level.- // When effectiveEventId is null (e.g. tab=summary suppresses auto-selection), fall back to- // the first generation in the tree so the Evals tab remains visible.+ // The Evaluations tab is trace-scoped, but manually running an evaluation targets a single+ // generation. When the selected event isn't a generation (or nothing is selected), fall back+ // to the first generation in the tree so the run action stays available.const firstGenerationNode = tree.find((node) => node.event.event === '$ai_generation') ?? nullconst effectiveEventNode = effectiveEventId ? findNodeForEvent(tree, effectiveEventId) : firstGenerationNodeconst isEffectiveEventGeneration = effectiveEventNode?.event.event === '$ai_generation'@@ -1455,7 +1454,10 @@ const EventContent = React.memo(const showSaveToDatasetButton = featureFlags[FEATURE_FLAGS.LLM_ANALYTICS_DATASETS]- const showEvalsTab = !!effectiveGenerationEvent+ // Badges are context-sensitive: a generation shows its own results, the trace root shows+ // trace-target results. Other events (spans, embeddings) have no evaluation context.+ const isTraceRoot = !!event && !isLLMEvent(event)+ const showEvalBadges = isGenerationEvent || isTraceRootconst showTagsTab = effectiveGenerationEvent && !!featureFlags[FEATURE_FLAGS.LLM_ANALYTICS_TAGS]// If the user is viewing the Tags tab but it's no longer available (flag off or@@ -1542,7 +1544,7 @@ const EventContent = React.memo(/>)}{isLLMEvent(event) && <ParametersHeader eventProperties={event.properties} />}- {(aggregation || showEvalsTab) && (+ {(aggregation || showEvalBadges) && (<div className="flex flex-col gap-1">{aggregation && (<div className="flex flex-row flex-wrap items-center gap-2">@@ -1564,8 +1566,11 @@ const EventContent = React.memo()}</div>)}- {showEvalsTab && (- <EvalResultBadges generationEventId={effectiveGenerationEvent.id} />+ {showEvalBadges && (+ <EvalResultBadges+ traceId={trace.id}+ generationEventId={isGenerationEvent ? event.id : undefined}+ />)}</div>)}@@ -1767,23 +1772,18 @@ const EventContent = React.memo(},]: []),- ...(showEvalsTab- ? [- {- key: TraceViewMode.Evals,- label: 'Evaluations',- 'data-attr': 'llma-trace-evals-tab',- content: (- <EvalsTabContent- generationEventId={effectiveGenerationEvent.id}- timestamp={effectiveGenerationEvent.createdAt}- event={effectiveGenerationEvent.event}- distinctId={trace.distinctId}- />- ),- },- ]- : []),+ {+ key: TraceViewMode.Evals,+ label: 'Evaluations',+ 'data-attr': 'llma-trace-evals-tab',+ content: (+ <EvalsTabContent+ traceId={trace.id}+ generationEvent={effectiveGenerationEvent}+ distinctId={trace.distinctId}+ />+ ),+ },...(showTagsTab? [{diff --git a/products/ai_observability/frontend/components/EvalResultBadges.test.tsx b/products/ai_observability/frontend/components/EvalResultBadges.test.tsxindex 1f4e003b2789..a7fb02914410 100644--- a/products/ai_observability/frontend/components/EvalResultBadges.test.tsx+++ b/products/ai_observability/frontend/components/EvalResultBadges.test.tsx@@ -1,5 +1,5 @@import { EvaluationRun } from '../evaluations/types'-import { getEvalBadgeProps, getEvalSummaries } from './EvalResultBadges'+import { getEvalBadgeProps, getEvalSummaries, scopeRunsToTarget } from './EvalResultBadges'import { isSentimentRun } from './EvaluationResultTag'function makeRun(overrides: Partial<EvaluationRun> = {}): EvaluationRun {@@ -58,6 +58,23 @@ describe('EvalResultBadges', () => {})})+ describe('scopeRunsToTarget', () => {+ const generationRun = makeRun({ id: 'gen-run', generation_id: 'gen-1' })+ const otherGenerationRun = makeRun({ id: 'other-gen-run', generation_id: 'gen-2' })+ // HogQL returns '' (not null) for a missing $ai_target_event_id on trace-target runs.+ const traceRunEmptyString = makeRun({ id: 'trace-run-empty', generation_id: '' })+ const traceRunNull = makeRun({ id: 'trace-run-null', generation_id: null })+ const allRuns = [generationRun, otherGenerationRun, traceRunEmptyString, traceRunNull]++ it('scoped to a generation, returns only that generation runs', () => {+ expect(scopeRunsToTarget(allRuns, 'gen-1').map((r) => r.id)).toEqual(['gen-run'])+ })++ it('without a generation, returns only trace-target runs (empty string and null)', () => {+ expect(scopeRunsToTarget(allRuns).map((r) => r.id)).toEqual(['trace-run-empty', 'trace-run-null'])+ })+ })+describe('getEvalBadgeProps', () => {it.each([['failed status', makeRun({ status: 'failed' }), { type: 'danger', label: 'Error' }],diff --git a/products/ai_observability/frontend/components/EvalResultBadges.tsx b/products/ai_observability/frontend/components/EvalResultBadges.tsxindex 8ecba12330e4..09d05b92f9e8 100644--- a/products/ai_observability/frontend/components/EvalResultBadges.tsx+++ b/products/ai_observability/frontend/components/EvalResultBadges.tsx@@ -38,6 +38,14 @@ export function getEvalBadgeProps(run: EvaluationRun): {return { type, icon, label }}+// Trace-target runs carry no $ai_target_event_id, which HogQL returns as '' (not null),+// so falsiness of generation_id is the discriminator between the two run kinds.+export function scopeRunsToTarget(runs: EvaluationRun[], generationEventId?: string): EvaluationRun[] {+ return generationEventId+ ? runs.filter((run) => run.generation_id === generationEventId)+ : runs.filter((run) => !run.generation_id)+}+function EvalTooltipContent({ latestRun, runCount }: EvalSummary): JSX.Element {return (<div className="max-w-80 space-y-1">@@ -51,9 +59,16 @@ function EvalTooltipContent({ latestRun, runCount }: EvalSummary): JSX.Element {)}-export function EvalResultBadges({ generationEventId }: { generationEventId: string }): JSX.Element | null {+export function EvalResultBadges({+ traceId,+ generationEventId,+}: {+ traceId: string+ /** When set, show runs targeting this generation; otherwise show trace-target runs. */+ generationEventId?: string+}): JSX.Element | null {const { generationEvaluationRuns, generationEvaluationRunsLoading } = useValues(- generationEvaluationRunsLogic({ lookupBy: 'generation', generationEventId })+ generationEvaluationRunsLogic({ traceId }))const traceLogic = useMountedLogic(aiObservabilityTraceLogic)const { setViewMode } = useActions(traceLogic)@@ -67,7 +82,9 @@ export function EvalResultBadges({ generationEventId }: { generationEventId: str)}- const summaries = getEvalSummaries(generationEvaluationRuns)+ // The trace-scoped fetch is capped at EVALUATION_SUMMARY_MAX_RUNS most-recent runs, so on a+ // trace with very heavy eval volume older runs for a given generation can fall out of view.+ const summaries = getEvalSummaries(scopeRunsToTarget(generationEvaluationRuns, generationEventId))if (summaries.length === 0) {return nulldiff --git a/products/ai_observability/frontend/components/EvalsTabContent.tsx b/products/ai_observability/frontend/components/EvalsTabContent.tsxindex be3758789996..62921cc45fac 100644--- a/products/ai_observability/frontend/components/EvalsTabContent.tsx+++ b/products/ai_observability/frontend/components/EvalsTabContent.tsx@@ -7,50 +7,45 @@ import { Link } from 'lib/lemon-ui/Link'import { useAttachedLogic } from 'lib/logic/scenes/useAttachedLogic'import { urls } from 'scenes/urls'+import { LLMTraceEvent } from '~/queries/schema/schema-general'+import { aiObservabilityTraceLogic } from '../aiObservabilityTraceLogic'import { llmEvaluationsLogic } from '../evaluations/llmEvaluationsLogic'import { generationEvaluationRunsLogic } from '../generationEvaluationRunsLogic'import { generationEvaluationRunsLogicType } from '../generationEvaluationRunsLogicType'import { llmEvaluationExecutionLogic } from '../llmEvaluationExecutionLogic'+import { formatLLMEventTitle } from '../utils'import { GenerationEvalRunsTable } from './GenerationEvalRunsTable'export function EvalsTabContent({- generationEventId,- timestamp,- event,+ traceId,+ generationEvent,distinctId,}: {- generationEventId: string- timestamp: string- event: string+ traceId: string+ generationEvent?: LLMTraceEventdistinctId?: string}): JSX.Element {- const generationRunsLogic = generationEvaluationRunsLogic({ lookupBy: 'generation', generationEventId })+ const runsLogic = generationEvaluationRunsLogic({ traceId })const traceLogic = useMountedLogic(aiObservabilityTraceLogic)- useAttachedLogic(generationRunsLogic, traceLogic)+ useAttachedLogic(runsLogic, traceLogic)return (<EvalsTabContentInner- generationEventId={generationEventId}- timestamp={timestamp}- event={event}+ generationEvent={generationEvent}distinctId={distinctId}- generationRunsLogic={generationRunsLogic}+ generationRunsLogic={runsLogic}/>)}function EvalsTabContentInner({- generationEventId,- timestamp,- event,+ generationEvent,distinctId,generationRunsLogic,}: {- generationEventId: string- timestamp: string- event: string+ generationEvent?: LLMTraceEventdistinctId?: stringgenerationRunsLogic: BuiltLogic<generationEvaluationRunsLogicType>}): JSX.Element {@@ -61,6 +56,9 @@ function EvalsTabContentInner({const { generationEvaluationRunsLoading, selectedEvaluationId } = useValues(generationRunsLogic)const availableEvaluations = evaluations?.filter((e) => !e.deleted) || []+ // Manual runs go through the generation workflow, so only generation-target evals+ // can be triggered from here, and only when there is a generation to point them at.+ const runnableEvaluations = generationEvent ? availableEvaluations.filter((e) => e.target === 'generation') : []const hasNoEvaluations = !evaluationsLoading && availableEvaluations.length === 0return (@@ -70,19 +68,24 @@ function EvalsTabContentInner({Click Refresh to see new results.</LemonBanner><div className="flex justify-between items-center mb-4">- <div className="flex gap-2">+ <div className="flex items-center gap-2">{hasNoEvaluations ? (<Link to={urls.aiObservabilityEvaluations()}><LemonButton type="primary" icon={<IconPlus />} size="small">Create your first evaluation</LemonButton></Link>- ) : (+ ) : generationEvent && !evaluationsLoading && runnableEvaluations.length === 0 ? (+ <span className="text-muted text-sm">+ No generation-target evaluations to run manually. Trace-target evaluations run+ automatically.+ </span>+ ) : generationEvent ? (<><LemonSelectvalue={selectedEvaluationId}onChange={setSelectedEvaluationId}- options={availableEvaluations.map((evaluation) => ({+ options={runnableEvaluations.map((evaluation) => ({value: evaluation.id,label: evaluation.name,}))}@@ -98,9 +101,9 @@ function EvalsTabContentInner({if (selectedEvaluationId) {runEvaluation(selectedEvaluationId,- generationEventId,- timestamp,- event,+ generationEvent.id,+ generationEvent.createdAt,+ generationEvent.event,distinctId)}@@ -111,8 +114,11 @@ function EvalsTabContentInner({>Run Evaluation</LemonButton>+ <span className="text-muted text-sm">+ Runs on generation {formatLLMEventTitle(generationEvent)}+ </span></>- )}+ ) : null}</div><LemonButtontype="secondary"diff --git a/products/ai_observability/frontend/components/EvaluationRunTargetCell.tsx b/products/ai_observability/frontend/components/EvaluationRunTargetCell.tsxnew file mode 100644index 000000000000..8f644feddc21--- /dev/null+++ b/products/ai_observability/frontend/components/EvaluationRunTargetCell.tsx@@ -0,0 +1,37 @@+import { useValues } from 'kea'+import { combineUrl, router } from 'kea-router'++import { Link } from '@posthog/lemon-ui'++import { urls } from 'scenes/urls'++import { EvaluationRun } from '../evaluations/types'+import { sanitizeTraceUrlSearchParams } from '../utils'++// Generation-target runs link to the specific event in the trace; trace-target+// runs (no generation id) link to the whole trace. Both land on the Evaluations tab.+export function EvaluationRunTargetCell({ run }: { run: EvaluationRun }): JSX.Element {+ const { searchParams } = useValues(router)++ if (!run.trace_id) {+ return <span className="font-mono text-sm text-muted">—</span>+ }++ // No timestamp param on purpose: the run's timestamp can be long after the trace+ // (debounce window, manual re-runs), and without one the trace query scans from the+ // beginning, which always finds the trace.+ const to = combineUrl(urls.aiObservabilityTrace(run.trace_id), {+ ...sanitizeTraceUrlSearchParams(searchParams, { removeSearch: true }),+ tab: 'evals',+ ...(run.generation_id ? { event: run.generation_id } : {}),+ }).url+ const label = run.generation_id ? `${run.generation_id.slice(0, 12)}...` : `trace ${run.trace_id.slice(0, 12)}...`++ return (+ <div className="font-mono text-sm">+ <Link to={to} className="text-primary">+ {label}+ </Link>+ </div>+ )+}diff --git a/products/ai_observability/frontend/components/GenerationEvalRunsTable.tsx b/products/ai_observability/frontend/components/GenerationEvalRunsTable.tsxindex d796d1e2565d..0b724f42bb76 100644--- a/products/ai_observability/frontend/components/GenerationEvalRunsTable.tsx+++ b/products/ai_observability/frontend/components/GenerationEvalRunsTable.tsx@@ -9,6 +9,7 @@ import { urls } from 'scenes/urls'import { EvaluationRun } from '../evaluations/types'import { generationEvaluationRunsLogicType } from '../generationEvaluationRunsLogicType'import { EvaluationResultTag, getEvaluationResultSortValue } from './EvaluationResultTag'+import { EvaluationRunTargetCell } from './EvaluationRunTargetCell'export function GenerationEvalRunsTable({generationRunsLogic,@@ -33,6 +34,11 @@ export function GenerationEvalRunsTable({</Link>),},+ {+ title: 'Target',+ key: 'target',+ render: (_, run) => <EvaluationRunTargetCell run={run} />,+ },{title: 'Result',key: 'result',@@ -68,7 +74,7 @@ export function GenerationEvalRunsTable({<div className="text-center py-8"><div className="text-muted mb-2">No evaluations run yet</div><div className="text-sm text-muted">- Click "Run Evaluation" above to run an evaluation on this generation.+ Evaluation runs for this trace and its generations will appear here.</div></div>}diff --git a/products/ai_observability/frontend/evaluations/components/EvaluationRunsTable.tsx b/products/ai_observability/frontend/evaluations/components/EvaluationRunsTable.tsxindex f3dabdcd36ce..18dca4f4acc6 100644--- a/products/ai_observability/frontend/evaluations/components/EvaluationRunsTable.tsx+++ b/products/ai_observability/frontend/evaluations/components/EvaluationRunsTable.tsx@@ -1,15 +1,13 @@import { useActions, useValues } from 'kea'-import { combineUrl, router } from 'kea-router'import { IconRefresh } from '@posthog/icons'-import { LemonButton, LemonSegmentedButton, LemonTable, LemonTag, Link, Tooltip } from '@posthog/lemon-ui'+import { LemonButton, LemonSegmentedButton, LemonTable, LemonTag, Tooltip } from '@posthog/lemon-ui'import { TZLabel } from 'lib/components/TZLabel'import { LemonTableColumns } from 'lib/lemon-ui/LemonTable'-import { urls } from 'scenes/urls'import { EvaluationResultTag, getEvaluationResultSortValue } from '../../components/EvaluationResultTag'-import { sanitizeTraceUrlSearchParams } from '../../utils'+import { EvaluationRunTargetCell } from '../../components/EvaluationRunTargetCell'import { evaluationSupportsReports } from '../evaluationCapabilities'import { llmEvaluationLogic } from '../llmEvaluationLogic'import { EvaluationRun, SentimentEvaluationRunsFilter } from '../types'@@ -41,8 +39,6 @@ function SentimentEvaluationRunsFilters(): JSX.Element {export function EvaluationRunsTable(): JSX.Element {const { filteredEvaluationRuns, evaluation, evaluationRunsLoading, runsLookup } = useValues(llmEvaluationLogic)- const { searchParams } = useValues(router)- const traceSearchParams = sanitizeTraceUrlSearchParams(searchParams, { removeSearch: true })const { refreshEvaluationRuns } = useActions(llmEvaluationLogic)const showSummary = evaluationSupportsReports(evaluation)const showSentimentFilters = evaluation?.evaluation_type === 'sentiment'@@ -57,40 +53,7 @@ export function EvaluationRunsTable(): JSX.Element {{title: 'Target',key: 'target',- render: (_, run) => {- // Generation-target runs link to the specific event in the trace; trace-target- // runs (no generation id) link to the whole trace.- if (run.generation_id) {- return (- <div className="font-mono text-sm">- <Link- to={- combineUrl(urls.aiObservabilityTrace(run.trace_id), {- ...traceSearchParams,- event: run.generation_id,- }).url- }- className="text-primary"- >- {run.generation_id.slice(0, 12)}...- </Link>- </div>- )- }- if (run.trace_id) {- return (- <div className="font-mono text-sm">- <Link- to={combineUrl(urls.aiObservabilityTrace(run.trace_id), traceSearchParams).url}- className="text-primary"- >- trace {run.trace_id.slice(0, 12)}...- </Link>- </div>- )- }- return <span className="font-mono text-sm text-muted">—</span>- },+ render: (_, run) => <EvaluationRunTargetCell run={run} />,},{title: 'Result',diff --git a/products/ai_observability/frontend/generationEvaluationRunsLogic.ts b/products/ai_observability/frontend/generationEvaluationRunsLogic.tsindex 877f15e026e2..055634dce781 100644--- a/products/ai_observability/frontend/generationEvaluationRunsLogic.ts+++ b/products/ai_observability/frontend/generationEvaluationRunsLogic.ts@@ -5,24 +5,14 @@ import { EvaluationRun } from './evaluations/types'import type { generationEvaluationRunsLogicType } from './generationEvaluationRunsLogicType'import { queryEvaluationRuns } from './utils'-export interface GenerationEvaluationRunsByGenerationProps {- lookupBy: 'generation'- generationEventId: string-}--export interface GenerationEvaluationRunsByTraceProps {- lookupBy: 'trace'+export interface GenerationEvaluationRunsLogicProps {traceId: string}-export type GenerationEvaluationRunsLogicProps =- | GenerationEvaluationRunsByGenerationProps- | GenerationEvaluationRunsByTraceProps-export const generationEvaluationRunsLogic = kea<generationEvaluationRunsLogicType>([path(['products', 'ai_observability', 'frontend', 'generationEvaluationRunsLogic']),props({} as GenerationEvaluationRunsLogicProps),- key((props) => (props.lookupBy === 'trace' ? `trace-${props.traceId}` : props.generationEventId)),+ key((props) => `trace-${props.traceId}`),actions({refreshGenerationEvaluationRuns: true,@@ -36,9 +26,7 @@ export const generationEvaluationRunsLogic = kea<generationEvaluationRunsLogicTyloadGenerationEvaluationRuns: async () => {try {return await queryEvaluationRuns({- ...(props.lookupBy === 'trace'- ? { traceId: props.traceId }- : { generationEventId: props.generationEventId }),+ traceId: props.traceId,forceRefresh: values.isForceRefresh,})} catch (error) {diff --git a/products/ai_observability/frontend/utils.ts b/products/ai_observability/frontend/utils.tsindex 86589633cbe5..9e220deed384 100644--- a/products/ai_observability/frontend/utils.ts+++ b/products/ai_observability/frontend/utils.ts@@ -1157,19 +1157,18 @@ export function mapEvaluationRunRow(row: RawEvaluationRunRow): EvaluationRun {export async function queryEvaluationRuns(params: {evaluationId?: string- generationEventId?: stringtraceId?: stringforceRefresh?: boolean}): Promise<EvaluationRun[]> {- const { evaluationId, generationEventId, traceId, forceRefresh } = params+ const { evaluationId, traceId, forceRefresh } = params- const propertyValue = evaluationId || generationEventId || traceId+ const propertyValue = evaluationId || traceIdif (!propertyValue) {- throw new Error('Either evaluationId, generationEventId, or traceId must be provided')+ throw new Error('Either evaluationId or traceId must be provided')}- const propertyName = evaluationId ? '$ai_evaluation_id' : generationEventId ? '$ai_target_event_id' : '$ai_trace_id'+ const propertyName = evaluationId ? '$ai_evaluation_id' : '$ai_trace_id'const query = hogql`SELECT