Skip to content

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

feat: include all values from scoreConfig in filterOptions (#7524)

Steffen Schmitz5cf2411e74f5
diff --git a/packages/shared/src/server/repositories/scores.ts b/packages/shared/src/server/repositories/scores.ts
index 0c675c7467..c21c335ef3 100644
--- a/packages/shared/src/server/repositories/scores.ts
+++ b/packages/shared/src/server/repositories/scores.ts
@@ -31,6 +31,7 @@ import { _handleGetScoreById, _handleGetScoresByIds } from "./scores-utils";
import { parseMetadataCHRecordToDomain } from "../utils/metadata_conversion";
import { ClickHouseClientConfigOptions } from "@clickhouse/client";
import { recordDistribution } from "../instrumentation";
+import { prisma } from "../../db";
export const searchExistingAnnotationScore = async (
projectId: string,
@@ -622,7 +623,7 @@ export const getCategoricalScoresGroupedByName = async (
: undefined;
const query = `
- SELECT
+ SELECT
name AS label,
groupArray(DISTINCT string_value) AS values
FROM scores s
@@ -651,7 +652,58 @@ export const getCategoricalScoresGroupedByName = async (
},
});
- return rows;
+ // Get score names from ClickHouse results to query score configs
+ const scoreNames = rows.map((row) => row.label);
+
+ // Query score_configs table for categorical configurations
+ const scoreConfigs =
+ scoreNames.length > 0
+ ? await prisma.scoreConfig.findMany({
+ where: {
+ projectId: projectId,
+ name: {
+ in: scoreNames,
+ },
+ dataType: "CATEGORICAL",
+ isArchived: false,
+ },
+ select: {
+ name: true,
+ categories: true,
+ },
+ })
+ : [];
+
+ // Create a map of score configs for easy lookup
+ const configMap = new Map(
+ scoreConfigs.map((config) => [config.name, config.categories]),
+ );
+
+ // Enhance the results with all possible category values from score configs
+ return rows.map((row) => {
+ const configCategories = configMap.get(row.label);
+
+ if (configCategories && Array.isArray(configCategories)) {
+ // Extract all possible category labels from the score config
+ const allPossibleValues = (
+ configCategories as Array<{ label: string; value: number }>
+ ).map((category) => category.label);
+
+ // Merge actual values from ClickHouse with all possible values from config
+ // Use Set to ensure uniqueness
+ const mergedValues = Array.from(
+ new Set([...row.values, ...allPossibleValues]),
+ );
+
+ return {
+ ...row,
+ values: mergedValues,
+ };
+ }
+
+ // If no config found, return original values
+ return row;
+ });
};
export const getScoresUiCount = async (props: {
diff --git a/web/src/__tests__/async/traces-trpc.servertest.ts b/web/src/__tests__/async/traces-trpc.servertest.ts
index 03970a44a0..ebd9d2f36f 100644
--- a/web/src/__tests__/async/traces-trpc.servertest.ts
+++ b/web/src/__tests__/async/traces-trpc.servertest.ts
@@ -6,7 +6,12 @@ import { pruneDatabase } from "@/src/__tests__/test-utils";
import { prisma } from "@langfuse/shared/src/db";
import { appRouter } from "@/src/server/api/root";
import { createInnerTRPCContext } from "@/src/server/api/trpc";
-import { createTrace, createTracesCh } from "@langfuse/shared/src/server";
+import {
+ createTrace,
+ createTracesCh,
+ createTraceScore,
+ createScoresCh,
+} from "@langfuse/shared/src/server";
import { randomUUID } from "crypto";
describe("traces trpc", () => {
@@ -137,4 +142,56 @@ describe("traces trpc", () => {
expect(traceRes?.timestamp).toEqual(new Date(trace.timestamp));
});
});
+
+ describe("traces.filterOptions", () => {
+ it("should include all possible categorical score values from score configs", async () => {
+ // Create a trace
+ const trace = createTrace({
+ project_id: projectId,
+ });
+ await createTracesCh([trace]);
+
+ // Create a categorical score config with multiple possible values
+ const scoreConfig = await prisma.scoreConfig.create({
+ data: {
+ projectId: projectId,
+ name: "sentiment",
+ dataType: "CATEGORICAL",
+ categories: [
+ { label: "positive", value: 1 },
+ { label: "neutral", value: 0 },
+ { label: "negative", value: -1 },
+ ],
+ },
+ });
+
+ // Create only one actual score (subset of possible values)
+ const score = createTraceScore({
+ project_id: projectId,
+ trace_id: trace.id,
+ name: "sentiment",
+ string_value: "custom",
+ data_type: "CATEGORICAL",
+ config_id: scoreConfig.id,
+ });
+ await createScoresCh([score]);
+
+ // Get filter options
+ const filterOptions = await caller.traces.filterOptions({
+ projectId,
+ });
+
+ // Find the sentiment score in categorical scores
+ const sentimentScore = filterOptions.score_categories.find(
+ (score) => score.label === "sentiment",
+ );
+
+ expect(sentimentScore).toBeDefined();
+ expect(sentimentScore?.values).toEqual(
+ expect.arrayContaining(["custom", "positive", "neutral", "negative"]),
+ );
+ // Should include all possible values from config, not just the actual score value
+ expect(sentimentScore?.values).toHaveLength(4);
+ });
+ });
});