Skip to content

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

fix: reject filters on pair-expanded dimensions (#16481)

Valery Meleshkinf6725ae52a17
diff --git a/packages/shared/src/features/query/server/queryBuilder.filterValidation.test.ts b/packages/shared/src/features/query/server/queryBuilder.filterValidation.test.ts
index 21aa53ed9b..115f923028 100644
--- a/packages/shared/src/features/query/server/queryBuilder.filterValidation.test.ts
+++ b/packages/shared/src/features/query/server/queryBuilder.filterValidation.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it, vi } from "vitest";
+import { InvalidRequestError } from "../../../errors";
import type { FilterCondition } from "../../../types";
import { getViewDeclaration } from "../dataModel";
import type { QueryType, ViewVersion } from "../types";
@@ -130,6 +131,24 @@ describe("queryBuilder filter type validation", () => {
},
);
+ it("rejects filters on pair-expanded dimensions", async () => {
+ const result = buildQueryWithFilter(
+ {
+ column: "usageType",
+ operator: "contains",
+ value: "cache",
+ type: "string",
+ },
+ undefined,
+ "v2",
+ );
+
+ await expect(result).rejects.toThrow(InvalidRequestError);
+ await expect(result).rejects.toThrow(
+ "Field 'usageType' cannot be used as a filter.",
+ );
+ });
+
it.each([
{
name: "arrayOptions on string array dimension",
diff --git a/packages/shared/src/features/query/server/queryBuilder.ts b/packages/shared/src/features/query/server/queryBuilder.ts
index cf07b95a24..e02fa96e3e 100644
--- a/packages/shared/src/features/query/server/queryBuilder.ts
+++ b/packages/shared/src/features/query/server/queryBuilder.ts
@@ -337,6 +337,12 @@ export class QueryBuilder {
const dimension = this.resolveDimension(filter.column, view);
if (dimension) {
+ if (dimension.pairExpand) {
+ throw new InvalidRequestError(
+ `Field '${filter.column}' cannot be used as a filter.`,
+ );
+ }
+
const compatibleFilterTypes = getCompatibleFilterTypesForQueryDimension(
dimension.type,
);
diff --git a/web/src/__tests__/server/mcp-tools-read.servertest.ts b/web/src/__tests__/server/mcp-tools-read.servertest.ts
index fd5c2d71a3..796b6cdb28 100644
--- a/web/src/__tests__/server/mcp-tools-read.servertest.ts
+++ b/web/src/__tests__/server/mcp-tools-read.servertest.ts
@@ -36,6 +36,7 @@ vi.mock(
import { nanoid } from "nanoid";
import { createHash, randomUUID } from "crypto";
+import { ErrorCode } from "@modelcontextprotocol/sdk/types.js";
import { z } from "zod";
import { prisma } from "@langfuse/shared/src/db";
import {
@@ -2099,6 +2100,34 @@ describe("MCP Read Tools", () => {
).rejects.toThrow(/Use returned metric aliases.*getMetricsSchema/i);
});
+ it("should return an invalid request for filters on pair-expanded dimensions", async () => {
+ const context = mockServerContext();
+
+ await expect(
+ handleQueryMetrics(
+ {
+ view: "observations",
+ metrics: [{ measure: "count", aggregation: "count" }],
+ filters: [
+ {
+ type: "string",
+ column: "usageType",
+ operator: "contains",
+ value: "cache",
+ },
+ ],
+ ...metricsWindow,
+ } as unknown as Parameters<typeof handleQueryMetrics>[0],
+ context,
+ ),
+ ).rejects.toMatchObject({
+ code: ErrorCode.InvalidRequest,
+ message: expect.stringContaining(
+ "Field 'usageType' cannot be used as a filter.",
+ ),
+ });
+ });
+
it("should apply default row_limit of 100 when omitted", async () => {
const { context, projectId } = await createMcpTestSetup();
const traceId = randomUUID();