Skip to content

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

feat(filters): metadata key presence operators; coerce legacy empty-substring filters (#17357)

Niklas Semmler2db6c3018fff
diff --git a/fern/apis/server/definition/evaluation-commons.yml b/fern/apis/server/definition/evaluation-commons.yml
index 2c43010194..33b26daf57 100644
--- a/fern/apis/server/definition/evaluation-commons.yml
+++ b/fern/apis/server/definition/evaluation-commons.yml
@@ -488,7 +488,7 @@ types:
- `datetime`: `"="`, `">"`, `"<"`, `">="`, `"<="`
- `stringOptions`: `any of`, `none of`
- `arrayOptions`: `any of`, `none of`, `all of`
- - `stringObject`: same operators as `string`
+ - `stringObject`: `"="`, `contains`, `does not contain`, `starts with`, `ends with`, `is set`, `is not set` (use `is set` / `is not set` for key presence; an empty value for `contains`, `starts with`, or `ends with` is treated as `is set`)
- `boolean`: `"="`, `"<>"`
- `null`: `is null`, `is not null`
diff --git a/fern/apis/server/definition/legacy/observations-v1.yml b/fern/apis/server/definition/legacy/observations-v1.yml
index 646556ce61..50dab3e370 100644
--- a/fern/apis/server/definition/legacy/observations-v1.yml
+++ b/fern/apis/server/definition/legacy/observations-v1.yml
@@ -83,7 +83,7 @@ service:
// - categoryOptions: "any of", "none of"
// - arrayOptions: "any of", "none of", "all of"
// - number: "=", ">", "<", ">=", "<="
- // - stringObject: "=", "contains", "does not contain", "starts with", "ends with"
+ // - stringObject: "=", "contains", "does not contain", "starts with", "ends with", "is set", "is not set"
// - numberObject: "=", ">", "<", ">=", "<="
// - boolean: "=", "<>"
// - null: "is null", "is not null"
diff --git a/fern/apis/server/definition/observations.yml b/fern/apis/server/definition/observations.yml
index 09ba59e2d7..552b3fb7a5 100644
--- a/fern/apis/server/definition/observations.yml
+++ b/fern/apis/server/definition/observations.yml
@@ -120,7 +120,7 @@ service:
// - categoryOptions: "any of", "none of"
// - arrayOptions: "any of", "none of", "all of"
// - number: "=", ">", "<", ">=", "<="
- // - stringObject: "=", "contains", "does not contain", "starts with", "ends with", "matches"
+ // - stringObject: "=", "contains", "does not contain", "starts with", "ends with", "matches", "is set", "is not set"
// - numberObject: "=", ">", "<", ">=", "<="
// - boolean: "=", "<>"
// - null: "is null", "is not null"
diff --git a/fern/apis/server/definition/scores.yml b/fern/apis/server/definition/scores.yml
index 6df30711b3..f403b89817 100644
--- a/fern/apis/server/definition/scores.yml
+++ b/fern/apis/server/definition/scores.yml
@@ -91,7 +91,8 @@ service:
Supports filtering by score metadata using the stringObject type.
Example: [{"type":"stringObject","column":"metadata","key":"user_id","operator":"=","value":"abc123"}].
Supported types: stringObject (metadata key-value filtering), string, number, datetime, stringOptions, arrayOptions.
- Supported operators for stringObject: =, contains, does not contain, starts with, ends with.
+ Supported operators for stringObject: =, contains, does not contain, starts with, ends with, is set, is not set.
+ Use is set / is not set to filter on metadata key presence. An empty value for contains, starts with, or ends with is treated as is set (key presence).
response: GetScoresResponse
get-by-id:
availability:
diff --git a/fern/apis/server/definition/trace.yml b/fern/apis/server/definition/trace.yml
index 004056be9f..ce93e784d8 100644
--- a/fern/apis/server/definition/trace.yml
+++ b/fern/apis/server/definition/trace.yml
@@ -97,7 +97,7 @@ service:
// - categoryOptions: "any of", "none of"
// - arrayOptions: "any of", "none of", "all of"
// - number: "=", ">", "<", ">=", "<="
- // - stringObject: "=", "contains", "does not contain", "starts with", "ends with"
+ // - stringObject: "=", "contains", "does not contain", "starts with", "ends with", "is set", "is not set"
// - numberObject: "=", ">", "<", ">=", "<="
// - booleanObject: "=", "<>"
// - boolean: "=", "<>"
diff --git a/packages/shared/src/domain/table-view-presets.ts b/packages/shared/src/domain/table-view-presets.ts
index 6b7dd76562..5f7da0321a 100644
--- a/packages/shared/src/domain/table-view-presets.ts
+++ b/packages/shared/src/domain/table-view-presets.ts
@@ -1,4 +1,4 @@
-import { singleFilter } from "../interfaces/filters";
+import { singleFilterList } from "../interfaces/filters";
import { orderBy } from "../interfaces/orderBy";
import z from "zod";
@@ -67,7 +67,7 @@ export const TableViewPresetDomainSchema = z.object({
createdBy: z.string().nullable(),
name: z.string(),
tableName: z.enum(TableViewPresetTableName),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
columnOrder: z.array(z.string()),
columnVisibility: z.record(z.string(), z.boolean()),
searchQuery: z.string().nullable(),
diff --git a/packages/shared/src/features/batchAction/types.ts b/packages/shared/src/features/batchAction/types.ts
index a8a9bdc163..6d1734f9b5 100644
--- a/packages/shared/src/features/batchAction/types.ts
+++ b/packages/shared/src/features/batchAction/types.ts
@@ -1,5 +1,5 @@
import z from "zod";
-import { singleFilter } from "../../interfaces/filters";
+import { singleFilterList } from "../../interfaces/filters";
import { orderBy } from "../../interfaces/orderBy";
import { BatchTableNames } from "../../interfaces/tableNames";
import {
@@ -37,7 +37,7 @@ const ActionIdSchema = z.enum(ActionId);
export const BatchActionQuerySchema = z
.object({
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
orderBy,
searchQuery: z.string().optional(),
searchType: z.array(TracingSearchType).optional(),
diff --git a/packages/shared/src/features/batchExport/types.ts b/packages/shared/src/features/batchExport/types.ts
index 09012304bb..a71980d16e 100644
--- a/packages/shared/src/features/batchExport/types.ts
+++ b/packages/shared/src/features/batchExport/types.ts
@@ -2,7 +2,7 @@ import z from "zod";
import { BatchExport } from "@prisma/client";
-import { singleFilter } from "../../interfaces/filters";
+import { singleFilterList } from "../../interfaces/filters";
import { orderBy } from "../../interfaces/orderBy";
import { BatchTableNames } from "../../interfaces/tableNames";
import {
@@ -53,7 +53,7 @@ export const exportOptions: Record<
export const BatchExportQuerySchema = z
.object({
tableName: z.enum(BatchTableNames),
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
searchQuery: z.string().optional(),
searchType: z.array(TracingSearchType).optional(),
orderBy,
diff --git a/packages/shared/src/features/evals/validateEvaluatorFilters.ts b/packages/shared/src/features/evals/validateEvaluatorFilters.ts
index 7893c90414..65e8c6aa4e 100644
--- a/packages/shared/src/features/evals/validateEvaluatorFilters.ts
+++ b/packages/shared/src/features/evals/validateEvaluatorFilters.ts
@@ -1,5 +1,4 @@
-import { z } from "zod";
-import { singleFilter } from "../../interfaces/filters";
+import { singleFilterList } from "../../interfaces/filters";
import type { ColumnDefinition } from "../../tableDefinitions";
import {
evalDatasetFormFilterCols,
@@ -36,7 +35,7 @@ export type EvaluatorFilterValidationResult = {
issues: EvaluatorFilterValidationIssue[];
};
-const parsedFilterSchema = z.array(singleFilter).nullable();
+const parsedFilterSchema = singleFilterList.nullable();
const getSupportedColumnsForTarget = (
targetObject: EvalTargetObjectType,
diff --git a/packages/shared/src/features/filters/filterQueryEncoding.ts b/packages/shared/src/features/filters/filterQueryEncoding.ts
index a9ae14b938..1dd66918fd 100644
--- a/packages/shared/src/features/filters/filterQueryEncoding.ts
+++ b/packages/shared/src/features/filters/filterQueryEncoding.ts
@@ -1,5 +1,8 @@
import { type FilterState } from "../../types";
-import { singleFilter } from "../../interfaces/filters";
+import {
+ coerceLegacyEmptyMetadataFilters,
+ singleFilter,
+} from "../../interfaces/filters";
import { type SingleValueOption } from "../../tableDefinitions/types";
import { normalizeLegacySessionPositionInTraceKey } from "./sessionPositionInTrace";
@@ -219,8 +222,11 @@ export function decodeFiltersGeneric(query: string): FilterState {
}
}
- // Validate with zod
- const parsed = singleFilter.safeParse(filter);
+ // Coerce the legacy `contains ""` presence spelling to `is set` so a
+ // bookmarked/shared link carrying it survives instead of being dropped.
+ const [coerced] = coerceLegacyEmptyMetadataFilters([filter]) as unknown[];
+
+ const parsed = singleFilter.safeParse(coerced);
if (parsed.success) {
filters.push(parsed.data);
} else {
diff --git a/packages/shared/src/features/monitors/scheduler/types.test.ts b/packages/shared/src/features/monitors/scheduler/types.test.ts
index af72e7eacf..2aa9b98aab 100644
--- a/packages/shared/src/features/monitors/scheduler/types.test.ts
+++ b/packages/shared/src/features/monitors/scheduler/types.test.ts
@@ -88,6 +88,38 @@ describe("MonitorQueueEventSchema", () => {
.success,
).toBe(true);
});
+
+ // The scheduler reads monitors.filters via raw SQL and bypasses the coercion
+ // in monitorFromPrisma, so a monitor persisted with the legacy empty-substring
+ // presence spelling reaches this schema unrewritten. Parsing must coerce it to
+ // `is set` rather than throw — a throw here fails the whole shared scheduler
+ // batch, not just the one monitor.
+ it("coerces a legacy empty-substring metadata filter to `is set`", () => {
+ const result = MonitorQueueEventSchema.safeParse({
+ ...validQueueEvent,
+ filters: [
+ {
+ type: "stringObject",
+ column: "metadata",
+ key: "environment",
+ operator: "contains",
+ value: "",
+ },
+ ],
+ });
+ expect(result.success).toBe(true);
+ if (result.success) {
+ expect(result.data.filters).toEqual([
+ {
+ type: "stringObject",
+ column: "metadata",
+ key: "environment",
+ operator: "is set",
+ value: "",
+ },
+ ]);
+ }
+ });
});
describe("MonitorWebhookQueueEventSchema", () => {
diff --git a/packages/shared/src/features/monitors/service/helpers.ts b/packages/shared/src/features/monitors/service/helpers.ts
index 6c3582468b..2a5898b8e4 100644
--- a/packages/shared/src/features/monitors/service/helpers.ts
+++ b/packages/shared/src/features/monitors/service/helpers.ts
@@ -11,6 +11,7 @@ import {
Prisma,
} from "@prisma/client";
+import { coerceLegacyEmptyMetadataFilters } from "../../../interfaces/filters";
import { DAY, HOUR, MINUTE, WEEK } from "../helpers";
import {
type Monitor,
@@ -201,6 +202,9 @@ export { windowToMs, windowFromMs };
export const monitorFromPrisma = (monitor: PrismaMonitor): Monitor =>
MonitorSchema.parse({
...monitor,
+ // Persisted filters may use the legacy metadata `contains ""` key-presence
+ // idiom that the value guard now rejects; coerce it to `is set` on read.
+ filters: coerceLegacyEmptyMetadataFilters(monitor.filters),
view: viewFromPrisma(monitor.view),
window: windowFromMs(monitor.windowMs),
alertThreshold: monitor.alertThreshold.toNumber(),
diff --git a/packages/shared/src/features/monitors/types.ts b/packages/shared/src/features/monitors/types.ts
index 4ccc1f58b0..4ec25e1194 100644
--- a/packages/shared/src/features/monitors/types.ts
+++ b/packages/shared/src/features/monitors/types.ts
@@ -8,7 +8,7 @@ import {
import { z } from "zod";
import { InvalidRequestError } from "../../errors";
-import { singleFilter } from "../../interfaces/filters";
+import { singleFilterList } from "../../interfaces/filters";
import { granularities, metric as MetricSchema, viewsV2 } from "../query/types";
import { isValidQuery } from "./isValidQuery";
@@ -29,10 +29,13 @@ export const ErrorAtLeastOneTrigger = "At least one automation is required";
/**
* MonitorFiltersSchema is the array of filters applied to a Monitor's
- * underlying query — a thin alias over `singleFilter[]` so all Monitor
- * schemas reference one source of truth.
+ * underlying query. It routes through `singleFilterList` so the scheduler's
+ * raw-SQL read path (which bypasses `monitorFromPrisma`) still coerces legacy
+ * empty-substring metadata filters to `is set` before the queue event is
+ * validated in the worker — otherwise a single legacy filter throws on parse
+ * and takes down the whole shared scheduler batch.
*/
-export const MonitorFiltersSchema = z.array(singleFilter);
+export const MonitorFiltersSchema = singleFilterList;
export type MonitorFilters = z.infer<typeof MonitorFiltersSchema>;
/** MonitorSeveritySchema is the wire form of Prisma's `MonitorSeverity` enum. */
diff --git a/packages/shared/src/features/query/types.ts b/packages/shared/src/features/query/types.ts
index 2b9398a36a..68f15799ef 100644
--- a/packages/shared/src/features/query/types.ts
+++ b/packages/shared/src/features/query/types.ts
@@ -1,5 +1,5 @@
import { z } from "zod";
-import { singleFilter } from "../../interfaces/filters";
+import { singleFilterList } from "../../interfaces/filters";
export const metricAggregations = z.enum([
"sum",
@@ -104,7 +104,7 @@ export const viewDeclaration = z.object({
}),
),
// Segments are used to apply "constant" filters to the query. For example, if we only want one type of observations.
- segments: z.array(singleFilter),
+ segments: singleFilterList,
timeDimension: z.string(),
// When set, adds a subquery filter to restrict rows to those whose "root event"
// (matching the condition) has timeDimension in the query window.
@@ -221,7 +221,7 @@ export const query = z
view: z.union([views, z.literal(SCORES_LISTABLE_COUNT_VIEW)]),
dimensions: z.array(dimension),
metrics: z.array(metric),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
timeDimension: z
.object({
// TODO: We may want to extend this and allow custom intervals like 3h in the future.
diff --git a/packages/shared/src/features/scores/interfaces/api/shared.ts b/packages/shared/src/features/scores/interfaces/api/shared.ts
index bee00fc7f9..4c9f3c00e9 100644
--- a/packages/shared/src/features/scores/interfaces/api/shared.ts
+++ b/packages/shared/src/features/scores/interfaces/api/shared.ts
@@ -15,7 +15,7 @@ import {
ScoreSourceEnum,
TEXT_SCORE_MAX_LENGTH,
} from "../../../../domain/scores";
-import { singleFilter } from "../../../../interfaces/filters";
+import { singleFilterList } from "../../../../interfaces/filters";
import { InvalidRequestError } from "../../../../errors";
const operators = ["<", ">", "<=", ">=", "!=", "="] as const;
@@ -69,7 +69,7 @@ export const GetScoresQuery = z.object({
throw new InvalidRequestError("Invalid JSON in filter parameter");
}
})
- .pipe(z.array(singleFilter).optional()),
+ .pipe(singleFilterList.optional()),
});
// POST /scores
diff --git a/packages/shared/src/interfaces/filters.test.ts b/packages/shared/src/interfaces/filters.test.ts
new file mode 100644
index 0000000000..5d68b235ef
--- /dev/null
+++ b/packages/shared/src/interfaces/filters.test.ts
@@ -0,0 +1,132 @@
+import { describe, expect, it } from "vitest";
+
+import {
+ singleFilter,
+ singleFilterList,
+ eventsTableSingleFilter,
+ eventsTableSingleFilterList,
+ coerceLegacyEmptyMetadataFilters,
+} from "./filters";
+
+const metadataFilter = (operator: string, value: string) => ({
+ type: "stringObject" as const,
+ column: "metadata",
+ key: "turn",
+ operator,
+ value,
+});
+
+describe("stringObject empty substring handling", () => {
+ it.each(["contains", "starts with", "ends with"])(
+ "no longer rejects an empty value for the substring operator %s at the single-filter schema",
+ (operator) => {
+ expect(singleFilter.safeParse(metadataFilter(operator, "")).success).toBe(
+ true,
+ );
+ expect(
+ eventsTableSingleFilter.safeParse(metadataFilter(operator, "")).success,
+ ).toBe(true);
+ },
+ );
+
+ it.each(["contains", "starts with", "ends with"])(
+ "accepts a non-empty value for the substring operator %s",
+ (operator) => {
+ expect(
+ singleFilter.safeParse(metadataFilter(operator, "x")).success,
+ ).toBe(true);
+ },
+ );
+
+ it.each(["is set", "is not set"])(
+ "accepts the presence operator %s with an empty value",
+ (operator) => {
+ expect(singleFilter.safeParse(metadataFilter(operator, "")).success).toBe(
+ true,
+ );
+ },
+ );
+
+ it("accepts an empty value for = and does not contain", () => {
+ expect(singleFilter.safeParse(metadataFilter("=", "")).success).toBe(true);
+ expect(
+ singleFilter.safeParse(metadataFilter("does not contain", "")).success,
+ ).toBe(true);
+ });
+});
+
+describe("singleFilterList", () => {
+ it.each(["contains", "starts with", "ends with"])(
+ "coerces a legacy empty-value %s metadata filter to `is set`",
+ (operator) => {
+ const result = singleFilterList.safeParse([metadataFilter(operator, "")]);
+ expect(result.success).toBe(true);
+ if (result.success) {
+ expect(result.data[0].operator).toBe("is set");
+ }
+ },
+ );
+
+ it("leaves a non-empty substring filter untouched", () => {
+ const result = singleFilterList.safeParse([
+ metadataFilter("contains", "x"),
+ ]);
+ expect(result.success).toBe(true);
+ if (result.success) {
+ expect(result.data[0].operator).toBe("contains");
+ }
+ });
+});
+
+describe("eventsTableSingleFilterList", () => {
+ it.each(["contains", "starts with", "ends with"])(
+ "coerces a legacy empty-value %s metadata filter to `is set`",
+ (operator) => {
+ const result = eventsTableSingleFilterList.safeParse([
+ metadataFilter(operator, ""),
+ ]);
+ expect(result.success).toBe(true);
+ if (result.success) {
+ expect(result.data[0].operator).toBe("is set");
+ }
+ },
+ );
+
+ it("leaves a non-empty substring filter untouched", () => {
+ const result = eventsTableSingleFilterList.safeParse([
+ metadataFilter("contains", "x"),
+ ]);
+ expect(result.success).toBe(true);
+ if (result.success) {
+ expect(result.data[0].operator).toBe("contains");
+ }
+ });
+});
+
+describe("coerceLegacyEmptyMetadataFilters", () => {
+ it.each(["contains", "starts with", "ends with"])(
+ "rewrites a legacy empty-value %s metadata filter to `is set` so it parses",
+ (operator) => {
+ const coerced = coerceLegacyEmptyMetadataFilters([
+ metadataFilter(operator, ""),
+ ]);
+ expect((coerced as { operator: string }[])[0].operator).toBe("is set");
+ expect(singleFilter.safeParse((coerced as unknown[])[0]).success).toBe(
+ true,
+ );
+ },
+ );
+
+ it("leaves non-empty and non-substring filters untouched", () => {
+ const input = [
+ metadataFilter("contains", "x"),
+ metadataFilter("=", ""),
+ { type: "string", column: "name", operator: "contains", value: "" },
+ ];
+ expect(coerceLegacyEmptyMetadataFilters(input)).toEqual(input);
+ });
+
+ it("passes through non-array input unchanged", () => {
+ expect(coerceLegacyEmptyMetadataFilters(null)).toBe(null);
+ });
+});
diff --git a/packages/shared/src/interfaces/filters.ts b/packages/shared/src/interfaces/filters.ts
index ac59691a60..d44f73c75b 100644
--- a/packages/shared/src/interfaces/filters.ts
+++ b/packages/shared/src/interfaces/filters.ts
@@ -21,6 +21,8 @@ export const filterOperators = {
"does not contain",
"starts with",
"ends with",
+ "is set",
+ "is not set",
],
numberObject: ["=", ">", "<", ">=", "<="],
booleanObject: ["=", "<>"],
@@ -74,6 +76,17 @@ export const arrayOptionsFilter = z
"Value array must not be empty unless operator is 'all of' or 'none of' (which represent waiting for selection)",
},
);
+// A substring operator with an empty value skips the ngram prefilter and
+// degrades to a full-scan key-existence check over the whole time window — the
+// same semantics the `is set` presence operator now expresses cleanly. Empty
+// substrings are the legacy spelling of presence, so callers coerce them to
+// `is set` (see `coerceLegacyEmptyMetadataFilters`) rather than reject them.
+const LEGACY_EMPTY_SUBSTRING_STRING_OBJECT_OPERATORS = new Set<string>([
+ "contains",
+ "starts with",
+ "ends with",
+]);
+
export const stringObjectFilter = z.object({
type: z.literal("stringObject"),
column: z.string(),
@@ -81,6 +94,29 @@ export const stringObjectFilter = z.object({
operator: z.enum(filterOperators.stringObject),
value: z.string(),
});
+
+// Metadata `contains ""` / `starts with ""` / `ends with ""` was the historical
+// way to express key presence before `is set` / `is not set` existed. Rewrite
+// that shape to the equivalent `is set` at every parse boundary — fresh API
+// input and persisted reads alike — so it never reaches the SQL layer as an
+// empty substring and never throws on parse.
+export const coerceLegacyEmptyMetadataFilters = (filters: unknown): unknown => {
+ if (!Array.isArray(filters)) return filters;
+ return filters.map((filter) => {
+ if (
+ filter &&
+ typeof filter === "object" &&
+ (filter as { type?: unknown }).type === "stringObject" &&
+ (filter as { value?: unknown }).value === "" &&
+ LEGACY_EMPTY_SUBSTRING_STRING_OBJECT_OPERATORS.has(
+ (filter as { operator?: unknown }).operator as string,
+ )
+ ) {
+ return { ...(filter as object), operator: "is set" };
+ }
+ return filter;
+ });
+};
export const numberObjectFilter = z.object({
type: z.literal("numberObject"),
column: z.string(),
@@ -147,6 +183,20 @@ export const singleFilter = z.discriminatedUnion("type", [
positionInTraceFilter,
]);
+// Single choke point for parsing arrays of filters. `z.preprocess` runs the
+// legacy-empty-substring coercion before validation, so both fresh API/tRPC
+// input and persisted reads route through one place instead of remembering to
+// wrap each call site. Prefer this over a bare `z.array(singleFilter)`.
+// The cast pins the input type to `SingleFilter[]`. Without it `z.preprocess`
+// infers `unknown` input (the coercer takes `unknown`), which would surface as
+// `unknown` on tRPC mutation variables and form values that consume `z.input`.
+// A legacy `contains ""` filter is itself a valid `singleFilter`, so the coerced
+// shape is fully within this input type.
+export const singleFilterList = z.preprocess(
+ coerceLegacyEmptyMetadataFilters,
+ z.array(singleFilter),
+) as z.ZodType<z.output<typeof singleFilter>[], z.input<typeof singleFilter>[]>;
+
const eventsTableStringOperator = z.union([
z.enum(filterOperators.string),
z.literal(FTS_MATCH_OPERATOR),
@@ -181,3 +231,15 @@ export const eventsTableSingleFilter = z.discriminatedUnion("type", [
]);
export const eventsTableFilterState = z.array(eventsTableSingleFilter);
+
+// Coercing choke point for the events-table filter variant, mirroring
+// `singleFilterList`. Prefer this over a bare `z.array(eventsTableSingleFilter)`
+// when parsing external input so legacy empty-substring metadata filters are
+// rewritten to `is set` instead of throwing at the SQL layer.
+export const eventsTableSingleFilterList = z.preprocess(
+ coerceLegacyEmptyMetadataFilters,
+ eventsTableFilterState,
+) as z.ZodType<
+ z.output<typeof eventsTableSingleFilter>[],
+ z.input<typeof eventsTableSingleFilter>[]
+>;
diff --git a/packages/shared/src/server/filterToPrisma.test.ts b/packages/shared/src/server/filterToPrisma.test.ts
new file mode 100644
index 0000000000..5491d28112
--- /dev/null
+++ b/packages/shared/src/server/filterToPrisma.test.ts
@@ -0,0 +1,45 @@
+import { describe, expect, it } from "vitest";
+
+import { type ColumnDefinition } from "../tableDefinitions";
+import { type FilterState } from "../types";
+import { tableColumnsToSqlFilter } from "./filterToPrisma";
+
+const metadataCol: ColumnDefinition = {
+ name: "metadata",
+ id: "metadata",
+ type: "stringObject",
+ internal: "di.metadata",
+};
+
+const build = (operator: "is set" | "is not set") =>
+ tableColumnsToSqlFilter(
+ [
+ {
+ type: "stringObject",
+ column: "metadata",
+ key: "env",
+ operator,
+ value: "",
+ },
+ ] as FilterState,
+ [metadataCol],
+ "dataset_items",
+ );
+
+describe("tableColumnsToSqlFilter metadata presence operators", () => {
+ it("compiles `is set` to a jsonb key-existence check with the key bound as a param", () => {
+ const sql = build("is set");
+ expect(sql.sql).toContain("jsonb_exists");
+ expect(sql.sql).toContain("::jsonb");
+ expect(sql.sql).not.toMatch(/\bis set\b/);
+ expect(sql.values).toContain("env");
+ });
+
+ it("compiles `is not set` to a negated key-existence check", () => {
+ const sql = build("is not set");
+ expect(sql.sql).toContain("jsonb_exists");
+ expect(sql.sql).toContain("NOT");
+ expect(sql.sql).not.toMatch(/\bis not set\b/);
+ expect(sql.values).toContain("env");
+ });
+});
diff --git a/packages/shared/src/server/filterToPrisma.ts b/packages/shared/src/server/filterToPrisma.ts
index 4a315bdc08..8734b32837 100644
--- a/packages/shared/src/server/filterToPrisma.ts
+++ b/packages/shared/src/server/filterToPrisma.ts
@@ -128,6 +128,18 @@ export function tableColumnsToSqlFilter(
return Prisma.sql`(${filterAndColumn.internalColumn} IS NOT NULL AND ${filterAndColumn.internalColumn} <> '')`;
}
+ if (
+ filter.type === "stringObject" &&
+ (filter.operator === "is set" || filter.operator === "is not set")
+ ) {
+ // Key presence, mirroring the ClickHouse `has`/`mapContains` semantics.
+ // COALESCE handles a NULL column (no metadata at all) as "key absent".
+ const keyExists = Prisma.sql`COALESCE(jsonb_exists(${filterAndColumn.internalColumn}::jsonb, ${filter.key}), false)`;
+ return filter.operator === "is set"
+ ? keyExists
+ : Prisma.sql`NOT ${keyExists}`;
+ }
+
const jsonKeyPrisma =
filter.type === "stringObject" || filter.type === "numberObject"
? Prisma.sql`->>${filter.key}`
diff --git a/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.events-full-routing.test.ts b/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.events-full-routing.test.ts
index daec275c62..952c504f7f 100644
--- a/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.events-full-routing.test.ts
+++ b/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.events-full-routing.test.ts
@@ -84,6 +84,11 @@ describe("metadataFilterIsEventsCoreSafe", () => {
expect(metadataFilterIsEventsCoreSafe("is not null", undefined)).toBe(true);
});
+ it("keeps key presence/absence checks on events_core (metadata_names is untruncated)", () => {
+ expect(metadataFilterIsEventsCoreSafe("is set", "")).toBe(true);
+ expect(metadataFilterIsEventsCoreSafe("is not set", "")).toBe(true);
+ });
+
it("counts by code point, not UTF-16 unit (astral chars near the boundary)", () => {
// 100 astral code points = 200 UTF-16 units but only 100 code points, so an
// `=` filter stays on events_core.
@@ -154,6 +159,64 @@ describe("filtersRequireEventsFull metadata routing", () => {
});
});
+describe("StringObjectFilter key presence/absence operators", () => {
+ it("compiles `is set` / `is not set` to has() on events tables", () => {
+ expect(metadataString("is set", "").apply().query).toMatch(
+ /^has\(metadata_names, \{stringObjectKeyFilter\w+: String\}\)$/,
+ );
+ expect(metadataString("is not set", "").apply().query).toMatch(
+ /^NOT \(has\(metadata_names, \{stringObjectKeyFilter\w+: String\}\)\)$/,
+ );
+ });
+
+ it("compiles `is set` / `is not set` to mapContains() on map tables", () => {
+ const mapFilter = (operator: StringObjectFilter["operator"]) =>
+ new StringObjectFilter({
+ clickhouseTable: "traces",
+ field: "metadata",
+ key: "some_key",
+ operator,
+ value: "",
+ });
+ expect(mapFilter("is set").apply().query).toMatch(
+ /^mapContains\(metadata, \{stringObjectKeyFilter\w+: String\}\)$/,
+ );
+ expect(mapFilter("is not set").apply().query).toMatch(
+ /^NOT \(mapContains\(metadata, \{stringObjectKeyFilter\w+: String\}\)\)$/,
+ );
+ });
+
+ it("keeps presence/absence filters on events_core", () => {
+ expect(
+ filtersRequireEventsFull(new FilterList([metadataString("is set", "")])),
+ ).toBe(false);
+ expect(
+ filtersRequireEventsFull(
+ new FilterList([metadataString("is not set", "")]),
+ ),
+ ).toBe(false);
+ });
+});
+
+describe("StringObjectFilter empty-value rejection", () => {
+ it.each(["contains", "starts with", "ends with"] as const)(
+ "rejects an empty value for the substring operator `%s`",
+ (operator) => {
+ expect(() => metadataString(operator, "").apply()).toThrow(
+ /Empty value is not allowed/,
+ );
+ },
+ );
+
+ it("allows a non-empty value for substring operators", () => {
+ expect(() => metadataString("contains", "x").apply()).not.toThrow();
+ });
+
+ it("allows an empty value for `=` (empty-string equality is a valid match)", () => {
+ expect(() => metadataString("=", "").apply()).not.toThrow();
+ });
+});
+
describe("filtersRequireEventsFull input/output routing", () => {
const ioFilter = (field: "input" | "output") =>
new StringFilter({
diff --git a/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.ts b/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.ts
index b44752ad1d..8c3de3dde2 100644
--- a/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.ts
+++ b/packages/shared/src/server/queries/clickhouse-sql/clickhouse-filter.ts
@@ -3,6 +3,7 @@ import {
type FtsMatchOperator,
filterOperators,
} from "../../../interfaces/filters";
+import { InvalidRequestError } from "../../../errors";
import { convertDateToClickhouseDateTime } from "../../clickhouse/client";
import { clickhouseCompliantRandomCharacters } from "../../repositories";
import { escapeSqlLikePattern } from "../../utils/sqlLike";
@@ -35,6 +36,14 @@ const NGRAM_ACCELERATED_METADATA_OPERATORS = new Set<
(typeof filterOperators)["stringObject"][number]
>(["contains", "starts with", "ends with"]);
+// Substring operators with an empty value skip the ngram prefilter and degrade
+// to a full-scan "does this key exist" over the whole time window — a confirmed
+// events_full timeout vector. Callers who want key existence must use the
+// `is set` / `is not set` presence operators instead.
+const NON_EMPTY_VALUE_METADATA_OPERATORS = new Set<
+ (typeof filterOperators)["stringObject"][number]
+>(["contains", "starts with", "ends with"]);
+
export class StringFilter implements Filter {
public clickhouseTable: string;
public field: string;
@@ -359,6 +368,16 @@ export class StringObjectFilter implements Filter {
}
apply(): ClickhouseFilter {
+ if (
+ this.operator !== FTS_MATCH_OPERATOR &&
+ NON_EMPTY_VALUE_METADATA_OPERATORS.has(this.operator) &&
+ this.value.length === 0
+ ) {
+ throw new InvalidRequestError(
+ `Empty value is not allowed for metadata '${this.operator}' filters. Use the 'is set' / 'is not set' operators to filter on key presence.`,
+ );
+ }
+
const varKeyName = `stringObjectKeyFilter${clickhouseCompliantRandomCharacters()}`;
const varValueName = `stringObjectValueFilter${clickhouseCompliantRandomCharacters()}`;
const prefix = this.tablePrefix ? this.tablePrefix + "." : "";
@@ -411,6 +430,12 @@ export class StringObjectFilter implements Filter {
case "ends with":
query = `${hasKey}${ngramConjunct} AND (endsWith(${valueAccessor}, ${valueParam}))`;
break;
+ case "is set":
+ query = hasKey;
+ break;
+ case "is not set":
+ query = `NOT (${hasKey})`;
+ break;
case FTS_MATCH_OPERATOR:
assertValidFtsMatchFilter({
filterType: "stringObject",
@@ -468,6 +493,12 @@ export class StringObjectFilter implements Filter {
case "ends with":
query = `${hasKey} AND (endsWith(${valueAccessor}, {${varValueName}: String}))`;
break;
+ case "is set":
+ query = hasKey;
+ break;
+ case "is not set":
+ query = `NOT (${hasKey})`;
+ break;
default:
throw new Error(`Unsupported operator: ${this.operator}`);
}
@@ -760,6 +791,8 @@ const EVENTS_CORE_TRUNCATION_LIMIT = 200;
// - `<>`: boolean not-equal — value is inherently short.
// - `is null` / `is not null`: truncation-invariant (metadata_names is not
// truncated; emptiness is unaffected).
+// - `is set` / `is not set`: key-presence over metadata_names only, which is
+// not truncated.
// Deliberately excluded (match can live past code point 200): `contains`,
// `does not contain`, `ends with`, and the FTS `matches` operator (which also
// needs the events_full-only index).
@@ -773,6 +806,8 @@ const EVENTS_CORE_SAFE_METADATA_OPERATORS = new Set<ClickhouseOperator>([
"<>",
"is null",
"is not null",
+ "is set",
+ "is not set",
]);
// Count Unicode code points to mirror ClickHouse leftUTF8, which truncates by
diff --git a/packages/shared/src/server/services/DashboardService/types.ts b/packages/shared/src/server/services/DashboardService/types.ts
index eeb359bb29..3a67454e00 100644
--- a/packages/shared/src/server/services/DashboardService/types.ts
+++ b/packages/shared/src/server/services/DashboardService/types.ts
@@ -1,6 +1,6 @@
import { DashboardWidgetChartType, DashboardWidgetViews } from "@prisma/client";
import { z } from "zod";
-import { singleFilter } from "../../../";
+import { singleFilterList } from "../../../";
import {
persistedWidgetViewToQueryView,
type views,
@@ -140,7 +140,9 @@ export const DashboardDomainSchema = z.object({
name: z.string(),
description: z.string(),
definition: DashboardDefinitionSchema,
- filters: z.array(singleFilter).default([]),
+ // Persisted filters may predate the `is set` operator and use the legacy
+ // metadata `contains ""` key-presence idiom; coerce it before validation.
+ filters: singleFilterList.default([]),
owner: OwnerEnum,
});
@@ -163,7 +165,7 @@ export const WidgetDomainSchema = z.object({
view: z.enum(DashboardWidgetViews),
dimensions: z.array(DimensionSchema),
metrics: z.array(MetricSchema),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
chartType: z.enum(DashboardWidgetChartType),
chartConfig: ChartConfigSchema,
// Lowest query-engine version required by the persisted widget definition.
@@ -178,7 +180,7 @@ export const CreateWidgetInputSchema = z.object({
view: z.enum(DashboardWidgetViews),
dimensions: z.array(DimensionSchema),
metrics: z.array(MetricSchema),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
chartType: z.enum(DashboardWidgetChartType),
chartConfig: ChartConfigSchema,
});
diff --git a/packages/shared/src/server/services/InMemoryFilterService.ts b/packages/shared/src/server/services/InMemoryFilterService.ts
index 0c8dac0437..b89b20d908 100644
--- a/packages/shared/src/server/services/InMemoryFilterService.ts
+++ b/packages/shared/src/server/services/InMemoryFilterService.ts
@@ -338,23 +338,35 @@ export class InMemoryFilterService {
filterValue: string,
operator: string,
): boolean {
- if (!fieldValue || typeof fieldValue !== "object") {
- return false;
- }
-
- // Type assertion is safe here since we've checked typeof fieldValue === "object" above.
// Use hasOwnProperty rather than bracket-access-is-undefined: a key that
// collides with an Object.prototype name (e.g. "toString", "constructor")
// would otherwise resolve the inherited property instead of undefined,
// silently bypassing this guard.
- const record = fieldValue as Record<string, unknown>;
- if (!Object.prototype.hasOwnProperty.call(record, key)) {
+ const hasKey =
+ !!fieldValue &&
+ typeof fieldValue === "object" &&
+ Object.prototype.hasOwnProperty.call(
+ fieldValue as Record<string, unknown>,
+ key,
+ );
+
+ // Presence operators only care whether the key exists, mirroring the
+ // ClickHouse `has(names, k)` / `mapContains(column, k)` conditions.
+ if (operator === "is set") {
+ return hasKey;
+ }
+ if (operator === "is not set") {
+ return !hasKey;
+ }
+
+ if (!hasKey) {
// The key does not exist on the object. Coalescing this to an empty
// string below would make e.g. `contains ""` incorrectly match rows
- // that never had the key. Mirror the ClickHouse `mapContains` guard
- // (PR #13369) and require the key to exist for every operator.
+ // that never had the key, so require the key to exist for every value
+ // operator (matching the ClickHouse `mapContains` / `has` guard).
return false;
}
+ const record = fieldValue as Record<string, unknown>;
const objectValue = record[key];
// Mirror the ClickHouse-side representation exactly: ingestion stores a
// metadata value as `typeof value === "string" ? value : JSON.stringify(value)`
diff --git a/packages/shared/src/server/services/TableViewService/TableViewService.ts b/packages/shared/src/server/services/TableViewService/TableViewService.ts
index ed6344f473..550727922f 100644
--- a/packages/shared/src/server/services/TableViewService/TableViewService.ts
+++ b/packages/shared/src/server/services/TableViewService/TableViewService.ts
@@ -1,5 +1,6 @@
import { Prisma } from "@prisma/client";
import { prisma } from "../../../db";
+import { coerceLegacyEmptyMetadataFilters } from "../../../interfaces/filters";
import {
TableViewPresetTableName,
type TableViewPresetDomain,
@@ -245,7 +246,12 @@ export class TableViewService {
const presets = TableViewPresetsNamesCreatorListSchema.parse([
...systemPresets,
- ...records,
+ // Persisted presets may use the legacy metadata `contains ""` key-presence
+ // idiom that the value guard now rejects; coerce it to `is set` on read.
+ ...records.map((record) => ({
+ ...record,
+ filters: coerceLegacyEmptyMetadataFilters(record.filters),
+ })),
]);
if (tableName === TableViewPresetTableName.ObservationsEvents) {
@@ -329,7 +335,10 @@ export class TableViewService {
);
}
- return tableViewPresets as unknown as TableViewPresetDomain;
+ return {
+ ...tableViewPresets,
+ filters: coerceLegacyEmptyMetadataFilters(tableViewPresets.filters),
+ } as unknown as TableViewPresetDomain;
}
/**
diff --git a/packages/shared/src/server/services/TableViewService/types.ts b/packages/shared/src/server/services/TableViewService/types.ts
index 98e35b411e..e9382117c5 100644
--- a/packages/shared/src/server/services/TableViewService/types.ts
+++ b/packages/shared/src/server/services/TableViewService/types.ts
@@ -1,7 +1,7 @@
import z from "zod";
import {
orderBy,
- singleFilter,
+ singleFilterList,
SystemTableViewPresetCategory,
TableViewPresetTableName,
} from "../../..";
@@ -10,7 +10,7 @@ export const CreateTableViewPresetsInput = z.object({
projectId: z.string(),
name: z.string().min(1, "View name is required"),
tableName: z.enum(TableViewPresetTableName),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
columnOrder: z.array(z.string()),
columnVisibility: z.record(z.string(), z.boolean()),
searchQuery: z.string().optional(),
@@ -55,7 +55,7 @@ export const TableViewPresetsNamesCreatorListSchema = z.array(
name: z.string().nullish(),
})
.nullish(),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
columnOrder: z.array(z.string()),
columnVisibility: z.record(z.string(), z.boolean()),
searchQuery: z.string().nullish(),
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index fa22d16a82..812d7cd65e 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -3252,7 +3252,7 @@ paths:
// - categoryOptions: "any of", "none of"
// - arrayOptions: "any of", "none of", "all of"
// - number: "=", ">", "<", ">=", "<="
- // - stringObject: "=", "contains", "does not contain", "starts with", "ends with"
+ // - stringObject: "=", "contains", "does not contain", "starts with", "ends with", "is set", "is not set"
// - numberObject: "=", ">", "<", ">=", "<="
// - boolean: "=", "<>"
// - null: "is null", "is not null"
@@ -4573,7 +4573,7 @@ paths:
// - categoryOptions: "any of", "none of"
// - arrayOptions: "any of", "none of", "all of"
// - number: "=", ">", "<", ">=", "<="
- // - stringObject: "=", "contains", "does not contain", "starts with", "ends with", "matches"
+ // - stringObject: "=", "contains", "does not contain", "starts with", "ends with", "matches", "is set", "is not set"
// - numberObject: "=", ">", "<", ">=", "<="
// - boolean: "=", "<>"
// - null: "is null", "is not null"
@@ -7001,7 +7001,9 @@ paths:
Supported types: stringObject (metadata key-value filtering),
string, number, datetime, stringOptions, arrayOptions. Supported
operators for stringObject: =, contains, does not contain, starts
- with, ends with.
+ with, ends with, is set, is not set. Use is set / is not set to
+ filter on metadata key presence. An empty value for contains, starts
+ with, or ends with is treated as is set (key presence).
required: false
schema:
type: string
@@ -7547,7 +7549,7 @@ paths:
// - categoryOptions: "any of", "none of"
// - arrayOptions: "any of", "none of", "all of"
// - number: "=", ">", "<", ">=", "<="
- // - stringObject: "=", "contains", "does not contain", "starts with", "ends with"
+ // - stringObject: "=", "contains", "does not contain", "starts with", "ends with", "is set", "is not set"
// - numberObject: "=", ">", "<", ">=", "<="
// - booleanObject: "=", "<>"
// - boolean: "=", "<>"
@@ -12234,7 +12236,10 @@ components:
- `arrayOptions`: `any of`, `none of`, `all of`
- - `stringObject`: same operators as `string`
+ - `stringObject`: `"="`, `contains`, `does not contain`, `starts with`,
+ `ends with`, `is set`, `is not set` (use `is set` / `is not set` for key
+ presence; an empty value for `contains`, `starts with`, or `ends with`
+ is treated as `is set`)
- `boolean`: `"="`, `"<>"`
diff --git a/web/src/__tests__/server/evaluation-rule-v2-repository.servertest.ts b/web/src/__tests__/server/evaluation-rule-v2-repository.servertest.ts
index 50593135ba..13721e1928 100644
--- a/web/src/__tests__/server/evaluation-rule-v2-repository.servertest.ts
+++ b/web/src/__tests__/server/evaluation-rule-v2-repository.servertest.ts
@@ -369,6 +369,44 @@ describe("evaluation rule v2 repository", () => {
});
});
+ describe("findActiveRuleWithMatchingFilterAndSampling", () => {
+ it("dedups a legacy empty-substring metadata filter against a coerced is-set candidate", async () => {
+ const evaluator = await createEvaluator();
+ await createRule({
+ evaluatorId: evaluator.id,
+ filter: [
+ {
+ type: "stringObject",
+ column: "metadata",
+ key: "user_id",
+ operator: "contains",
+ value: "",
+ },
+ ] as FilterState,
+ });
+
+ const candidate: FilterState = [
+ {
+ type: "stringObject",
+ column: "metadata",
+ key: "user_id",
+ operator: "is set",
+ value: "",
+ },
+ ];
+
+ const match =
+ await ruleRepository.findActiveRuleWithMatchingFilterAndSampling({
+ prisma,
+ projectId,
+ filter: candidate,
+ sampling: 1,
+ });
+
+ expect(match).not.toBeNull();
+ });
+ });
+
describe("createRule", () => {
it("persists fixed observation-rule fields and assignments", async () => {
const evaluator = await createEvaluator();
diff --git a/web/src/__tests__/server/mcp-tools-read.servertest.ts b/web/src/__tests__/server/mcp-tools-read.servertest.ts
index 8e3ce51998..c16a6f000b 100644
--- a/web/src/__tests__/server/mcp-tools-read.servertest.ts
+++ b/web/src/__tests__/server/mcp-tools-read.servertest.ts
@@ -2332,6 +2332,8 @@ describe("MCP Read Tools", () => {
"does not contain",
"starts with",
"ends with",
+ "is set",
+ "is not set",
],
requiresKey: true,
},
diff --git a/web/src/components/table/key-value-filter-builder.tsx b/web/src/components/table/key-value-filter-builder.tsx
index 00abe3f63d..b49bdedeed 100644
--- a/web/src/components/table/key-value-filter-builder.tsx
+++ b/web/src/components/table/key-value-filter-builder.tsx
@@ -24,6 +24,7 @@ import {
} from "@/src/components/ui/input-command";
import { MultiSelect } from "@/src/features/filters/components/multi-select";
import { rankFacetOptions } from "@/src/features/filters/lib/facet-display";
+import { isStringPresenceOperator } from "@/src/features/filters/lib/sidebar-filter-actions";
import { Plus, X, Check, ChevronDown } from "lucide-react";
import { cn } from "@/src/utils/tailwind";
import { ScoreTag } from "@/src/components/score-tag";
@@ -93,8 +94,12 @@ const STRING_OPERATOR_LABELS = {
"=": "equals",
contains: "contains",
"does not contain": "does not contain",
+ "is set": "is set",
+ "is not set": "is not set",
} as const;
+type StringOperator = keyof typeof STRING_OPERATOR_LABELS;
+
const BOOLEAN_OPERATOR_LABELS = {
"=": "equals",
"<>": "does not equal",
@@ -282,11 +287,14 @@ export function KeyValueFilterBuilder(props: KeyValueFilterBuilderProps) {
const isComplete = (filter: KeyedFilterEntry) =>
!!filter.key &&
- (Array.isArray(filter.value)
- ? filter.value.length > 0
- : typeof filter.value === "string"
- ? filter.value.trim() !== ""
- : true);
+ // Presence rows (`is set` / `is not set`) need no value to be applied.
+ (isStringPresenceOperator(filter.operator)
+ ? true
+ : Array.isArray(filter.value)
+ ? filter.value.length > 0
+ : typeof filter.value === "string"
+ ? filter.value.trim() !== ""
+ : true);
const updateFilters = (filters: KeyedFilterEntry[]) => {
setDraftFilters(
@@ -591,11 +599,17 @@ export function KeyValueFilterBuilder(props: KeyValueFilterBuilderProps) {
{/* String operator select */}
<Select
value={filter.operator}
- onValueChange={(value) =>
+ onValueChange={(value) => {
+ const operator = value as StringOperator;
handleFilterChange(index, {
- operator: value as "=" | "contains" | "does not contain",
- })
- }
+ operator,
+ // Presence operators carry no value; clear it so no
+ // stale string is persisted alongside `is set`.
+ ...(isStringPresenceOperator(operator)
+ ? { value: "" }
+ : {}),
+ });
+ }}
>
<SelectTrigger>
<SelectValue />
@@ -611,16 +625,18 @@ export function KeyValueFilterBuilder(props: KeyValueFilterBuilderProps) {
</SelectContent>
</Select>
- {/* String value input, with observed-value suggestions */}
- <SuggestingInput
- value={filter.value as string}
- onChange={(value) => handleFilterChange(index, { value })}
- suggestions={
- filter.key ? (valueOptions?.[filter.key] ?? []) : []
- }
- placeholder="Value"
- disabled={!filter.key}
- />
+ {/* String value input, hidden for value-less presence operators */}
+ {isStringPresenceOperator(filter.operator) ? null : (
+ <SuggestingInput
+ value={filter.value as string}
+ onChange={(value) => handleFilterChange(index, { value })}
+ suggestions={
+ filter.key ? (valueOptions?.[filter.key] ?? []) : []
+ }
+ placeholder="Value"
+ disabled={!filter.key}
+ />
+ )}
</>
)}
</div>
diff --git a/web/src/features/automations/server/router.ts b/web/src/features/automations/server/router.ts
index b5ef334c60..00ec091a5c 100644
--- a/web/src/features/automations/server/router.ts
+++ b/web/src/features/automations/server/router.ts
@@ -7,7 +7,7 @@ import {
ActionCreateSchema,
ActionType,
JobConfigState,
- singleFilter,
+ singleFilterList,
isWebhookActionConfig,
TriggerEventSource,
TriggerEventSourceSchema,
@@ -34,7 +34,7 @@ const CreateAutomationInputSchema = z.object({
name: z.string().min(1, "Name is required"),
eventSource: z.string(),
eventAction: z.array(z.string()),
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
status: z.enum(JobConfigState).default(JobConfigState.ACTIVE),
// Action fields
actionType: z.enum(ActionType),
diff --git a/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.ts b/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.ts
index 2c9248e9e5..110f1e7e97 100644
--- a/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.ts
+++ b/web/src/features/dashboard/lib/dashboardUiTableToViewMapping.ts
@@ -1,8 +1,8 @@
-import { z } from "zod";
-import { singleFilter } from "@langfuse/shared";
+import type { z } from "zod";
+import { singleFilterList } from "@langfuse/shared";
import { type views } from "@langfuse/shared/query";
-export const FilterArray = z.array(singleFilter);
+export const FilterArray = singleFilterList;
/**
* Central compatibility layer for dashboard/widget filter columns.
diff --git a/web/src/features/dashboard/server/dashboard-router.ts b/web/src/features/dashboard/server/dashboard-router.ts
index aa4d908aba..c34a9d996e 100644
--- a/web/src/features/dashboard/server/dashboard-router.ts
+++ b/web/src/features/dashboard/server/dashboard-router.ts
@@ -34,7 +34,7 @@ import {
orderBy,
StringNoHTML,
InvalidRequestError,
- singleFilter,
+ singleFilterList,
LANGFUSE_HOME_DASHBOARD_ID,
type FilterState,
} from "@langfuse/shared";
@@ -100,7 +100,7 @@ const SetHomeDashboardInput = z.object({
const UpdateDashboardFiltersInput = z.object({
projectId: z.string(),
dashboardId: z.string(),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
});
/**
diff --git a/web/src/features/datasets/server/dataset-router.ts b/web/src/features/datasets/server/dataset-router.ts
index 6daa3168c3..762a4bac43 100644
--- a/web/src/features/datasets/server/dataset-router.ts
+++ b/web/src/features/datasets/server/dataset-router.ts
@@ -15,7 +15,7 @@ import {
import { MediaContentType } from "@/src/features/media/validation";
import {
paginationZod,
- singleFilter,
+ singleFilterList,
StringNoHTML,
StringNoHTMLNonEmpty,
type FilterState,
@@ -542,7 +542,7 @@ export const datasetRouter = createTRPCRouter({
.input(
z.object({
projectId: z.string(), // Required for protectedProjectProcedure
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
}),
)
.query(async ({ input, ctx }) => {
@@ -1108,7 +1108,7 @@ export const datasetRouter = createTRPCRouter({
z.object({
projectId: z.string(),
datasetId: z.string(),
- filter: z.array(singleFilter).nullish(),
+ filter: singleFilterList.nullish(),
searchQuery: z.string().optional(),
searchType: z.array(TracingSearchType).optional(),
version: z.date().optional(),
@@ -1851,7 +1851,7 @@ export const datasetRouter = createTRPCRouter({
datasetId: z.string(),
datasetRunId: z.string(),
datasetItemIds: z.array(z.string()).optional(),
- filter: z.array(singleFilter),
+ filter: singleFilterList,
...optionalPaginationZod,
}),
)
@@ -1950,9 +1950,7 @@ export const datasetRouter = createTRPCRouter({
datasetId: z.string(),
runIds: z.array(z.string()),
filterByRun: z
- .array(
- z.object({ runId: z.string(), filters: z.array(singleFilter) }),
- )
+ .array(z.object({ runId: z.string(), filters: singleFilterList }))
.nullish(),
...paginationZod,
}),
@@ -2026,9 +2024,7 @@ export const datasetRouter = createTRPCRouter({
datasetId: z.string(),
runIds: z.array(z.string()),
filterByRun: z
- .array(
- z.object({ runId: z.string(), filters: z.array(singleFilter) }),
- )
+ .array(z.object({ runId: z.string(), filters: singleFilterList }))
.nullish(),
}),
)
diff --git a/web/src/features/datasets/server/service.ts b/web/src/features/datasets/server/service.ts
index 5d4cd373c9..c3ea703106 100644
--- a/web/src/features/datasets/server/service.ts
+++ b/web/src/features/datasets/server/service.ts
@@ -5,7 +5,7 @@ import {
type FilterState,
type DatasetItem,
type TracingSearchType,
- singleFilter,
+ singleFilterList,
type DatasetRunItemDomain,
AGGREGATABLE_SCORE_TYPES,
} from "@langfuse/shared";
@@ -27,7 +27,7 @@ import { calculateRecursiveMetricsForRunItems } from "./utils";
export const datasetRunsTableSchema = z.object({
projectId: z.string(),
datasetId: z.string(),
- filter: z.array(singleFilter),
+ filter: singleFilterList,
...optionalPaginationZod,
});
@@ -35,7 +35,7 @@ export const datasetRunTableMetricsSchema = z.object({
projectId: z.string(),
datasetId: z.string(),
runIds: z.array(z.string()),
- filter: z.array(singleFilter),
+ filter: singleFilterList,
});
export type DatasetRunItemsTableInput = {
diff --git a/web/src/features/evals/components/inner-evaluator-form.tsx b/web/src/features/evals/components/inner-evaluator-form.tsx
index 33531372d0..28b52a0231 100644
--- a/web/src/features/evals/components/inner-evaluator-form.tsx
+++ b/web/src/features/evals/components/inner-evaluator-form.tsx
@@ -16,7 +16,7 @@ import { Tabs } from "@/src/components/design-system/Tabs/Tabs";
import { Badge } from "@/src/components/ui/badge";
import {
tracesTableColsWithOptions,
- singleFilter,
+ singleFilterList,
availableTraceEvalVariables,
datasetFormFilterColsWithOptions,
observationEvalFilterColsWithOptions,
@@ -138,7 +138,7 @@ const TracesPreview = memo(
filterState,
}: {
projectId: string;
- filterState: z.infer<typeof singleFilter>[];
+ filterState: z.infer<typeof singleFilterList>[number][];
}) => {
const dateRange = useMemo(() => {
return {
@@ -188,7 +188,7 @@ const ObservationsPreview = memo(
compatibilityCheckWasPerformed,
}: {
projectId: string;
- filterState: z.infer<typeof singleFilter>[];
+ filterState: z.infer<typeof singleFilterList>[number][];
isNewCompatible: boolean;
compatibilityCheckWasPerformed: boolean;
}) => {
@@ -322,7 +322,7 @@ function CodeEvalSourceLink({
);
}
-const EMPTY_FILTER_STATE: z.infer<typeof singleFilter>[] = [];
+const EMPTY_FILTER_STATE: z.infer<typeof singleFilterList>[number][] = [];
export const InnerEvaluatorForm = (props: {
projectId: string;
@@ -454,7 +454,7 @@ export const InnerEvaluatorForm = (props: {
props.existingEvaluator?.scoreName ?? `${props.evalTemplate.name}`,
target: defaultTarget,
filter: props.existingEvaluator?.filter
- ? z.array(singleFilter).parse(props.existingEvaluator.filter)
+ ? singleFilterList.parse(props.existingEvaluator.filter)
: defaultTarget === EvalTargetObject.TRACE
? // For new trace evaluators, exclude internal environments by default
DEFAULT_TRACE_FILTER
@@ -603,7 +603,7 @@ export const InnerEvaluatorForm = (props: {
values = props.preprocessFormValues(values);
}
- const validatedFilter = z.array(singleFilter).safeParse(values.filter);
+ const validatedFilter = singleFilterList.safeParse(values.filter);
if (
props.existingEvaluator?.timeScope.includes("EXISTING") &&
@@ -1222,7 +1222,9 @@ export const InnerEvaluatorForm = (props: {
columns={getFilterColumns()}
filterState={field.value ?? []}
onChange={(
- value: z.infer<typeof singleFilter>[],
+ value: z.infer<
+ typeof singleFilterList
+ >[number][],
) => {
field.onChange(value);
if (router.query.traceId) {
diff --git a/web/src/features/evals/hooks/useEvaluatorTableData.ts b/web/src/features/evals/hooks/useEvaluatorTableData.ts
index 075fef26f0..24def20ef0 100644
--- a/web/src/features/evals/hooks/useEvaluatorTableData.ts
+++ b/web/src/features/evals/hooks/useEvaluatorTableData.ts
@@ -1,9 +1,8 @@
import { useMemo } from "react";
-import { z } from "zod";
import {
type EvaluatorBlockReason,
type FilterState,
- singleFilter,
+ singleFilterList,
type OrderByState,
} from "@langfuse/shared";
import { api } from "@/src/utils/api";
@@ -122,7 +121,7 @@ export const useEvaluatorTableData = ({
blockReason: jobConfig.blockReason,
scoreName: jobConfig.scoreName,
target: jobConfig.targetObject,
- filter: z.array(singleFilter).parse(jobConfig.filter),
+ filter: singleFilterList.parse(jobConfig.filter),
result: generateJobExecutionCounts(executionCounts),
isCostLoading: !costs.data,
isResultLoading:
diff --git a/web/src/features/evals/server/evalConfigState.ts b/web/src/features/evals/server/evalConfigState.ts
index b934f75d75..1638a2ff8a 100644
--- a/web/src/features/evals/server/evalConfigState.ts
+++ b/web/src/features/evals/server/evalConfigState.ts
@@ -1,8 +1,7 @@
-import { z } from "zod";
import {
type JobConfiguration,
JobConfigState,
- singleFilter,
+ singleFilterList,
} from "@langfuse/shared";
export const resetEvalConfigBlockFields = {
@@ -29,7 +28,7 @@ const evaluatorTargetsDataset = ({
}: Pick<JobConfiguration, "filter"> & {
datasetId: string;
}) => {
- const parsedFilter = z.array(singleFilter).safeParse(filter);
+ const parsedFilter = singleFilterList.safeParse(filter);
if (!parsedFilter.success) {
return false;
diff --git a/web/src/features/evals/server/router.ts b/web/src/features/evals/server/router.ts
index 75f7c22e3c..8d26051277 100644
--- a/web/src/features/evals/server/router.ts
+++ b/web/src/features/evals/server/router.ts
@@ -9,7 +9,8 @@ import { auditLog } from "@/src/features/audit-logs/server";
import {
DEFAULT_TRACE_JOB_DELAY,
deriveEvaluatorDisplayStateFromExecutionCounts,
- singleFilter,
+ type singleFilter,
+ singleFilterList,
variableMapping,
observationVariableMapping,
paginationZod,
@@ -90,7 +91,7 @@ const CreateEvalJobSchema = z.object({
evalTemplateId: z.string(),
scoreName: z.string().min(1),
target: EvalTargetObjectSchema,
- filter: z.array(singleFilter).nullable(), // reusing the filter type from the tables
+ filter: singleFilterList.nullable(),
// Accept either full variableMapping (trace/dataset) or simplified observationVariableMapping (event/experiment)
mapping: z.union([
z.array(variableMapping),
@@ -217,7 +218,7 @@ const assertTemplateCanRunForActivation = async (params: {
const UpdateEvalJobSchema = z.object({
scoreName: z.string().min(1).optional(),
- filter: z.array(singleFilter).optional(),
+ filter: singleFilterList.optional(),
// Accept either full variableMapping (trace/dataset) or simplified observationVariableMapping (event/experiment)
variableMapping: z
.union([z.array(variableMapping), z.array(observationVariableMapping)])
@@ -309,7 +310,7 @@ export const evalRouter = createTRPCRouter({
.input(
z.object({
projectId: z.string(), // Required for protectedProjectProcedure
- filter: z.array(singleFilter),
+ filter: singleFilterList,
orderBy: orderBy,
searchQuery: z.string().nullish(),
...paginationZod,
@@ -1171,7 +1172,7 @@ export const evalRouter = createTRPCRouter({
.input(
z.object({
projectId: z.string(),
- filter: z.array(singleFilter),
+ filter: singleFilterList,
jobConfigurationId: z.string().optional(),
...paginationZod,
}),
diff --git a/web/src/features/evals/utils/evaluator-form-utils.ts b/web/src/features/evals/utils/evaluator-form-utils.ts
index 1fcbd29918..43685ab2ef 100644
--- a/web/src/features/evals/utils/evaluator-form-utils.ts
+++ b/web/src/features/evals/utils/evaluator-form-utils.ts
@@ -1,7 +1,7 @@
import { z } from "zod";
import {
EvalTargetObjectSchema,
- singleFilter,
+ singleFilterList,
type langfuseObjects,
TimeScopeSchema,
wipVariableMapping,
@@ -15,7 +15,7 @@ export const evalConfigFormSchema = z
.object({
scoreName: z.string(),
target: EvalTargetObjectSchema,
- filter: z.array(singleFilter).nullable(), // reusing the filter type from the tables
+ filter: singleFilterList.nullable(),
mapping: z.array(wipVariableMapping),
sampling: z.coerce.number().gt(0).lte(1),
delay: z.coerce.number().min(0).optional().default(10),
diff --git a/web/src/features/evals/v2/components/Rules/EditRuleDialog/components/EditRuleDialogContent/EditRuleDialogContent.tsx b/web/src/features/evals/v2/components/Rules/EditRuleDialog/components/EditRuleDialogContent/EditRuleDialogContent.tsx
index 5f8204f9f9..c70fb928c9 100644
--- a/web/src/features/evals/v2/components/Rules/EditRuleDialog/components/EditRuleDialogContent/EditRuleDialogContent.tsx
+++ b/web/src/features/evals/v2/components/Rules/EditRuleDialog/components/EditRuleDialogContent/EditRuleDialogContent.tsx
@@ -2,7 +2,7 @@ import { showSuccessToast } from "@/src/features/notifications";
import {
EvalTemplateType,
observationVariableMappingList,
- singleFilter,
+ singleFilterList,
} from "@langfuse/shared";
import { useState } from "react";
import { DialogBody } from "@/src/components/ui/dialog";
@@ -40,7 +40,7 @@ export function EditRuleDialogContent({
const [ruleSetupStore] = useState(() =>
createRuleSetupStore({
name: rule.name,
- filter: singleFilter.array().catch([]).parse(rule.filter),
+ filter: singleFilterList.catch([]).parse(rule.filter),
sampling: rule.sampling,
assignments: rule.assignments.map((assignment) => {
const preparedDefault = prepareModernRuleVariableMapping(
diff --git a/web/src/features/evals/v2/server/evaluators/evaluatorTypes.ts b/web/src/features/evals/v2/server/evaluators/evaluatorTypes.ts
index c41c71492e..16b502b201 100644
--- a/web/src/features/evals/v2/server/evaluators/evaluatorTypes.ts
+++ b/web/src/features/evals/v2/server/evaluators/evaluatorTypes.ts
@@ -9,7 +9,7 @@ import {
ZodModelConfig,
jsonSchema,
paginationLimitZod,
- singleFilter,
+ singleFilterList,
type ObservationVariableMapping,
type PersistedEvaluatorPromptMessages,
} from "@langfuse/shared";
@@ -141,7 +141,7 @@ export const EvaluatorIdsSchema = z.object({
});
export const ActivationCostEstimatesSchema = EvaluatorIdsSchema.extend({
- filter: z.array(singleFilter),
+ filter: singleFilterList,
sampling: z.number().min(0).max(1),
shouldRunMissingTest: z.boolean().optional().default(true),
knownTestRunCostUsd: z.number().nonnegative().optional(),
@@ -168,8 +168,7 @@ export const ActivationCostEstimatesSchema = EvaluatorIdsSchema.extend({
{ message: "Evaluator IDs must be unique", path: ["evaluatorIds"] },
);
-const EvaluatorListFilterSchema = z
- .array(singleFilter)
+const EvaluatorListFilterSchema = singleFilterList
.superRefine((filters, ctx) => {
for (const [index, filter] of filters.entries()) {
const valid =
diff --git a/web/src/features/evals/v2/server/rules/ruleRepository.ts b/web/src/features/evals/v2/server/rules/ruleRepository.ts
index e94611d8f1..ac4e5c6804 100644
--- a/web/src/features/evals/v2/server/rules/ruleRepository.ts
+++ b/web/src/features/evals/v2/server/rules/ruleRepository.ts
@@ -4,6 +4,7 @@ import {
type PrismaClient,
} from "@langfuse/shared/src/db";
import {
+ coerceLegacyEmptyMetadataFilters,
EvalTargetObject,
type FilterState,
type ObservationVariableMapping,
@@ -294,7 +295,10 @@ export async function findActiveRuleWithMatchingFilterAndSampling(params: {
return (
rules.find((rule) =>
- filtersMatch(rule.filter as FilterState, params.filter),
+ filtersMatch(
+ coerceLegacyEmptyMetadataFilters(rule.filter) as FilterState,
+ params.filter,
+ ),
) ?? null
);
}
diff --git a/web/src/features/evals/v2/server/rules/ruleTypes.ts b/web/src/features/evals/v2/server/rules/ruleTypes.ts
index c11860b057..8d9d74788d 100644
--- a/web/src/features/evals/v2/server/rules/ruleTypes.ts
+++ b/web/src/features/evals/v2/server/rules/ruleTypes.ts
@@ -2,13 +2,13 @@ import {
EvalTargetObject,
observationVariableMappingList,
paginationLimitZod,
- singleFilter,
+ singleFilterList,
} from "@langfuse/shared";
import { z } from "zod";
export const RuleMetadataSchema = z.object({
name: z.string().trim().min(1),
- filter: z.array(singleFilter),
+ filter: singleFilterList,
sampling: z.number().min(0).max(1),
});
@@ -44,8 +44,7 @@ export const ListRulesSchema = z.object({
.min(1)
.max(2)
.optional(),
- filter: z
- .array(singleFilter)
+ filter: singleFilterList
.superRefine((filters, ctx) => {
for (const [index, filter] of filters.entries()) {
const valid =
diff --git a/web/src/features/events/server/eventsRouter.ts b/web/src/features/events/server/eventsRouter.ts
index 952bc84100..7e4b770a3b 100644
--- a/web/src/features/events/server/eventsRouter.ts
+++ b/web/src/features/events/server/eventsRouter.ts
@@ -9,7 +9,7 @@ import {
type OrderByState,
normalizeOrderByForTable,
paginationZod,
- singleFilter,
+ singleFilterList,
timeFilter,
} from "@langfuse/shared";
import {
@@ -74,7 +74,7 @@ export type GetAllEventsInput = z.infer<typeof GetAllEventsInput>;
const GetEventFilterOptionsInput = zodSchema.object({
projectId: zodSchema.string(),
- filter: zodSchema.array(singleFilter).optional(),
+ filter: singleFilterList.optional(),
startTimeFilter: zodSchema.array(timeFilter).optional(),
isRootObservation: zodSchema.boolean().optional(),
hasParentObservation: zodSchema.boolean().optional(),
diff --git a/web/src/features/events/server/types.ts b/web/src/features/events/server/types.ts
index 1ba8d4877c..75b89435b7 100644
--- a/web/src/features/events/server/types.ts
+++ b/web/src/features/events/server/types.ts
@@ -1,7 +1,7 @@
import { z } from "zod";
import {
hasValidTracingSearchTypes,
- singleFilter,
+ singleFilterList,
TRACING_SEARCH_TYPE_REQUIRED_MESSAGE,
TracingSearchType,
orderBy,
@@ -9,7 +9,7 @@ import {
const eventsTableOptionsShape = {
projectId: z.string(), // Required for protectedProjectProcedure
- filter: z.array(singleFilter),
+ filter: singleFilterList,
searchQuery: z.string().nullable(),
searchType: z.array(TracingSearchType),
};
diff --git a/web/src/features/experiments/hooks/useExperimentEvaluatorSelection.ts b/web/src/features/experiments/hooks/useExperimentEvaluatorSelection.ts
index 5be753938c..3fefdb9bde 100644
--- a/web/src/features/experiments/hooks/useExperimentEvaluatorSelection.ts
+++ b/web/src/features/experiments/hooks/useExperimentEvaluatorSelection.ts
@@ -1,8 +1,7 @@
import { api, type RouterOutputs } from "@/src/utils/api";
import { useMemo } from "react";
-import { isEvalRuleExecutable, singleFilter } from "@langfuse/shared";
+import { isEvalRuleExecutable, singleFilterList } from "@langfuse/shared";
import { getEvalTemplateFamilyKey } from "@/src/features/evals/utils/eval-template-family";
-import { z } from "zod";
/**
* One entry per evaluator family that already has a job config for this
@@ -24,7 +23,7 @@ export const getExistingEvaluators = (
> = {};
for (const jobConfig of jobConfigs ?? []) {
- const parsedFilter = z.array(singleFilter).safeParse(jobConfig.filter);
+ const parsedFilter = singleFilterList.safeParse(jobConfig.filter);
if (!parsedFilter.success) continue;
const matchesDataset =
parsedFilter.data.length === 0 ||
diff --git a/web/src/features/experiments/server/router.ts b/web/src/features/experiments/server/router.ts
index 884ce0fa72..22c1a97179 100644
--- a/web/src/features/experiments/server/router.ts
+++ b/web/src/features/experiments/server/router.ts
@@ -43,7 +43,7 @@ import {
PromptType,
extractPlaceholderNames,
type PromptMessage,
- singleFilter,
+ singleFilterList,
type FilterState,
orderBy,
paginationZod,
@@ -61,7 +61,7 @@ import { describeVariableMismatch } from "@/src/features/experiments/fns/describ
const ExperimentFilterOptions = z.object({
projectId: z.string(),
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
orderBy: orderBy,
...paginationZod,
});
@@ -338,7 +338,7 @@ export const experimentsRouter = createTRPCRouter({
.input(
z.object({
projectId: z.string(),
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
limit: z.number().int().min(1).max(50),
}),
)
@@ -412,7 +412,7 @@ export const experimentsRouter = createTRPCRouter({
.input(
z.object({
projectId: z.string(),
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
}),
)
.query(async ({ input, ctx }) => {
@@ -437,7 +437,7 @@ export const experimentsRouter = createTRPCRouter({
z.object({
projectId: z.string(),
experimentIds: z.array(z.string()),
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
}),
)
.query(async ({ input, ctx }) => {
@@ -633,7 +633,7 @@ export const experimentsRouter = createTRPCRouter({
.array(
z.object({
experimentId: z.string(),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
}),
)
.nullish(),
@@ -772,7 +772,7 @@ export const experimentsRouter = createTRPCRouter({
.array(
z.object({
experimentId: z.string(),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
}),
)
.nullish(),
diff --git a/web/src/features/filters/components/filter-builder.tsx b/web/src/features/filters/components/filter-builder.tsx
index baf44c0cd8..345287d00c 100644
--- a/web/src/features/filters/components/filter-builder.tsx
+++ b/web/src/features/filters/components/filter-builder.tsx
@@ -912,7 +912,10 @@ function FilterBuilderForm({
operator: value as any,
// Value-less operators keep an empty string for schema compatibility.
value:
- filter.type === "null" || value === "is not empty"
+ filter.type === "null" ||
+ value === "is not empty" ||
+ value === "is set" ||
+ value === "is not set"
? ""
: (filter.value as any),
},
@@ -939,7 +942,10 @@ function FilterBuilderForm({
const valueControl = keyPending ? (
<Input disabled />
) : filter.type === "string" &&
- filter.operator === "is not empty" ? null : stringObjectSuggest &&
+ filter.operator === "is not empty" ? null : filter.type ===
+ "stringObject" &&
+ (filter.operator === "is set" ||
+ filter.operator === "is not set") ? null : stringObjectSuggest &&
filter.type === "stringObject" ? (
<SingleSelect
title="Value"
diff --git a/web/src/features/filters/hooks/useSidebarFilterState.tsx b/web/src/features/filters/hooks/useSidebarFilterState.tsx
index e70dcafc24..d8c318494f 100644
--- a/web/src/features/filters/hooks/useSidebarFilterState.tsx
+++ b/web/src/features/filters/hooks/useSidebarFilterState.tsx
@@ -1668,7 +1668,12 @@ export function useSidebarFilterPresentation(
) as Array<{
column: string;
type: "stringObject";
- operator: "=" | "contains" | "does not contain";
+ operator:
+ | "="
+ | "contains"
+ | "does not contain"
+ | "is set"
+ | "is not set";
key: string;
value: string;
}>;
diff --git a/web/src/features/filters/lib/sidebar-filter-actions.clienttest.ts b/web/src/features/filters/lib/sidebar-filter-actions.clienttest.ts
index c1f98bbd3d..e6a489f492 100644
--- a/web/src/features/filters/lib/sidebar-filter-actions.clienttest.ts
+++ b/web/src/features/filters/lib/sidebar-filter-actions.clienttest.ts
@@ -455,6 +455,33 @@ describe("keyed facet transitions", () => {
]);
});
+ it("keeps `is set` / `is not set` presence rows even with an empty value", () => {
+ const next = applyKeyedFilterEntries([], "metadata", {
+ kind: "stringObject",
+ entries: [
+ { key: "env", operator: "is set", value: "" },
+ { key: "region", operator: "is not set", value: "" },
+ { key: "", operator: "is set", value: "" },
+ ],
+ });
+ expect(next).toEqual([
+ {
+ column: "metadata",
+ type: "stringObject",
+ operator: "is set",
+ key: "env",
+ value: "",
+ },
+ {
+ column: "metadata",
+ type: "stringObject",
+ operator: "is not set",
+ key: "region",
+ value: "",
+ },
+ ]);
+ });
+
it("filters numeric and boolean drafts by their empty-value sentinel", () => {
expect(
applyKeyedFilterEntries([], "scores_avg", {
diff --git a/web/src/features/filters/lib/sidebar-filter-actions.ts b/web/src/features/filters/lib/sidebar-filter-actions.ts
index 0b5c99beba..18aa372874 100644
--- a/web/src/features/filters/lib/sidebar-filter-actions.ts
+++ b/web/src/features/filters/lib/sidebar-filter-actions.ts
@@ -63,13 +63,19 @@ export type BooleanKeyValueFilterEntry = {
};
// Represents one active string filter row in the string key-value facet UI
-// Example: key="environment", operator="=", value="production"
+// Example: key="environment", operator="=", value="production". `is set` /
+// `is not set` are value-less key-presence operators (value stays "").
export type StringKeyValueFilterEntry = {
key: string;
- operator: "=" | "contains" | "does not contain";
+ operator: "=" | "contains" | "does not contain" | "is set" | "is not set";
value: string;
};
+// `is set` / `is not set` filter on key presence, so a row is complete without a
+// value — unlike the value operators, whose empty-value rows are still drafts.
+export const isStringPresenceOperator = (operator: string): boolean =>
+ operator === "is set" || operator === "is not set";
+
const toValueList = (options: SidebarFilterOptions[string]): string[] | null =>
Array.isArray(options)
? options.map((opt) => (typeof opt === "string" ? opt : opt.value))
@@ -640,13 +646,19 @@ export function applyKeyedFilterEntries(
return [
...without,
...update.entries
- .filter((entry) => entry.key && entry.value.trim() !== "")
+ .filter(
+ (entry) =>
+ entry.key &&
+ (isStringPresenceOperator(entry.operator) ||
+ entry.value.trim() !== ""),
+ )
.map((entry) => ({
column,
type: "stringObject" as const,
operator: entry.operator,
key: entry.key,
- value: entry.value,
+ // Presence operators carry no value.
+ value: isStringPresenceOperator(entry.operator) ? "" : entry.value,
})),
];
}
diff --git a/web/src/features/mcp/server/observations/tools/listObservations.ts b/web/src/features/mcp/server/observations/tools/listObservations.ts
index 17574c09b9..4e26b7e40a 100644
--- a/web/src/features/mcp/server/observations/tools/listObservations.ts
+++ b/web/src/features/mcp/server/observations/tools/listObservations.ts
@@ -6,6 +6,7 @@ import {
eventsTableStringFilter,
eventsTableStringObjectFilter,
eventsTableCols,
+ coerceLegacyEmptyMetadataFilters,
filterOperators,
FTS_MATCH_OPERATOR,
numberFilter,
@@ -131,11 +132,11 @@ const OBSERVATION_MCP_FILTER_SCHEMA_BY_TYPE = {
column: z.literal(column),
}),
stringObject: (column: string, requireType = false) => {
- const filterSchema = OBSERVATION_MCP_FTS_COLUMNS.has(column)
+ const filterSchemaBase = OBSERVATION_MCP_FTS_COLUMNS.has(column)
? eventsTableStringObjectFilter
: stringObjectFilter;
- return filterSchema.omit({ type: true, column: true }).extend({
+ return filterSchemaBase.omit({ type: true, column: true }).extend({
type: requireType
? z.literal("stringObject")
: z.literal("stringObject").optional(),
@@ -249,11 +250,16 @@ const ObservationMcpFilterSchema = z.preprocess(
const type =
filter.type ?? OBSERVATION_MCP_FILTER_COLUMN_TYPES.get(filter.column);
- return eventsTableSingleFilter.parse(
+ const reshaped =
filter.column === "tags"
? { ...filter, type, column: "traceTags" }
- : { ...filter, type },
- );
+ : { ...filter, type };
+ // Legacy `contains ""` presence spelling → `is set` before validation.
+ const [coerced] = coerceLegacyEmptyMetadataFilters([
+ reshaped,
+ ]) as unknown[];
+
+ return eventsTableSingleFilter.parse(coerced);
}),
);
diff --git a/web/src/features/natural-language-filters/server/utils.ts b/web/src/features/natural-language-filters/server/utils.ts
index e7ddab1026..356f55b3ae 100644
--- a/web/src/features/natural-language-filters/server/utils.ts
+++ b/web/src/features/natural-language-filters/server/utils.ts
@@ -1,11 +1,10 @@
import { Langfuse } from "langfuse";
-import { type FilterCondition, singleFilter } from "@langfuse/shared";
-import { z } from "zod";
+import { type FilterCondition, singleFilterList } from "@langfuse/shared";
import { getProductBaseUrl } from "@/src/utils/base-url";
let langfuseClient: Langfuse | null = null;
-const FilterArraySchema = z.array(singleFilter);
+const FilterArraySchema = singleFilterList;
export function parseFiltersFromCompletion(
completion: string,
diff --git a/web/src/features/prompts/server/routers/promptRouter.ts b/web/src/features/prompts/server/routers/promptRouter.ts
index cc0c8025b7..88b077bcbe 100644
--- a/web/src/features/prompts/server/routers/promptRouter.ts
+++ b/web/src/features/prompts/server/routers/promptRouter.ts
@@ -30,7 +30,7 @@ import {
StringNoHTMLNonEmpty,
TracingSearchType,
orderBy,
- singleFilter,
+ singleFilterList,
normalizeOrderByForTable,
} from "@langfuse/shared";
import {
@@ -63,7 +63,7 @@ const buildPathPrefixFilter = (pathPrefix?: string): Prisma.Sql => {
const PromptFilterOptions = z.object({
projectId: z.string(), // Required for protectedProjectProcedure
- filter: z.array(singleFilter),
+ filter: singleFilterList,
orderBy: orderBy,
...paginationZod,
pathPrefix: z.string().optional(),
@@ -206,7 +206,7 @@ export const promptRouter = createTRPCRouter({
searchQuery: z.string().optional(),
searchType: z.array(TracingSearchType).optional(),
pathPrefix: z.string().optional(),
- filter: z.array(singleFilter).optional(),
+ filter: singleFilterList.optional(),
}),
)
.query(async ({ input, ctx }) => {
diff --git a/web/src/features/public-api/types/metrics.ts b/web/src/features/public-api/types/metrics.ts
index f4e9c97fa2..99eed60464 100644
--- a/web/src/features/public-api/types/metrics.ts
+++ b/web/src/features/public-api/types/metrics.ts
@@ -3,7 +3,7 @@ import {
InvalidRequestError,
paginationMetaResponseZod,
publicApiPaginationZod,
- singleFilter,
+ singleFilterList,
} from "@langfuse/shared";
import { stringDateTime } from "@langfuse/shared/src/server";
import { z } from "zod";
@@ -38,7 +38,7 @@ const MetricsQueryObject = z
view: views,
dimensions: z.array(dimension).optional().default([]),
metrics: z.array(metric),
- filters: z.array(singleFilter).optional().default([]),
+ filters: singleFilterList.optional().default([]),
timeDimension: z
.object({
granularity: publicGranularities,
@@ -106,7 +106,7 @@ export const MetricsQueryObjectV2 = z
view: viewsV2,
dimensions: z.array(dimension).optional().default([]),
metrics: z.array(metric),
- filters: z.array(singleFilter).optional().default([]),
+ filters: singleFilterList.optional().default([]),
timeDimension: z
.object({
granularity: publicGranularities,
diff --git a/web/src/features/public-api/types/observations.ts b/web/src/features/public-api/types/observations.ts
index cbe8083fac..337bbd65a5 100644
--- a/web/src/features/public-api/types/observations.ts
+++ b/web/src/features/public-api/types/observations.ts
@@ -5,12 +5,12 @@ import {
type EventsObservation,
OBSERVATION_FIELD_GROUPS_PUBLIC_API,
ObservationLevel,
- eventsTableSingleFilter,
+ eventsTableSingleFilterList,
optionalCommaSeparatedStringArray,
optionalJsonParam,
paginationMetaResponseZod,
publicApiPaginationZod,
- singleFilter,
+ singleFilterList,
InvalidRequestError,
} from "@langfuse/shared";
import {
@@ -226,7 +226,7 @@ export const GetObservationsV1Query = z.object({
fromStartTime: stringDateTime,
toStartTime: stringDateTime,
useEventsTable: useEventsTableSchema,
- filter: optionalJsonParam(z.array(singleFilter), "filter"),
+ filter: optionalJsonParam(singleFilterList, "filter"),
});
export const GetObservationsV1Response = z
.object({
@@ -346,7 +346,7 @@ export const GetObservationsV2Query = z.object({
environment: z.union([z.array(z.string()), z.string()]).nullish(),
fromStartTime: stringDateTime.optional(),
toStartTime: stringDateTime.optional(),
- filter: optionalJsonParam(z.array(eventsTableSingleFilter), "filter"),
+ filter: optionalJsonParam(eventsTableSingleFilterList, "filter"),
});
/**
diff --git a/web/src/features/public-api/types/traces.ts b/web/src/features/public-api/types/traces.ts
index 2b07748f94..d99f1ddb15 100644
--- a/web/src/features/public-api/types/traces.ts
+++ b/web/src/features/public-api/types/traces.ts
@@ -7,7 +7,7 @@ import {
orderBy,
optionalJsonParam,
publicApiPaginationZod,
- singleFilter,
+ singleFilterList,
} from "@langfuse/shared";
import {
stringDateTime,
@@ -85,7 +85,7 @@ export const GetTracesV1Query = z.object({
unknownValues: "filter",
}).transform((fields) => (fields && fields.length > 0 ? fields : null)),
useEventsTable: useEventsTableSchema,
- filter: optionalJsonParam(z.array(singleFilter), "filter"),
+ filter: optionalJsonParam(singleFilterList, "filter"),
});
export const GetTracesV1Response = z
.object({
diff --git a/web/src/features/public-api/types/unstable-dashboard-widgets.ts b/web/src/features/public-api/types/unstable-dashboard-widgets.ts
index ab42699d49..16cd60c4b1 100644
--- a/web/src/features/public-api/types/unstable-dashboard-widgets.ts
+++ b/web/src/features/public-api/types/unstable-dashboard-widgets.ts
@@ -4,7 +4,7 @@ import {
DashboardWidgetChartType,
DimensionSchema,
MetricSchema,
- singleFilter,
+ singleFilterList,
} from "@langfuse/shared";
import { metricAggregations } from "@langfuse/shared/query";
@@ -47,7 +47,7 @@ const UnstableDashboardWidgetBody = z.object({
view: PostUnstableDashboardWidgetView,
dimensions: z.array(DashboardWidgetDimensionSchema),
metrics: z.array(DashboardWidgetMetricSchema).min(1),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
chartType: z.enum(DashboardWidgetChartType),
chartConfig: DashboardWidgetChartConfigInput.optional(),
});
@@ -67,7 +67,7 @@ const PublicDashboardWidget = z
view: DashboardWidgetViewOutput,
dimensions: z.array(DashboardWidgetDimensionSchema),
metrics: z.array(DashboardWidgetMetricSchema),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
chartType: z.enum(DashboardWidgetChartType),
chartConfig: ChartConfigSchema,
})
diff --git a/web/src/features/public-api/types/unstable-dashboards.ts b/web/src/features/public-api/types/unstable-dashboards.ts
index 954e646eb2..32a779dc88 100644
--- a/web/src/features/public-api/types/unstable-dashboards.ts
+++ b/web/src/features/public-api/types/unstable-dashboards.ts
@@ -1,5 +1,5 @@
import { z } from "zod";
-import { singleFilter } from "@langfuse/shared";
+import { singleFilterList } from "@langfuse/shared";
const pagination = z.object({
page: z.coerce.number().int().positive().default(1),
@@ -54,7 +54,7 @@ export const DashboardSchema = z
name: z.string(),
description: z.string(),
definition: PublicDashboardDefinitionSchema,
- filters: z.array(singleFilter),
+ filters: singleFilterList,
})
.strict();
@@ -73,7 +73,7 @@ export const PostUnstableDashboardBody = z.object({
name: z.string().min(1),
description: z.string().default(""),
definition: PublicDashboardDefinitionSchema.optional(),
- filters: z.array(singleFilter).optional(),
+ filters: singleFilterList.optional(),
});
/** @alias */
export const PostUnstableDashboardResponse = DashboardSchema;
@@ -86,7 +86,7 @@ export const PatchUnstableDashboardBody = z
name: z.string().min(1).optional(),
description: z.string().optional(),
definition: PublicDashboardDefinitionSchema.optional(),
- filters: z.array(singleFilter).optional(),
+ filters: singleFilterList.optional(),
})
.refine(
(value) => Object.keys(value).length > 0,
diff --git a/web/src/features/widgets/components/widgetFormSchema.ts b/web/src/features/widgets/components/widgetFormSchema.ts
index 8100296f9b..beb3d6f8b3 100644
--- a/web/src/features/widgets/components/widgetFormSchema.ts
+++ b/web/src/features/widgets/components/widgetFormSchema.ts
@@ -1,7 +1,7 @@
import { z } from "zod";
import startCase from "lodash/startCase";
-import { singleFilter, type FilterState } from "@langfuse/shared";
+import { singleFilterList, type FilterState } from "@langfuse/shared";
import {
getValidAggregationsForMeasureType,
getWidgetRequiredVersion,
@@ -185,7 +185,7 @@ export function makeWidgetFormSchema(viewVersion: ViewVersion) {
name: z.string().nullable(),
description: z.string().nullable(),
view: views,
- filters: z.array(singleFilter),
+ filters: singleFilterList,
metrics: z.array(MetricFieldSchema).min(1),
dimensions: z.array(z.object({ field: z.string() })),
chart: z.object({
diff --git a/web/src/features/widgets/utils/import-export-utils.ts b/web/src/features/widgets/utils/import-export-utils.ts
index da7a191f5a..4ef802f140 100644
--- a/web/src/features/widgets/utils/import-export-utils.ts
+++ b/web/src/features/widgets/utils/import-export-utils.ts
@@ -17,7 +17,7 @@ import {
ChartConfigSchema,
DimensionSchema,
MetricSchema,
- singleFilter,
+ singleFilterList,
type FilterState,
} from "@langfuse/shared";
import { dashboardWidgetChartTypeSchema } from "@/src/features/widgets/lib/dashboardWidgetChartTypes";
@@ -47,7 +47,7 @@ const widgetImportBaseSchema = z
view: views,
dimensions: z.array(DimensionSchema),
metrics: z.array(widgetMetricSchema),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
chartType: dashboardWidgetChartTypeSchema,
chartConfig: ChartConfigSchema,
minVersion: z.number().int().optional(),
diff --git a/web/src/server/api/routers/dashboardWidgets.ts b/web/src/server/api/routers/dashboardWidgets.ts
index 78a18905e9..a4c6b9e2df 100644
--- a/web/src/server/api/routers/dashboardWidgets.ts
+++ b/web/src/server/api/routers/dashboardWidgets.ts
@@ -5,7 +5,7 @@ import {
} from "@/src/server/api/trpc";
import {
orderBy,
- singleFilter,
+ singleFilterList,
optionalPaginationZod,
LangfuseConflictError,
} from "@langfuse/shared";
@@ -29,7 +29,7 @@ const CreateDashboardWidgetInput = z.object({
view: views,
dimensions: z.array(DimensionSchema),
metrics: z.array(MetricSchema),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
chartType: z.enum(DashboardWidgetChartType),
chartConfig: ChartConfigSchema,
});
@@ -43,7 +43,7 @@ const UpdateDashboardWidgetInput = z.object({
view: views,
dimensions: z.array(DimensionSchema),
metrics: z.array(MetricSchema),
- filters: z.array(singleFilter),
+ filters: singleFilterList,
chartType: z.enum(DashboardWidgetChartType),
chartConfig: ChartConfigSchema,
});
diff --git a/web/src/server/api/routers/generations/utils/GenerationTableOptions.ts b/web/src/server/api/routers/generations/utils/GenerationTableOptions.ts
index 690c62dc6a..29e84b6cf1 100644
--- a/web/src/server/api/routers/generations/utils/GenerationTableOptions.ts
+++ b/web/src/server/api/routers/generations/utils/GenerationTableOptions.ts
@@ -1,7 +1,7 @@
import { z } from "zod";
import {
hasValidTracingSearchTypes,
- singleFilter,
+ singleFilterList,
TRACING_SEARCH_TYPE_REQUIRED_MESSAGE,
TracingSearchType,
orderBy,
@@ -10,7 +10,7 @@ import {
export const GenerationTableOptions = z
.object({
projectId: z.string(), // Required for protectedProjectProcedure
- filter: z.array(singleFilter),
+ filter: singleFilterList,
searchQuery: z.string().nullable(),
searchType: z.array(TracingSearchType),
orderBy: orderBy,
diff --git a/web/src/server/api/routers/scores.ts b/web/src/server/api/routers/scores.ts
index 4c39039d84..1a269f31fa 100644
--- a/web/src/server/api/routers/scores.ts
+++ b/web/src/server/api/routers/scores.ts
@@ -15,7 +15,7 @@ import {
orderBy,
paginationZod,
normalizeOrderByForTable,
- singleFilter,
+ singleFilterList,
timeFilter,
UpdateAnnotationScoreData,
validateDbScore,
@@ -74,7 +74,7 @@ import { toDomainWithStringifiedMetadata } from "@/src/utils/clientSideDomainTyp
const ScoreFilterOptions = z.object({
projectId: z.string(), // Required for protectedProjectProcedure
- filter: z.array(singleFilter),
+ filter: singleFilterList,
orderBy: orderBy,
});
@@ -1145,7 +1145,7 @@ export const scoresRouter = createTRPCRouter({
.input(
z.object({
projectId: z.string(),
- filter: z.array(singleFilter).optional(),
+ filter: singleFilterList.optional(),
fromTimestamp: z.date().optional(),
toTimestamp: z.date().optional(),
}),
diff --git a/web/src/server/api/routers/sessions.ts b/web/src/server/api/routers/sessions.ts
index 33266e633d..498ffb141c 100644
--- a/web/src/server/api/routers/sessions.ts
+++ b/web/src/server/api/routers/sessions.ts
@@ -43,7 +43,7 @@ import {
orderBy,
paginationZod,
type PrismaClient,
- singleFilter,
+ singleFilterList,
timeFilter,
type SessionOptions,
type ScoreDomain,
@@ -60,7 +60,7 @@ import {
const SessionCountOptions = z.object({
projectId: z.string(), // Required for protectedProjectProcedure
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
orderBy: orderBy,
});
const SessionFilterOptions = SessionCountOptions.extend({
@@ -71,7 +71,7 @@ const SessionTraceObservationsInput = z.object({
projectId: z.string(),
sessionId: z.string(),
traceId: z.string(),
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
});
/**
diff --git a/web/src/server/api/routers/traces.ts b/web/src/server/api/routers/traces.ts
index 4ce558ee68..d466c2fdaa 100644
--- a/web/src/server/api/routers/traces.ts
+++ b/web/src/server/api/routers/traces.ts
@@ -44,7 +44,7 @@ import {
normalizeOrderByForTable,
orderBy,
paginationZod,
- singleFilter,
+ singleFilterList,
timeFilter,
type Observation,
hasValidTracingSearchTypes,
@@ -76,7 +76,7 @@ const TraceCountOptions = z
projectId: z.string(), // Required for protectedProjectProcedure
searchQuery: z.string().nullable(),
searchType: z.array(TracingSearchType),
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
orderBy: orderBy,
})
.refine(hasValidTracingSearchTypes, {
@@ -207,7 +207,7 @@ export const traceRouter = createTRPCRouter({
z.object({
projectId: z.string(),
traceIds: z.array(z.string()),
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
}),
)
.query(async ({ input, ctx }) => {
diff --git a/web/src/server/api/routers/users.ts b/web/src/server/api/routers/users.ts
index b82e73690b..92ad87deac 100644
--- a/web/src/server/api/routers/users.ts
+++ b/web/src/server/api/routers/users.ts
@@ -4,7 +4,7 @@ import {
createTRPCRouter,
protectedProjectProcedure,
} from "@/src/server/api/trpc";
-import { paginationZod, singleFilter } from "@langfuse/shared";
+import { paginationZod, singleFilterList } from "@langfuse/shared";
import {
getTotalUserCount,
getTracesGroupedByUsers,
@@ -18,7 +18,7 @@ import {
const UserFilterOptions = z.object({
projectId: z.string(), // Required for protectedProjectProcedure
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
searchQuery: z
.string()
.optional()
@@ -73,7 +73,7 @@ export const userRouter = createTRPCRouter({
z.object({
projectId: z.string(),
userIds: z.array(z.string().min(1)),
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
}),
)
.query(async ({ input }) => {
@@ -168,7 +168,7 @@ export const userRouter = createTRPCRouter({
z.object({
projectId: z.string(),
userIds: z.array(z.string().min(1)),
- filter: z.array(singleFilter).nullable(),
+ filter: singleFilterList.nullable(),
}),
)
.query(async ({ input }) => {
diff --git a/web/src/server/api/services/sqlInterface.ts b/web/src/server/api/services/sqlInterface.ts
index 752793d76a..a6f74ac4e8 100644
--- a/web/src/server/api/services/sqlInterface.ts
+++ b/web/src/server/api/services/sqlInterface.ts
@@ -1,4 +1,4 @@
-import { singleFilter } from "@langfuse/shared";
+import { singleFilterList } from "@langfuse/shared";
import { z } from "zod";
export type DatabaseRow = {
@@ -42,7 +42,7 @@ const orderByInterface = z.array(
}),
);
-export const filterInterface = z.array(singleFilter);
+export const filterInterface = singleFilterList;
export const sqlInterface = z.object({
from: z.enum([
diff --git a/worker/src/__tests__/evalService.filtering.test.ts b/worker/src/__tests__/evalService.filtering.test.ts
index e65118e92b..688ef0fe76 100644
--- a/worker/src/__tests__/evalService.filtering.test.ts
+++ b/worker/src/__tests__/evalService.filtering.test.ts
@@ -492,7 +492,7 @@ describe("test eval filtering", () => {
expect(jobs[0].status.toString()).toBe("PENDING");
}, 10_000);
- test("does not create eval job for a trace missing the metadata key when filtering on contains empty string", async ({
+ test("does not create eval job for a trace missing the metadata key when filtering on is set", async ({
expect,
upsertTwoTraces,
configureDefaultJobWithSingleFilter,
@@ -500,7 +500,6 @@ describe("test eval filtering", () => {
getJobs,
traceId1,
}) => {
- // trace1 has the "turn" metadata key, trace2 never had it set at all.
await upsertTwoTraces([
{
metadata: { turn: "1" },
@@ -510,15 +509,14 @@ describe("test eval filtering", () => {
},
]);
- // "contains ''" on a metadata key should behave as a key-existence
- // check, not match every trace regardless of whether the key was ever
- // set.
+ // `is set` matches only traces where the metadata key exists, not every
+ // trace regardless of whether the key was ever set.
await configureDefaultJobWithSingleFilter({
type: "stringObject",
key: "turn",
value: "",
column: "metadata",
- operator: "contains",
+ operator: "is set",
});
// No cachedTrace is passed here, so this exercises the database
diff --git a/worker/src/__tests__/inMemoryFilterService.test.ts b/worker/src/__tests__/inMemoryFilterService.test.ts
index d248159eca..aea4eea4c0 100644
--- a/worker/src/__tests__/inMemoryFilterService.test.ts
+++ b/worker/src/__tests__/inMemoryFilterService.test.ts
@@ -982,6 +982,63 @@ describe("InMemoryFilterService", () => {
).toBe(true);
});
+ test("evaluates stringObject key presence/absence operators", () => {
+ const presence = (key: string, operator: "is set" | "is not set") =>
+ InMemoryFilterService.evaluateFilter(
+ mockData,
+ [
+ {
+ column: "metadata",
+ type: "stringObject",
+ key,
+ operator,
+ value: "",
+ },
+ ],
+ fieldMapper,
+ );
+
+ expect(presence("userId", "is set")).toBe(true);
+ expect(presence("userId", "is not set")).toBe(false);
+ expect(presence("missingKey", "is set")).toBe(false);
+ expect(presence("missingKey", "is not set")).toBe(true);
+
+ // A key colliding with an inherited Object.prototype name counts as
+ // absent, mirroring the hasOwnProperty guard used by the value operators.
+ expect(presence("toString", "is set")).toBe(false);
+ expect(presence("toString", "is not set")).toBe(true);
+ });
+
+ test("treats a legacy empty substring value as key presence (matches when the key exists)", () => {
+ const emptySubstring = (
+ key: string,
+ operator: "contains" | "starts with" | "ends with",
+ ) =>
+ InMemoryFilterService.evaluateFilter(
+ mockData,
+ [
+ {
+ column: "metadata",
+ type: "stringObject",
+ key,
+ operator,
+ value: "",
+ },
+ ],
+ fieldMapper,
+ );
+
+ // Legacy `contains ""` / `starts with ""` / `ends with ""` behaved as a
+ // key-existence check; persisted rules using it must keep matching a
+ // present key (they are coerced to `is set` at the read boundary, and the
+ // in-memory evaluator must not flip them to never-match if one slips
+ // through uncoerced).
+ for (const op of ["contains", "starts with", "ends with"] as const) {
+ expect(emptySubstring("userId", op)).toBe(true);
+ expect(emptySubstring("missingKey", op)).toBe(false);
+ }
+ });
+
test("evaluates numberObject filters correctly", () => {
expect(
InMemoryFilterService.evaluateFilter(
diff --git a/worker/src/features/evaluation/evalService.ts b/worker/src/features/evaluation/evalService.ts
index 7f8be1ec27..b9def06503 100644
--- a/worker/src/features/evaluation/evalService.ts
+++ b/worker/src/features/evaluation/evalService.ts
@@ -45,7 +45,7 @@ import {
} from "./traceFilterUtils";
import {
Prisma,
- singleFilter,
+ singleFilterList,
variableMappingList,
evalDatasetFormFilterCols,
availableDatasetEvalVariables,
@@ -397,7 +397,7 @@ export const createEvalJobs = async ({
if (config.targetObject !== EvalTargetObject.TRACE) {
return false;
}
- const parsedFilter = z.array(singleFilter).safeParse(config.filter);
+ const parsedFilter = singleFilterList.safeParse(config.filter);
return (
!parsedFilter.success ||
inMemoryFilterRequiresMetadata(parsedFilter.data)
@@ -553,7 +553,7 @@ export const createEvalJobs = async ({
}
logger.debug("Creating eval job for config", config.id);
- const validatedFilter = z.array(singleFilter).parse(config.filter);
+ const validatedFilter = singleFilterList.parse(config.filter);
const maxTimeStamp =
"timestamp" in event &&
diff --git a/worker/src/features/evaluation/evalSpanAttributes.ts b/worker/src/features/evaluation/evalSpanAttributes.ts
index 45e13738ff..37adc28383 100644
--- a/worker/src/features/evaluation/evalSpanAttributes.ts
+++ b/worker/src/features/evaluation/evalSpanAttributes.ts
@@ -1,9 +1,8 @@
import { type JobConfiguration } from "@prisma/client";
-import { z } from "zod";
import {
EvalTargetObject,
observationVariableMappingList,
- singleFilter,
+ singleFilterList,
variableMappingList,
} from "@langfuse/shared";
import {
@@ -16,7 +15,7 @@ const dedupeStrings = (values: string[]): string[] => [
];
const getFilterDimensions = (filter: JobConfiguration["filter"]): string[] => {
- const parsedFilter = z.array(singleFilter).safeParse(filter);
+ const parsedFilter = singleFilterList.safeParse(filter);
if (!parsedFilter.success) return [];
return dedupeStrings(parsedFilter.data.map(({ column }) => column));
diff --git a/worker/src/features/evaluation/observationEval/fetchObservationEvalRules.ts b/worker/src/features/evaluation/observationEval/fetchObservationEvalRules.ts
index 5071206453..a2a83064a8 100644
--- a/worker/src/features/evaluation/observationEval/fetchObservationEvalRules.ts
+++ b/worker/src/features/evaluation/observationEval/fetchObservationEvalRules.ts
@@ -1,6 +1,7 @@
import {
EvalTargetObject,
JobConfigState,
+ coerceLegacyEmptyMetadataFilters,
normalizeEvaluationRuleTarget,
type FilterState,
} from "@langfuse/shared";
@@ -98,7 +99,7 @@ export async function fetchObservationEvalRules(
targetObject: rule.targetObject as
| typeof EvalTargetObject.EVENT
| typeof EvalTargetObject.EXPERIMENT,
- filter: rule.filter as FilterState,
+ filter: coerceLegacyEmptyMetadataFilters(rule.filter) as FilterState,
});
return {
...rule,
diff --git a/worker/src/features/evaluation/observationEval/scheduleObservationEvals.ts b/worker/src/features/evaluation/observationEval/scheduleObservationEvals.ts
index 31f104cc6d..d9ed113ad6 100644
--- a/worker/src/features/evaluation/observationEval/scheduleObservationEvals.ts
+++ b/worker/src/features/evaluation/observationEval/scheduleObservationEvals.ts
@@ -19,6 +19,7 @@ import {
type FilterState,
type EvalExecutionMode,
canRunEvalRule,
+ coerceLegacyEmptyMetadataFilters,
mapEventEvalFilterColumnIdToField,
observationVariableMappingList,
} from "@langfuse/shared";
@@ -360,7 +361,9 @@ function evaluateFilter(
observation: ObservationForEval,
config: ObservationEvalRule,
): boolean {
- const filterConditions = config.filter as FilterState;
+ const filterConditions = coerceLegacyEmptyMetadataFilters(
+ config.filter,
+ ) as FilterState;
// Empty filter matches all (for filter purposes)
const isEmptyFilter =