Skip to content

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

feat(evals): add filter conditions for trace-level (#19400)

marliessophie7a15e696886d
diff --git a/packages/shared/src/features/evals/experimentFilters.ts b/packages/shared/src/features/evals/experimentFilters.ts
new file mode 100644
index 0000000000..cbb0392a3a
--- /dev/null
+++ b/packages/shared/src/features/evals/experimentFilters.ts
@@ -0,0 +1,29 @@
+import type {
+ ColumnDefinition,
+ SingleValueOption,
+} from "../../tableDefinitions";
+import { formatColumnOptions } from "../../tableDefinitions/typeHelpers";
+import {
+ evalFilterFields,
+ type ObservationEvalColumnDef,
+} from "./filterFields";
+
+export const experimentEvalFilterColumns: ObservationEvalColumnDef[] = [
+ evalFilterFields.experimentDatasetId,
+];
+
+export type ExperimentEvalOptions = {
+ experimentDatasetId?: Array<SingleValueOption>;
+};
+
+export function experimentEvalFilterColsWithOptions(
+ options?: ExperimentEvalOptions,
+ cols: ColumnDefinition[] = experimentEvalFilterColumns,
+): ColumnDefinition[] {
+ return cols.map((col) => {
+ if (col.id === "experimentDatasetId") {
+ return formatColumnOptions(col, options?.experimentDatasetId ?? []);
+ }
+ return col;
+ });
+}
diff --git a/packages/shared/src/features/evals/experimentRuleNormalization.ts b/packages/shared/src/features/evals/experimentRuleNormalization.ts
index 3ce0760271..2358fcfc0b 100644
--- a/packages/shared/src/features/evals/experimentRuleNormalization.ts
+++ b/packages/shared/src/features/evals/experimentRuleNormalization.ts
@@ -1,5 +1,5 @@
import type { FilterState } from "../../types";
-import { eventsEvalFilterColumns } from "./observationForEval";
+import { eventsEvalFilterColumns } from "./spanFilters";
import {
EvalTargetObject,
type EvalTargetObject as EvalTargetObjectType,
diff --git a/packages/shared/src/features/evals/filterFields.ts b/packages/shared/src/features/evals/filterFields.ts
new file mode 100644
index 0000000000..b689b2cf8f
--- /dev/null
+++ b/packages/shared/src/features/evals/filterFields.ts
@@ -0,0 +1,250 @@
+import { ObservationLevel, ObservationType } from "../../domain";
+import { isRootObservation } from "../../eventsTable";
+import type { ColumnDefinition } from "../../tableDefinitions";
+import type { FilterCondition } from "../../types";
+import type { ObservationForEval } from "./observationForEval";
+
+export type EvalFilterDefinition = {
+ columns: ColumnDefinition[];
+ /** When supplied, omitted filter types and operators are unsupported. */
+ operators?: Partial<
+ Record<FilterCondition["type"], readonly FilterCondition["operator"][]>
+ >;
+};
+
+export type ObservationEvalFilterColumnInternal =
+ /** Column identifier (must match an ObservationForEval field name) */
+ keyof Pick<
+ ObservationForEval,
+ | "type"
+ | "name"
+ | "environment"
+ | "level"
+ | "status_message"
+ | "version"
+ | "release"
+ | "trace_name"
+ | "user_id"
+ | "session_id"
+ | "provided_model_name"
+ | "prompt_name"
+ | "prompt_version"
+ | "tags"
+ | "experiment_id"
+ | "experiment_name"
+ | "experiment_dataset_id"
+ | "experiment_item_root_span_id"
+ | "metadata"
+ | "parent_span_id"
+ | "is_app_root"
+ | "tool_call_names"
+ | "tool_call_count"
+ >;
+
+export type ObservationEvalColumnDef = ColumnDefinition & {
+ internal: ObservationEvalFilterColumnInternal;
+};
+
+export const evalFilterFields = {
+ type: {
+ name: "Type",
+ id: "type",
+ type: "stringOptions",
+ internal: "type",
+ options: Object.values(ObservationType).map((key) => ({ value: key })),
+ },
+ name: {
+ name: "Name",
+ id: "name",
+ type: "stringOptions",
+ internal: "name",
+ options: [], // to be filled at runtime
+ },
+ environment: {
+ name: "Environment",
+ id: "environment",
+ type: "stringOptions",
+ internal: "environment",
+ options: [], // to be filled at runtime
+ },
+ level: {
+ name: "Status",
+ id: "level",
+ type: "stringOptions",
+ internal: "level",
+ options: Object.values(ObservationLevel).map((key) => ({ value: key })),
+ aliases: ["Level"],
+ },
+ version: {
+ name: "Version",
+ id: "version",
+ type: "string",
+ internal: "version",
+ nullable: true,
+ },
+ release: {
+ name: "Release",
+ id: "release",
+ type: "string",
+ internal: "release",
+ nullable: true,
+ },
+ statusMessage: {
+ name: "Status Message",
+ id: "statusMessage",
+ type: "string",
+ internal: "status_message",
+ nullable: true,
+ },
+ providedModelName: {
+ name: "Provided Model Name",
+ id: "providedModelName",
+ type: "string",
+ internal: "provided_model_name",
+ nullable: true,
+ },
+ promptName: {
+ name: "Prompt Name",
+ id: "promptName",
+ type: "string",
+ internal: "prompt_name",
+ nullable: true,
+ },
+ promptVersion: {
+ name: "Prompt Version",
+ id: "promptVersion",
+ type: "number",
+ internal: "prompt_version",
+ nullable: true,
+ },
+ traceName: {
+ name: "Trace Name",
+ id: "traceName",
+ type: "stringOptions",
+ internal: "trace_name",
+ options: [], // to be filled at runtime
+ nullable: true,
+ },
+ userId: {
+ name: "User ID",
+ id: "userId",
+ type: "string",
+ internal: "user_id",
+ nullable: true,
+ },
+ sessionId: {
+ name: "Session ID",
+ id: "sessionId",
+ type: "string",
+ internal: "session_id",
+ nullable: true,
+ },
+ tags: {
+ name: "Tags",
+ id: "tags",
+ type: "arrayOptions",
+ internal: "tags",
+ options: [], // to be filled at runtime
+ },
+ metadata: {
+ name: "Metadata",
+ id: "metadata",
+ type: "stringObject",
+ internal: "metadata",
+ },
+ isRootObservation: {
+ name: "Is Root Observation",
+ id: "isRootObservation",
+ type: "boolean",
+ internal: "is_app_root",
+ },
+ parentObservationId: {
+ name: "Parent Observation",
+ id: "parentObservationId",
+ type: "null",
+ internal: "parent_span_id",
+ nullable: true,
+ },
+ experimentId: {
+ name: "Experiment ID",
+ id: "experimentId",
+ type: "stringOptions",
+ internal: "experiment_id",
+ options: [], // to be filled at runtime
+ nullable: true,
+ },
+ experimentName: {
+ name: "Experiment Name",
+ id: "experimentName",
+ type: "string",
+ internal: "experiment_name",
+ nullable: true,
+ },
+ isExperimentItemRootSpan: {
+ name: "Is Experiment Item Root Span",
+ id: "isExperimentItemRootSpan",
+ type: "boolean",
+ internal: "experiment_item_root_span_id",
+ },
+ calledToolNames: {
+ name: "Called Tool Names",
+ id: "calledToolNames",
+ type: "arrayOptions",
+ internal: "tool_call_names",
+ options: [], // to be filled at runtime
+ },
+ toolCalls: {
+ name: "Tool Call Count",
+ id: "toolCalls",
+ type: "number",
+ internal: "tool_call_count",
+ step: 1,
+ min: 0,
+ },
+ experimentDatasetId: {
+ name: "Dataset",
+ id: "experimentDatasetId",
+ type: "stringOptions",
+ internal: "experiment_dataset_id",
+ options: [], // to be filled at runtime
+ },
+} satisfies Record<string, ObservationEvalColumnDef>;
+
+const evalFilterFieldsById = new Map<string, ObservationEvalColumnDef>(
+ Object.values(evalFilterFields).map((column) => [column.id, column]),
+);
+
+/**
+ * Field mapper for observation eval filters.
+ * Maps camelCase filter column IDs to snake_case observation fields.
+ * Uses the shared field definitions independently of each target's filter list.
+ *
+ * @param observation - The observation data object
+ * @param column - The camelCase column ID from filter definitions
+ * @returns The value from the observation object
+ */
+export function mapEventEvalFilterColumnIdToField(
+ observation: ObservationForEval,
+ column: string,
+) {
+ const columnMapping = evalFilterFieldsById.get(column);
+ if (!columnMapping) {
+ return undefined;
+ }
+
+ if (columnMapping.id === "isRootObservation") {
+ return isRootObservation({
+ parentObservationId: observation.parent_span_id,
+ isAppRoot: observation.is_app_root,
+ });
+ }
+
+ if (columnMapping.id === "isExperimentItemRootSpan") {
+ return (
+ observation.experiment_item_root_span_id !== null &&
+ observation.experiment_item_root_span_id === observation.span_id
+ );
+ }
+
+ return observation[columnMapping.internal];
+}
diff --git a/packages/shared/src/features/evals/observationForEval.ts b/packages/shared/src/features/evals/observationForEval.ts
index 54b2fcd7cd..f8f8625f34 100644
--- a/packages/shared/src/features/evals/observationForEval.ts
+++ b/packages/shared/src/features/evals/observationForEval.ts
@@ -1,13 +1,9 @@
import { z } from "zod";
import { DEFAULT_TRACE_ENVIRONMENT } from "../../server/ingestion/types";
import { type EventRecordBaseType } from "../../server/repositories/definitions";
-import { ObservationLevel, ObservationType } from "../../domain";
+import { ObservationLevel } from "../../domain";
import { metadataArraysToRecord } from "../../server/utils/metadata_conversion";
-import { SingleValueOption } from "../../tableDefinitions";
-import { ColumnDefinition } from "../../tableDefinitions";
-import { formatColumnOptions } from "../../tableDefinitions/typeHelpers";
import { parseJsonIfString } from "../../utils/json";
-import { isRootObservation } from "../../eventsTable";
const flexibleUsageCostSchema = z.record(
z.string(),
@@ -182,35 +178,6 @@ export function convertEventRecordToObservationForEval(
});
}
-export type ObservationEvalFilterColumnInternal =
- /** Column identifier (must match an ObservationForEval field name) */
- keyof Pick<
- ObservationForEval,
- | "type"
- | "name"
- | "environment"
- | "level"
- | "status_message"
- | "version"
- | "release"
- | "trace_name"
- | "user_id"
- | "session_id"
- | "provided_model_name"
- | "prompt_name"
- | "prompt_version"
- | "tags"
- | "experiment_id"
- | "experiment_name"
- | "experiment_dataset_id"
- | "experiment_item_root_span_id"
- | "metadata"
- | "parent_span_id"
- | "is_app_root"
- | "tool_call_names"
- | "tool_call_count"
- >;
-
export type ObservationEvalMappingColumnInternal = keyof Pick<
ObservationForEval,
| "input"
@@ -322,269 +289,18 @@ export const observationEvalVariableColumns: ObservationEvalVariableColumn[] = [
...experimentTargetEvalVariableColumns,
];
-type ObservationEvalColumnDef = ColumnDefinition & {
- internal: ObservationEvalFilterColumnInternal;
-};
-
-/**
- * Columns available for filtering in observation-based evals.
- *
- * These columns can be used in filter conditions to determine
- * which observations should be evaluated.
- */
-export const observationEvalFilterColumns: ObservationEvalColumnDef[] = [
- {
- name: "Type",
- id: "type",
- type: "stringOptions",
- internal: "type",
- options: Object.values(ObservationType).map((key) => ({ value: key })),
- },
- {
- name: "Name",
- id: "name",
- type: "stringOptions",
- internal: "name",
- options: [], // to be filled at runtime
- },
- {
- name: "Environment",
- id: "environment",
- type: "stringOptions",
- internal: "environment",
- options: [], // to be filled at runtime
- },
- {
- name: "Status",
- id: "level",
- type: "stringOptions",
- internal: "level",
- options: Object.values(ObservationLevel).map((key) => ({ value: key })),
- aliases: ["Level"],
- },
- {
- name: "Version",
- id: "version",
- type: "string",
- internal: "version",
- nullable: true,
- },
- {
- name: "Release",
- id: "release",
- type: "string",
- internal: "release",
- nullable: true,
- },
- {
- name: "Status Message",
- id: "statusMessage",
- type: "string",
- internal: "status_message",
- nullable: true,
- },
- {
- name: "Provided Model Name",
- id: "providedModelName",
- type: "string",
- internal: "provided_model_name",
- nullable: true,
- },
- {
- name: "Prompt Name",
- id: "promptName",
- type: "string",
- internal: "prompt_name",
- nullable: true,
- },
- {
- name: "Prompt Version",
- id: "promptVersion",
- type: "number",
- internal: "prompt_version",
- nullable: true,
- },
- {
- name: "Trace Name",
- id: "traceName",
- type: "stringOptions",
- internal: "trace_name",
- options: [], // to be filled at runtime
- nullable: true,
- },
- {
- name: "User ID",
- id: "userId",
- type: "string",
- internal: "user_id",
- nullable: true,
- },
- {
- name: "Session ID",
- id: "sessionId",
- type: "string",
- internal: "session_id",
- nullable: true,
- },
- {
- name: "Tags",
- id: "tags",
- type: "arrayOptions",
- internal: "tags",
- options: [], // to be filled at runtime
- },
- {
- name: "Metadata",
- id: "metadata",
- type: "stringObject",
- internal: "metadata",
- },
- {
- name: "Is Root Observation",
- id: "isRootObservation",
- type: "boolean",
- internal: "is_app_root",
- },
- {
- name: "Parent Observation",
- id: "parentObservationId",
- type: "null",
- internal: "parent_span_id",
- nullable: true,
- },
- {
- name: "Experiment ID",
- id: "experimentId",
- type: "stringOptions",
- internal: "experiment_id",
- options: [], // to be filled at runtime
- nullable: true,
- },
- {
- name: "Experiment Name",
- id: "experimentName",
- type: "string",
- internal: "experiment_name",
- nullable: true,
- },
- {
- name: "Is Experiment Item Root Span",
- id: "isExperimentItemRootSpan",
- type: "boolean",
- internal: "experiment_item_root_span_id",
- },
- {
- name: "Called Tool Names",
- id: "calledToolNames",
- type: "arrayOptions",
- internal: "tool_call_names",
- options: [], // to be filled at runtime
- },
- {
- name: "Tool Call Count",
- id: "toolCalls",
- type: "number",
- internal: "tool_call_count",
- step: 1,
- min: 0,
- },
-];
-
-export const experimentEvalFilterColumns: ObservationEvalColumnDef[] = [
- {
- name: "Dataset",
- id: "experimentDatasetId",
- type: "stringOptions",
- internal: "experiment_dataset_id",
- options: [], // to be filled at runtime
- },
-];
-
-export const eventsEvalFilterColumns: ObservationEvalColumnDef[] = [
- ...observationEvalFilterColumns,
- ...experimentEvalFilterColumns,
-];
-
-// Options type for observation eval filters
-export type ObservationEvalOptions = {
- environment?: Array<SingleValueOption>;
- tags?: Array<SingleValueOption>;
- traceName?: Array<SingleValueOption>;
- name?: Array<SingleValueOption>;
- calledToolNames?: Array<SingleValueOption>;
-};
-
-export type ExperimentEvalOptions = {
- experimentDatasetId?: Array<SingleValueOption>;
-};
-
-export function observationEvalFilterColsWithOptions(
- options?: ObservationEvalOptions,
- cols: ColumnDefinition[] = observationEvalFilterColumns,
-): ColumnDefinition[] {
- return cols.map((col) => {
- if (col.id === "environment") {
- return formatColumnOptions(col, options?.environment ?? []);
- }
- if (col.id === "tags") {
- return formatColumnOptions(col, options?.tags ?? []);
- }
- if (col.id === "traceName") {
- return formatColumnOptions(col, options?.traceName ?? []);
- }
- if (col.id === "name") {
- return formatColumnOptions(col, options?.name ?? []);
- }
- if (col.id === "calledToolNames") {
- return formatColumnOptions(col, options?.calledToolNames ?? []);
- }
- return col;
- });
-}
-
-export function experimentEvalFilterColsWithOptions(
- options?: ExperimentEvalOptions,
- cols: ColumnDefinition[] = experimentEvalFilterColumns,
-): ColumnDefinition[] {
- return cols.map((col) => {
- if (col.id === "experimentDatasetId") {
- return formatColumnOptions(col, options?.experimentDatasetId ?? []);
- }
- return col;
- });
-}
-
-/**
- * Field mapper for observation eval filters.
- * Maps camelCase filter column IDs to snake_case observation fields.
- * Based on events table column definitions.
- *
- * @param observation - The observation data object
- * @param column - The camelCase column ID from filter definitions
- * @returns The value from the observation object
- */
-export function mapEventEvalFilterColumnIdToField(
- observation: ObservationForEval,
- column: string,
-) {
- const columnMapping = eventsEvalFilterColumns.find((c) => c.id === column);
- if (!columnMapping) {
- return undefined;
- }
-
- if (columnMapping.id === "isRootObservation") {
- return isRootObservation({
- parentObservationId: observation.parent_span_id,
- isAppRoot: observation.is_app_root,
- });
- }
-
- if (columnMapping.id === "isExperimentItemRootSpan") {
- return (
- observation.experiment_item_root_span_id !== null &&
- observation.experiment_item_root_span_id === observation.span_id
- );
- }
-
- return observation[columnMapping.internal];
-}
+export {
+ mapEventEvalFilterColumnIdToField,
+ type ObservationEvalFilterColumnInternal,
+} from "./filterFields";
+export {
+ observationEvalFilterColumns,
+ eventsEvalFilterColumns,
+ observationEvalFilterColsWithOptions,
+ type ObservationEvalOptions,
+} from "./spanFilters";
+export {
+ experimentEvalFilterColumns,
+ experimentEvalFilterColsWithOptions,
+ type ExperimentEvalOptions,
+} from "./experimentFilters";
diff --git a/packages/shared/src/features/evals/spanFilters.ts b/packages/shared/src/features/evals/spanFilters.ts
new file mode 100644
index 0000000000..b725211391
--- /dev/null
+++ b/packages/shared/src/features/evals/spanFilters.ts
@@ -0,0 +1,73 @@
+import type {
+ ColumnDefinition,
+ SingleValueOption,
+} from "../../tableDefinitions";
+import { formatColumnOptions } from "../../tableDefinitions/typeHelpers";
+import {
+ evalFilterFields,
+ type ObservationEvalColumnDef,
+} from "./filterFields";
+import { experimentEvalFilterColumns } from "./experimentFilters";
+
+export const observationEvalFilterColumns: ObservationEvalColumnDef[] = [
+ evalFilterFields.type,
+ evalFilterFields.name,
+ evalFilterFields.environment,
+ evalFilterFields.level,
+ evalFilterFields.version,
+ evalFilterFields.release,
+ evalFilterFields.statusMessage,
+ evalFilterFields.providedModelName,
+ evalFilterFields.promptName,
+ evalFilterFields.promptVersion,
+ evalFilterFields.traceName,
+ evalFilterFields.userId,
+ evalFilterFields.sessionId,
+ evalFilterFields.tags,
+ evalFilterFields.metadata,
+ evalFilterFields.isRootObservation,
+ evalFilterFields.parentObservationId,
+ evalFilterFields.experimentId,
+ evalFilterFields.experimentName,
+ evalFilterFields.isExperimentItemRootSpan,
+ evalFilterFields.calledToolNames,
+ evalFilterFields.toolCalls,
+];
+
+export const eventsEvalFilterColumns: ObservationEvalColumnDef[] = [
+ ...observationEvalFilterColumns,
+ ...experimentEvalFilterColumns,
+];
+
+// Options type for observation eval filters
+export type ObservationEvalOptions = {
+ environment?: Array<SingleValueOption>;
+ tags?: Array<SingleValueOption>;
+ traceName?: Array<SingleValueOption>;
+ name?: Array<SingleValueOption>;
+ calledToolNames?: Array<SingleValueOption>;
+};
+
+export function observationEvalFilterColsWithOptions(
+ options?: ObservationEvalOptions,
+ cols: ColumnDefinition[] = observationEvalFilterColumns,
+): ColumnDefinition[] {
+ return cols.map((col) => {
+ if (col.id === "environment") {
+ return formatColumnOptions(col, options?.environment ?? []);
+ }
+ if (col.id === "tags") {
+ return formatColumnOptions(col, options?.tags ?? []);
+ }
+ if (col.id === "traceName") {
+ return formatColumnOptions(col, options?.traceName ?? []);
+ }
+ if (col.id === "name") {
+ return formatColumnOptions(col, options?.name ?? []);
+ }
+ if (col.id === "calledToolNames") {
+ return formatColumnOptions(col, options?.calledToolNames ?? []);
+ }
+ return col;
+ });
+}
diff --git a/packages/shared/src/features/evals/traceTranscriptFilters.ts b/packages/shared/src/features/evals/traceTranscriptFilters.ts
new file mode 100644
index 0000000000..f1ce309bc4
--- /dev/null
+++ b/packages/shared/src/features/evals/traceTranscriptFilters.ts
@@ -0,0 +1,41 @@
+import {
+ evalFilterFields,
+ type EvalFilterDefinition,
+ type ObservationEvalColumnDef,
+} from "./filterFields";
+
+/** One observation must satisfy every condition to select its whole trace. */
+const traceTranscriptFilterColumns: ObservationEvalColumnDef[] = [
+ evalFilterFields.traceName,
+ evalFilterFields.userId,
+ evalFilterFields.sessionId,
+ evalFilterFields.tags,
+ evalFilterFields.metadata,
+ evalFilterFields.name,
+ evalFilterFields.type,
+ evalFilterFields.environment,
+ evalFilterFields.version,
+ evalFilterFields.release,
+ evalFilterFields.level,
+ evalFilterFields.statusMessage,
+ evalFilterFields.providedModelName,
+ evalFilterFields.promptName,
+ evalFilterFields.experimentId,
+ evalFilterFields.experimentName,
+ evalFilterFields.experimentDatasetId,
+ evalFilterFields.isRootObservation,
+ evalFilterFields.isExperimentItemRootSpan,
+];
+
+/** Restrict operators to predicates exposed for matching a single observation. */
+export const traceTranscriptFilterDefinition: EvalFilterDefinition = {
+ columns: traceTranscriptFilterColumns,
+ operators: {
+ string: ["=", "contains", "starts with", "ends with", "is not empty"],
+ stringOptions: ["any of"],
+ arrayOptions: ["any of", "all of"],
+ stringObject: ["=", "contains", "starts with", "ends with", "is set"],
+ boolean: ["="],
+ null: [],
+ },
+};
diff --git a/packages/shared/src/features/evals/validateEvaluatorFilters.ts b/packages/shared/src/features/evals/validateEvaluatorFilters.ts
index 65e8c6aa4e..ad0f313757 100644
--- a/packages/shared/src/features/evals/validateEvaluatorFilters.ts
+++ b/packages/shared/src/features/evals/validateEvaluatorFilters.ts
@@ -5,13 +5,14 @@ import {
evalTraceTableCols,
} from "../../tableDefinitions/tracesTable";
import type { FilterCondition, FilterState } from "../../types";
-import {
- eventsEvalFilterColumns,
- experimentEvalFilterColumns,
-} from "./observationForEval";
+import type { EvalFilterDefinition } from "./filterFields";
+import { eventsEvalFilterColumns } from "./spanFilters";
+import { experimentEvalFilterColumns } from "./experimentFilters";
+import { traceTranscriptFilterDefinition } from "./traceTranscriptFilters";
import { COMPATIBLE_FILTER_TYPES } from "../../server/queries/clickhouse-sql/filterTypeCompatibility";
import {
EvalTargetObject,
+ TopicsTargetObject,
type EvalTargetObject as EvalTargetObjectType,
} from "./types";
@@ -37,18 +38,20 @@ export type EvaluatorFilterValidationResult = {
const parsedFilterSchema = singleFilterList.nullable();
-const getSupportedColumnsForTarget = (
- targetObject: EvalTargetObjectType,
-): ColumnDefinition[] => {
+const getFilterDefinitionForTarget = (
+ targetObject: EvalTargetObjectType | typeof TopicsTargetObject,
+): EvalFilterDefinition => {
switch (targetObject) {
case EvalTargetObject.TRACE:
- return evalTraceTableCols;
+ return { columns: evalTraceTableCols };
case EvalTargetObject.DATASET:
- return evalDatasetFormFilterCols;
+ return { columns: evalDatasetFormFilterCols };
+ case TopicsTargetObject:
+ return traceTranscriptFilterDefinition;
case EvalTargetObject.EVENT:
- return eventsEvalFilterColumns;
+ return { columns: eventsEvalFilterColumns };
case EvalTargetObject.EXPERIMENT:
- return experimentEvalFilterColumns;
+ return { columns: experimentEvalFilterColumns };
}
};
@@ -64,10 +67,10 @@ const findMatchingColumnDefinition = (
);
export function validateEvaluatorFiltersForTarget(params: {
- targetObject: EvalTargetObjectType;
+ targetObject: EvalTargetObjectType | typeof TopicsTargetObject;
filter: unknown;
}): EvaluatorFilterValidationResult {
- const columns = getSupportedColumnsForTarget(params.targetObject);
+ const definition = getFilterDefinitionForTarget(params.targetObject);
const parsedFilter = parsedFilterSchema.safeParse(params.filter);
if (!parsedFilter.success) {
@@ -91,7 +94,10 @@ export function validateEvaluatorFiltersForTarget(params: {
const issues: EvaluatorFilterValidationIssue[] = filters.flatMap(
(filter, index): EvaluatorFilterValidationIssue[] => {
- const column = findMatchingColumnDefinition(filter.column, columns);
+ const column = findMatchingColumnDefinition(
+ filter.column,
+ definition.columns,
+ );
if (!column) {
return [
@@ -105,6 +111,21 @@ export function validateEvaluatorFiltersForTarget(params: {
];
}
+ if (
+ definition.operators &&
+ !definition.operators[filter.type]?.includes(filter.operator)
+ ) {
+ return [
+ {
+ code: "invalid_filter_shape",
+ message: `Filter operator "${filter.operator}" is not supported for target "${params.targetObject}".`,
+ index,
+ column: filter.column,
+ filterType: filter.type,
+ },
+ ];
+ }
+
if (filter.type === "null") {
return [];
}
diff --git a/web/src/__tests__/server/unit/evaluator-filter-validation.servertest.ts b/web/src/__tests__/server/unit/evaluator-filter-validation.servertest.ts
index 087311b619..d3edf85564 100644
--- a/web/src/__tests__/server/unit/evaluator-filter-validation.servertest.ts
+++ b/web/src/__tests__/server/unit/evaluator-filter-validation.servertest.ts
@@ -1,5 +1,6 @@
import {
EvalTargetObject,
+ TopicsTargetObject,
validateEvaluatorFiltersForTarget,
type FilterState,
} from "@langfuse/shared";
@@ -72,6 +73,35 @@ describe("validateEvaluatorFiltersForTarget", () => {
},
] satisfies FilterState,
},
+ {
+ targetObject: TopicsTargetObject,
+ filter: [
+ {
+ type: "string",
+ column: "providedModelName",
+ operator: "=",
+ value: "gpt-4o",
+ },
+ {
+ type: "string",
+ column: "Prompt Name",
+ operator: "starts with",
+ value: "support",
+ },
+ {
+ type: "string",
+ column: "statusMessage",
+ operator: "contains",
+ value: "rate limit",
+ },
+ {
+ type: "boolean",
+ column: "isExperimentItemRootSpan",
+ operator: "=",
+ value: true,
+ },
+ ] satisfies FilterState,
+ },
])(
"accepts supported filters for $targetObject",
({ targetObject, filter }) => {
@@ -128,4 +158,23 @@ describe("validateEvaluatorFiltersForTarget", () => {
expectedColumnType: "stringOptions",
});
});
+
+ it("rejects unsupported trace transcript filters", () => {
+ const result = validateEvaluatorFiltersForTarget({
+ targetObject: TopicsTargetObject,
+ filter: [
+ { type: "number", column: "promptVersion", operator: ">", value: 1 },
+ {
+ type: "string",
+ column: "promptName",
+ operator: "does not contain",
+ value: "support",
+ },
+ { type: "null", column: "sessionId", operator: "is null", value: "" },
+ ] satisfies FilterState,
+ });
+
+ expect(result.isValid).toBe(false);
+ expect(result.issues.map((issue) => issue.index)).toEqual([0, 1, 2]);
+ });
});