Skip to content

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

fix(evals): target logical root observations (#15569)

Valery Meleshkin961a46f39fcc
diff --git a/fern/apis/server/definition/unstable/commons.yml b/fern/apis/server/definition/unstable/commons.yml
index 619512002e..05803443f9 100644
--- a/fern/apis/server/definition/unstable/commons.yml
+++ b/fern/apis/server/definition/unstable/commons.yml
@@ -494,6 +494,7 @@ types:
- `stringOptions`: `any of`, `none of`
- `arrayOptions`: `any of`, `none of`, `all of`
- `stringObject`: same operators as `string`
+ - `boolean`: `"="`, `"<>"`
- `null`: `is null`, `is not null`
Supported columns by target:
@@ -508,6 +509,7 @@ types:
- `sessionId`: `string`
- `tags`: `arrayOptions`, operators `any of` / `none of` / `all of`
- `metadata`: `stringObject` with `key`
+ - `isRootObservation`: `boolean`, operators `=` / `<>`; true when the observation has no parent or is explicitly marked as an application root
- `parentObservationId`: `null`, operators `is null` / `is not null`
- `calledToolNames`: `arrayOptions`, operators `any of` / `none of` / `all of`
- `toolCalls`: `number`
@@ -561,6 +563,12 @@ types:
type: "null"
column: parentObservationId
operator: is null
+ - name: ObservationLogicalRootFilter
+ value:
+ type: boolean
+ column: isRootObservation
+ operator: "="
+ value: true
- name: ObservationTagsFilter
value:
type: arrayOptions
diff --git a/packages/shared/src/eventsTable.ts b/packages/shared/src/eventsTable.ts
index 3ad1e21b10..cb7ac9aac6 100644
--- a/packages/shared/src/eventsTable.ts
+++ b/packages/shared/src/eventsTable.ts
@@ -3,6 +3,15 @@ import { type ColumnDefinition } from "./tableDefinitions";
export const eventsTableHasParentObservationSql = "e.parent_span_id != ''";
export const eventsTableIsRootObservationSql =
"(e.parent_span_id = '' OR e.is_app_root = true)";
+
+export const isRootObservation = ({
+ parentObservationId,
+ isAppRoot,
+}: {
+ parentObservationId: string | null | undefined;
+ isAppRoot: boolean | null | undefined;
+}): boolean => !parentObservationId || isAppRoot === true;
+
// True when the observation carries input / output. NULL/'' both count as
// absent (NULL != '' is NULL, i.e. not true), so only real payloads match.
export const eventsTableHasInputSql = "e.input != ''";
diff --git a/packages/shared/src/features/evals/observationForEval.ts b/packages/shared/src/features/evals/observationForEval.ts
index ab031bf331..cfbd47f4e3 100644
--- a/packages/shared/src/features/evals/observationForEval.ts
+++ b/packages/shared/src/features/evals/observationForEval.ts
@@ -7,6 +7,7 @@ import { SingleValueOption } from "../../tableDefinitions";
import { ColumnDefinition } from "../../tableDefinitions";
import { formatColumnOptions } from "../../tableDefinitions/typeHelpers";
import { parseJsonIfString } from "../../utils/json";
+import { isRootObservation } from "../../eventsTable";
const flexibleUsageCostSchema = z.record(
z.string(),
@@ -32,6 +33,7 @@ export const observationForEvalSchema = z.object({
trace_name: z.string().nullish(),
user_id: z.string().nullish(),
session_id: z.string().nullish(),
+ is_app_root: z.boolean().default(false),
tags: z.array(z.string()).default([]),
release: z.string().nullish(),
@@ -181,6 +183,7 @@ export type ObservationEvalFilterColumnInternal =
| "experiment_dataset_id"
| "metadata"
| "parent_span_id"
+ | "is_app_root"
| "tool_call_names"
| "tool_call_count"
>;
@@ -369,6 +372,12 @@ export const observationEvalFilterColumns: ObservationEvalColumnDef[] = [
type: "stringObject",
internal: "metadata",
},
+ {
+ name: "Is Root Observation",
+ id: "isRootObservation",
+ type: "boolean",
+ internal: "is_app_root",
+ },
{
name: "Parent Observation",
id: "parentObservationId",
@@ -474,5 +483,13 @@ export function mapEventEvalFilterColumnIdToField(
if (!columnMapping) {
return undefined;
}
+
+ if (columnMapping.id === "isRootObservation") {
+ return isRootObservation({
+ parentObservationId: observation.parent_span_id,
+ isAppRoot: observation.is_app_root,
+ });
+ }
+
return observation[columnMapping.internal];
}
diff --git a/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.ts b/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.ts
index e120967d0b..b4842acfc1 100644
--- a/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.ts
+++ b/packages/shared/src/server/queries/clickhouse-sql/event-query-builder.ts
@@ -119,6 +119,7 @@ const EVENTS_FIELDS = {
type: "e.type as type",
parentObservationId: 'e.parent_span_id as "parent_observation_id"',
isRootObservation: `toBool(${eventsTableIsRootObservationSql}) as "is_root_observation"`,
+ isAppRoot: 'e.is_app_root as "is_app_root"',
name: "e.name as name",
level: "e.level as level",
statusMessage: 'e.status_message as "status_message"',
@@ -443,6 +444,7 @@ const FIELD_SETS = {
"traceId",
"projectId",
"parentObservationId",
+ "isAppRoot",
"type",
"name",
"environment",
diff --git a/packages/shared/src/server/queries/clickhouse-sql/events-stream-query.test.ts b/packages/shared/src/server/queries/clickhouse-sql/events-stream-query.test.ts
index b3b02c0403..8311196673 100644
--- a/packages/shared/src/server/queries/clickhouse-sql/events-stream-query.test.ts
+++ b/packages/shared/src/server/queries/clickhouse-sql/events-stream-query.test.ts
@@ -51,6 +51,7 @@ describe("buildEventsStreamQuery", () => {
expect(query).toContain("e.project_id = {projectId: String}");
expect(query).toContain("e.is_deleted = 0");
+ expect(query).toContain('e.is_app_root as "is_app_root"');
const orderIndex = query.lastIndexOf("ORDER BY ");
const deduplicationIndex = query.lastIndexOf("LIMIT 1 BY ");
diff --git a/packages/shared/src/server/repositories/events-stream.ts b/packages/shared/src/server/repositories/events-stream.ts
index 56b0368524..68e4137aea 100644
--- a/packages/shared/src/server/repositories/events-stream.ts
+++ b/packages/shared/src/server/repositories/events-stream.ts
@@ -44,6 +44,7 @@ export const getEventsStreamForEval = async (props: {
trace_id: string;
project_id: string;
parent_observation_id: string | null;
+ is_app_root: boolean;
type: string;
name: string | null;
environment: string | null;
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index 567d26588e..409c980bf9 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -15024,6 +15024,8 @@ components:
- `stringObject`: same operators as `string`
+ - `boolean`: `"="`, `"<>"`
+
- `null`: `is null`, `is not null`
@@ -15040,6 +15042,7 @@ components:
- `sessionId`: `string`
- `tags`: `arrayOptions`, operators `any of` / `none of` / `all of`
- `metadata`: `stringObject` with `key`
+ - `isRootObservation`: `boolean`, operators `=` / `<>`; true when the observation has no parent or is explicitly marked as an application root
- `parentObservationId`: `null`, operators `is null` / `is not null`
- `calledToolNames`: `arrayOptions`, operators `any of` / `none of` / `all of`
- `toolCalls`: `number`
diff --git a/web/src/__tests__/server/unit/unstable-evals-contract.servertest.ts b/web/src/__tests__/server/unit/unstable-evals-contract.servertest.ts
index c58b944d52..9c0edf4b30 100644
--- a/web/src/__tests__/server/unit/unstable-evals-contract.servertest.ts
+++ b/web/src/__tests__/server/unit/unstable-evals-contract.servertest.ts
@@ -197,7 +197,21 @@ describe("unstable public eval contracts", () => {
expect(parsed.success).toBe(false);
});
- it("accepts code evaluation rule create bodies without mappings", () => {
+ it("accepts both root filters on code rule bodies without mappings", () => {
+ const rootFilters = [
+ {
+ type: "boolean",
+ column: "isRootObservation",
+ operator: "=",
+ value: true,
+ },
+ {
+ type: "null",
+ column: "parentObservationId",
+ operator: "is null",
+ value: "",
+ },
+ ] as const;
const parsed = PostUnstableEvaluationRuleBody.parse({
name: "toxicity-code-live",
evaluator: {
@@ -208,9 +222,10 @@ describe("unstable public eval contracts", () => {
target: "observation",
enabled: true,
sampling: 1,
- filter: [],
+ filter: rootFilters,
});
+ expect(parsed.filter).toEqual(rootFilters);
expect("mapping" in parsed).toBe(false);
expect("variableMapping" in parsed).toBe(false);
});
diff --git a/web/src/features/evals/eval-config-target-options.clienttest.ts b/web/src/features/evals/eval-config-target-options.clienttest.ts
index 2da687326e..fbe72f81dd 100644
--- a/web/src/features/evals/eval-config-target-options.clienttest.ts
+++ b/web/src/features/evals/eval-config-target-options.clienttest.ts
@@ -1,8 +1,9 @@
import { EvalTargetObject } from "@langfuse/shared";
import { resolveCheckboxOperator } from "@/src/features/filters/hooks/useSidebarFilterState";
import { evalConfigFilterColumns } from "@/src/server/api/definitions/evalConfigsTable";
+import { DEFAULT_OBSERVATION_FILTER_WHEN_REMAPPING } from "@/src/features/evals/utils/evaluator-constants";
-describe("eval config target filter options", () => {
+describe("eval config target behavior", () => {
it("should exclude all non-trace targets when selecting trace", () => {
const targetColumn = evalConfigFilterColumns.find(
(col) => col.id === "target",
@@ -35,4 +36,15 @@ describe("eval config target filter options", () => {
]),
});
});
+
+ it("uses semantic roots when remapping trace evaluators", () => {
+ expect(DEFAULT_OBSERVATION_FILTER_WHEN_REMAPPING).toEqual([
+ {
+ column: "isRootObservation",
+ operator: "=",
+ value: true,
+ type: "boolean",
+ },
+ ]);
+ });
});
diff --git a/web/src/features/evals/utils/evaluator-constants.ts b/web/src/features/evals/utils/evaluator-constants.ts
index 2c50e182ab..afb6e82ba1 100644
--- a/web/src/features/evals/utils/evaluator-constants.ts
+++ b/web/src/features/evals/utils/evaluator-constants.ts
@@ -69,9 +69,9 @@ export const DEFAULT_OBSERVATION_FILTER = [
// Default filter when remapping an evaluator from trace-level to observation-level
export const DEFAULT_OBSERVATION_FILTER_WHEN_REMAPPING = [
{
- column: "parentObservationId",
- operator: "is null" as const,
- value: "",
- type: "null" as const,
+ column: "isRootObservation",
+ operator: "=" as const,
+ value: true,
+ type: "boolean" as const,
},
];
diff --git a/worker/src/features/evaluation/observationEval/__tests__/extractObservationVariables.test.ts b/worker/src/features/evaluation/observationEval/__tests__/extractObservationVariables.test.ts
index 77ecf4e777..8b9dd3d052 100644
--- a/worker/src/features/evaluation/observationEval/__tests__/extractObservationVariables.test.ts
+++ b/worker/src/features/evaluation/observationEval/__tests__/extractObservationVariables.test.ts
@@ -70,6 +70,7 @@ describe("extractObservationVariables", () => {
trace_name: "my-trace",
user_id: "user-abc",
session_id: "session-xyz",
+ is_app_root: false,
tags: ["tag1", "tag2"],
release: "v2.0.0",
diff --git a/worker/src/features/evaluation/observationEval/__tests__/filterEvaluation.test.ts b/worker/src/features/evaluation/observationEval/__tests__/filterEvaluation.test.ts
index ca4cfbab7a..5b10325ada 100644
--- a/worker/src/features/evaluation/observationEval/__tests__/filterEvaluation.test.ts
+++ b/worker/src/features/evaluation/observationEval/__tests__/filterEvaluation.test.ts
@@ -1151,6 +1151,33 @@ describe("Filter Evaluation for Observation Evals", () => {
expect(matched).toBe(false);
});
});
+
+ it.each([
+ ["physical root", null, false, true],
+ ["app root", "external-parent", true, true],
+ ["ordinary child", "parent", false, false],
+ ] as const)(
+ "matches semantic roots for a %s",
+ async (_name, parentObservationId, isAppRoot, expected) => {
+ expect(
+ await testFilterMatch(
+ createTestObservation({
+ project_id: projectId,
+ parent_span_id: parentObservationId,
+ is_app_root: isAppRoot,
+ }),
+ [
+ {
+ column: "isRootObservation",
+ type: "boolean",
+ operator: "=",
+ value: true,
+ },
+ ],
+ ),
+ ).toBe(expected);
+ },
+ );
});
describe("tool_calls computation", () => {
diff --git a/worker/src/features/evaluation/observationEval/__tests__/fixtures.ts b/worker/src/features/evaluation/observationEval/__tests__/fixtures.ts
index c98494bbee..2c864ab150 100644
--- a/worker/src/features/evaluation/observationEval/__tests__/fixtures.ts
+++ b/worker/src/features/evaluation/observationEval/__tests__/fixtures.ts
@@ -60,6 +60,7 @@ export function createTestObservation(
trace_name: "test-trace",
user_id: "user-123",
session_id: "session-456",
+ is_app_root: false,
tags: ["test-tag"],
release: "v1.0.0",
diff --git a/worker/src/features/evaluation/observationEval/__tests__/observationForEvalSchema.test.ts b/worker/src/features/evaluation/observationEval/__tests__/observationForEvalSchema.test.ts
index 38b17c1d69..dc0969126a 100644
--- a/worker/src/features/evaluation/observationEval/__tests__/observationForEvalSchema.test.ts
+++ b/worker/src/features/evaluation/observationEval/__tests__/observationForEvalSchema.test.ts
@@ -78,6 +78,7 @@ describe("observationForEvalSchema", () => {
trace_name: "my-trace",
user_id: "user-123",
session_id: "session-456",
+ is_app_root: false,
tags: ["tag1", "tag2"],
release: "v2.0.0",
provided_model_name: "gpt-4",