Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(score-analytics): generate binLabels from statistics when heatmap empty (#10288)

Michael Fröhlich605a3de766f9
diff --git a/web/src/features/scores/components/score-analytics/components/cards/DistributionNumericCard.tsx b/web/src/features/scores/components/score-analytics/components/cards/DistributionNumericCard.tsx
index d1a0387425..7b22d751a6 100644
--- a/web/src/features/scores/components/score-analytics/components/cards/DistributionNumericCard.tsx
+++ b/web/src/features/scores/components/score-analytics/components/cards/DistributionNumericCard.tsx
@@ -33,8 +33,34 @@ export function DistributionNumericCard() {
const [activeTab, setActiveTab] = useState<DistributionTab>("all");
+ // Select appropriate bin labels based on active tab
+ // Backend uses different binning strategies per tab, so we need different labels
+ const selectedBinLabels = useMemo(() => {
+ if (!data?.distribution) return undefined;
+
+ const { distribution, metadata } = data;
+ const { mode } = metadata;
+
+ // Single score mode: use individual1 labels
+ if (mode === "single") {
+ return distribution.binLabelsIndividual1;
+ }
+
+ // Two score mode: select labels based on active tab
+ switch (activeTab) {
+ case "score1":
+ return distribution.binLabelsIndividual1; // Individual binning for score1
+ case "score2":
+ return distribution.binLabelsIndividual2; // Individual binning for score2
+ case "all":
+ case "matched":
+ return distribution.binLabelsGlobal; // Global binning for comparison
+ default:
+ return distribution.binLabelsGlobal;
+ }
+ }, [data, activeTab]);
+
// Select distribution data based on active tab
- // Note: binLabels are already computed in the Provider and stored in distribution.binLabels
const { distribution1Data, distribution2Data, description } = useMemo(() => {
if (!data) {
return {
@@ -64,14 +90,28 @@ export function DistributionNumericCard() {
// Two score mode - handle tabs
switch (activeTab) {
case "score1":
+ // Use individual distribution if available and non-empty, fallback to global distribution
+ // This handles the case where backend might return empty array for individual distributions
+ // when there are no matching pairs between scores
+ const score1Data =
+ distribution.score1Individual &&
+ distribution.score1Individual.length > 0
+ ? distribution.score1Individual
+ : distribution.score1;
return {
- distribution1Data: distribution.score1Individual,
+ distribution1Data: score1Data,
distribution2Data: undefined,
description: `${score1.name} - ${statistics.score1.total.toLocaleString()} observations`,
};
case "score2":
+ // Use individual distribution if available and non-empty, fallback to global distribution
+ const score2Data =
+ distribution.score2Individual &&
+ distribution.score2Individual.length > 0
+ ? distribution.score2Individual
+ : distribution.score2;
return {
- distribution1Data: distribution.score2Individual,
+ distribution1Data: score2Data,
distribution2Data: undefined,
description: `${score2?.name ?? "Score 2"} - ${statistics.score2?.total.toLocaleString()} observations`,
};
@@ -136,11 +176,11 @@ export function DistributionNumericCard() {
);
}
- const { distribution, metadata } = data;
+ const { metadata } = data;
const { mode } = metadata;
const { score1, score2 } = params;
- const hasData = distribution1Data.length > 0;
+ const hasData = (distribution1Data?.length ?? 0) > 0;
const showTabs = mode === "two";
// Helper function to truncate tab labels with max character limit
@@ -210,15 +250,15 @@ export function DistributionNumericCard() {
</div>
</CardHeader>
<CardContent className="flex h-[340px] flex-col pl-0">
- {hasData && distribution.binLabels ? (
+ {hasData && selectedBinLabels ? (
<ScoreDistributionNumericChart
- distribution1={distribution1Data}
+ distribution1={distribution1Data ?? []}
distribution2={
activeTab === "score1" || activeTab === "score2"
? undefined
: (distribution2Data ?? undefined)
}
- binLabels={distribution.binLabels}
+ binLabels={selectedBinLabels}
score1Name={
activeTab === "score2" && score2
? score2.name
diff --git a/web/src/features/scores/components/score-analytics/hooks/useScoreAnalyticsQuery.ts b/web/src/features/scores/components/score-analytics/hooks/useScoreAnalyticsQuery.ts
index a71060f05f..e632c42e32 100644
--- a/web/src/features/scores/components/score-analytics/hooks/useScoreAnalyticsQuery.ts
+++ b/web/src/features/scores/components/score-analytics/hooks/useScoreAnalyticsQuery.ts
@@ -83,7 +83,13 @@ export interface Distribution {
score1: Array<{ binIndex: number; count: number }>;
score2: Array<{ binIndex: number; count: number }> | null;
categories?: string[]; // For categorical/boolean
- binLabels?: string[]; // For numeric
+ binLabels?: string[]; // For numeric (deprecated - use specific labels below)
+
+ // Tab-specific bin labels (for numeric scores)
+ // Backend uses different binning strategies per tab, so we need different labels
+ binLabelsIndividual1?: string[]; // For score1 tab (using min1/max1 bounds)
+ binLabelsIndividual2?: string[]; // For score2 tab (using min2/max2 bounds)
+ binLabelsGlobal?: string[]; // For all/matched tabs (using global_min/global_max bounds)
// Tab-specific distributions (for card components to select from)
score1Individual: Array<{ binIndex: number; count: number }>;
@@ -268,14 +274,84 @@ export function useScoreAnalyticsQuery(
// ========================================================================
// 3. Generate bin labels (numeric only)
// ========================================================================
- const binLabels =
- isNumeric && apiData.heatmap[0]
- ? generateBinLabels({
- min: apiData.heatmap[0].min1,
- max: apiData.heatmap[0].max1,
+ // For numeric scores, we need min/max bounds to generate bin labels.
+ // Backend uses THREE different binning strategies:
+ // - Individual binning for score1 (using min1/max1)
+ // - Individual binning for score2 (using min2/max2)
+ // - Global binning for all/matched (using global_min/global_max)
+ // We must generate THREE separate sets of labels to match!
+ let binLabelsIndividual1: string[] | undefined = undefined;
+ let binLabelsIndividual2: string[] | undefined = undefined;
+ let binLabelsGlobal: string[] | undefined = undefined;
+
+ if (isNumeric) {
+ // Try to get bounds from heatmap (preferred, as it contains pre-calculated bounds)
+ if (apiData.heatmap[0]) {
+ // Individual labels for score1 (for score1 tab)
+ binLabelsIndividual1 = generateBinLabels({
+ min: apiData.heatmap[0].min1,
+ max: apiData.heatmap[0].max1,
+ nBins,
+ });
+
+ // Individual labels for score2 (for score2 tab)
+ binLabelsIndividual2 = generateBinLabels({
+ min: apiData.heatmap[0].min2,
+ max: apiData.heatmap[0].max2,
+ nBins,
+ });
+
+ // Global labels (for all/matched tabs)
+ binLabelsGlobal = generateBinLabels({
+ min: apiData.heatmap[0].globalMin,
+ max: apiData.heatmap[0].globalMax,
+ nBins,
+ });
+ }
+ // Fallback: Calculate bounds from statistics when heatmap is empty
+ // This handles the case when matchedCount = 0 (no paired observations)
+ else if (
+ apiData.statistics &&
+ apiData.statistics.mean1 !== null &&
+ apiData.statistics.std1 !== null
+ ) {
+ // For score1 individual
+ const mean1 = apiData.statistics.mean1;
+ const std1 = apiData.statistics.std1;
+ binLabelsIndividual1 = generateBinLabels({
+ min: mean1 - 3 * std1,
+ max: mean1 + 3 * std1,
+ nBins,
+ });
+
+ // For score2 individual (if available)
+ if (
+ apiData.statistics.mean2 !== null &&
+ apiData.statistics.std2 !== null
+ ) {
+ const mean2 = apiData.statistics.mean2;
+ const std2 = apiData.statistics.std2;
+ binLabelsIndividual2 = generateBinLabels({
+ min: mean2 - 3 * std2,
+ max: mean2 + 3 * std2,
+ nBins,
+ });
+
+ // Global bounds: union of both ranges
+ const globalMin = Math.min(mean1 - 3 * std1, mean2 - 3 * std2);
+ const globalMax = Math.max(mean1 + 3 * std1, mean2 + 3 * std2);
+ binLabelsGlobal = generateBinLabels({
+ min: globalMin,
+ max: globalMax,
nBins,
- })
- : undefined;
+ });
+ } else {
+ // Single score mode: all labels are the same
+ binLabelsIndividual2 = binLabelsIndividual1;
+ binLabelsGlobal = binLabelsIndividual1;
+ }
+ }
+ }
// ========================================================================
// 4. Transform heatmap data
@@ -485,7 +561,10 @@ export function useScoreAnalyticsQuery(
score1: distribution1,
score2: mode === "two" ? distribution2 : null,
categories,
- binLabels,
+ binLabels: binLabelsGlobal, // For backward compatibility, default to global
+ binLabelsIndividual1,
+ binLabelsIndividual2,
+ binLabelsGlobal,
score1Individual: distribution1Individual,
score2Individual: distribution2Individual,
score1Matched: distribution1Matched,
diff --git a/web/src/server/api/routers/scores.ts b/web/src/server/api/routers/scores.ts
index 27ad9ea151..b8f26082ce 100644
--- a/web/src/server/api/routers/scores.ts
+++ b/web/src/server/api/routers/scores.ts
@@ -1386,17 +1386,22 @@ export const scoresRouter = createTRPCRouter({
),
-- CTE 8: Statistics
+ -- IMPORTANT: Calculate mean/std from individual filtered scores, NOT matched_scores
+ -- This ensures statistics are available even when matchedCount = 0
+ -- Comparison metrics (mae/rmse/correlations) still use matched_scores since they require pairs
stats AS (
SELECT
- count() as matched_count,
+ (SELECT count() FROM matched_scores) as matched_count,
${
isNumeric
- ? `avg(value1) as mean1,
- avg(value2) as mean2,
- stddevPop(value1) as std1,
- stddevPop(value2) as std2,
- avg(abs(value1 - value2)) as mae,
- sqrt(avg(pow(value1 - value2, 2))) as rmse,
+ ? `-- Individual score statistics from filtered tables (not matched pairs)
+ (SELECT avg(value) FROM score1_filtered) as mean1,
+ (SELECT avg(value) FROM score2_filtered) as mean2,
+ (SELECT stddevPop(value) FROM score1_filtered) as std1,
+ (SELECT stddevPop(value) FROM score2_filtered) as std2,
+ -- Comparison metrics require matched pairs
+ (SELECT avg(abs(value1 - value2)) FROM matched_scores) as mae,
+ (SELECT sqrt(avg(pow(value1 - value2, 2))) FROM matched_scores) as rmse,
-- Conditional correlation: only execute subquery if safe
-- Uses short-circuit evaluation to prevent errors with insufficient data
${
@@ -1427,7 +1432,6 @@ export const scoresRouter = createTRPCRouter({
NULL as mae,
NULL as rmse`
}
- FROM matched_scores
),
${timeseriesCTE},