Skip to content

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

fix(evals): scope observation eval jobs by trace (#14124)

Hassieb Pakzadd96b1cc5a2d6
diff --git a/worker/src/features/evaluation/__tests__/productionDeps.integration.test.ts b/worker/src/features/evaluation/__tests__/productionDeps.integration.test.ts
index f993f85140..4d1ccf54f2 100644
--- a/worker/src/features/evaluation/__tests__/productionDeps.integration.test.ts
+++ b/worker/src/features/evaluation/__tests__/productionDeps.integration.test.ts
@@ -171,10 +171,11 @@ describe("Production Dependency Factories Integration Tests", () => {
it("should upload observation data to S3 and return the path", async () => {
const { projectId } = await createOrgProjectAndApiKey();
const observationId = randomUUID();
+ const traceId = randomUUID();
const observationData = {
id: observationId,
- traceId: randomUUID(),
+ traceId,
projectId,
type: "GENERATION",
input: { prompt: "Hello" },
@@ -186,6 +187,7 @@ describe("Production Dependency Factories Integration Tests", () => {
// Execute
const s3Path = await deps.uploadObservationToS3({
projectId,
+ traceId,
observationId,
data: observationData,
});
@@ -196,7 +198,7 @@ describe("Production Dependency Factories Integration Tests", () => {
// Verify path format (uses env prefix, defaults to "")
const prefix = env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX || "";
expect(s3Path).toBe(
- `${prefix}evals/${projectId}/observations/${observationId}.json`,
+ `${prefix}evals/${projectId}/traces/${traceId}/observations/${observationId}.json`,
);
// Verify file exists in S3 by checking it was created with the correct path
@@ -396,6 +398,7 @@ describe("Production Dependency Factories Integration Tests", () => {
const s3Path = await schedulerDeps.uploadObservationToS3({
projectId,
+ traceId,
observationId,
data: observationData,
});
diff --git a/worker/src/features/evaluation/observationEval/__tests__/observationEval.e2e.test.ts b/worker/src/features/evaluation/observationEval/__tests__/observationEval.e2e.test.ts
index ec3a55af01..372c244aaa 100644
--- a/worker/src/features/evaluation/observationEval/__tests__/observationEval.e2e.test.ts
+++ b/worker/src/features/evaluation/observationEval/__tests__/observationEval.e2e.test.ts
@@ -154,6 +154,7 @@ describe("Observation Eval E2E Pipeline", () => {
expect(pipeline.schedulerDeps.uploadObservationToS3).toHaveBeenCalledWith(
{
projectId,
+ traceId: observation.trace_id,
observationId: observation.span_id,
data: observation,
},
diff --git a/worker/src/features/evaluation/observationEval/__tests__/scheduleObservationEvals.test.ts b/worker/src/features/evaluation/observationEval/__tests__/scheduleObservationEvals.test.ts
index 7709240156..f5d8e66ba9 100644
--- a/worker/src/features/evaluation/observationEval/__tests__/scheduleObservationEvals.test.ts
+++ b/worker/src/features/evaluation/observationEval/__tests__/scheduleObservationEvals.test.ts
@@ -200,6 +200,7 @@ describe("scheduleObservationEvals", () => {
expect(schedulerDeps.uploadObservationToS3).toHaveBeenCalledTimes(1);
expect(schedulerDeps.uploadObservationToS3).toHaveBeenCalledWith({
projectId: "project-789",
+ traceId: "trace-456",
observationId: "obs-123",
data: observation,
});
@@ -342,7 +343,12 @@ describe("scheduleObservationEvals", () => {
});
const expectedJobExecutionId = createW3CTraceId(
- `${config.id}:${observation.span_id}`,
+ JSON.stringify([
+ "observation-eval",
+ config.id,
+ observation.trace_id,
+ observation.span_id,
+ ]),
);
expect(schedulerDeps.upsertJobExecution).toHaveBeenCalledWith({
id: expectedJobExecutionId,
@@ -370,7 +376,12 @@ describe("scheduleObservationEvals", () => {
});
const expectedJobExecutionId = createW3CTraceId(
- `${config.id}:${observation.span_id}`,
+ JSON.stringify([
+ "observation-eval",
+ config.id,
+ observation.trace_id,
+ observation.span_id,
+ ]),
);
expect(schedulerDeps.enqueueEvalJob).toHaveBeenCalledWith({
jobExecutionId: expectedJobExecutionId,
@@ -400,6 +411,73 @@ describe("scheduleObservationEvals", () => {
}),
);
});
+
+ it("should create distinct job executions for repeated observation ids on different traces", async () => {
+ const schedulerDeps = createMockSchedulerDeps();
+ const config = createMockConfig();
+ const firstObservation = createMockObservation({
+ span_id: "reused-observation-id",
+ trace_id: "trace-a",
+ });
+ const secondObservation = createMockObservation({
+ span_id: "reused-observation-id",
+ trace_id: "trace-b",
+ });
+
+ await scheduleObservationEvals({
+ observation: firstObservation,
+ configs: [config],
+ schedulerDeps,
+ });
+ await scheduleObservationEvals({
+ observation: secondObservation,
+ configs: [config],
+ schedulerDeps,
+ });
+
+ expect(schedulerDeps.upsertJobExecution).toHaveBeenCalledTimes(2);
+
+ const firstJobExecutionId = createW3CTraceId(
+ JSON.stringify([
+ "observation-eval",
+ config.id,
+ firstObservation.trace_id,
+ firstObservation.span_id,
+ ]),
+ );
+ const secondJobExecutionId = createW3CTraceId(
+ JSON.stringify([
+ "observation-eval",
+ config.id,
+ secondObservation.trace_id,
+ secondObservation.span_id,
+ ]),
+ );
+
+ expect(firstJobExecutionId).not.toBe(secondJobExecutionId);
+ expect(schedulerDeps.upsertJobExecution).toHaveBeenNthCalledWith(
+ 1,
+ expect.objectContaining({ id: firstJobExecutionId }),
+ );
+ expect(schedulerDeps.upsertJobExecution).toHaveBeenNthCalledWith(
+ 2,
+ expect.objectContaining({ id: secondJobExecutionId }),
+ );
+ expect(schedulerDeps.uploadObservationToS3).toHaveBeenNthCalledWith(
+ 1,
+ expect.objectContaining({
+ traceId: firstObservation.trace_id,
+ observationId: firstObservation.span_id,
+ }),
+ );
+ expect(schedulerDeps.uploadObservationToS3).toHaveBeenNthCalledWith(
+ 2,
+ expect.objectContaining({
+ traceId: secondObservation.trace_id,
+ observationId: secondObservation.span_id,
+ }),
+ );
+ });
});
describe("multiple configs", () => {
diff --git a/worker/src/features/evaluation/observationEval/createSchedulerDeps.ts b/worker/src/features/evaluation/observationEval/createSchedulerDeps.ts
index e3b1fe7a63..a705eb2593 100644
--- a/worker/src/features/evaluation/observationEval/createSchedulerDeps.ts
+++ b/worker/src/features/evaluation/observationEval/createSchedulerDeps.ts
@@ -50,7 +50,7 @@ export function createObservationEvalSchedulerDeps(): ObservationEvalSchedulerDe
},
uploadObservationToS3: async (params) => {
- const path = `${env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX}evals/${params.projectId}/observations/${params.observationId}.json`;
+ const path = `${env.LANGFUSE_S3_EVENT_UPLOAD_PREFIX}evals/${params.projectId}/traces/${params.traceId}/observations/${params.observationId}.json`;
const s3Client = getEvalS3StorageClient();
await s3Client.uploadJson(path, params.data);
diff --git a/worker/src/features/evaluation/observationEval/scheduleObservationEvals.ts b/worker/src/features/evaluation/observationEval/scheduleObservationEvals.ts
index 8fef0e1a6e..4cdadf5016 100644
--- a/worker/src/features/evaluation/observationEval/scheduleObservationEvals.ts
+++ b/worker/src/features/evaluation/observationEval/scheduleObservationEvals.ts
@@ -88,6 +88,7 @@ export async function scheduleObservationEvals(
// Upload observation to S3 once
const observationS3Path = await schedulerDeps.uploadObservationToS3({
projectId: observation.project_id,
+ traceId: observation.trace_id,
observationId: observation.span_id,
data: observation,
});
@@ -133,7 +134,12 @@ async function processMatchingConfig(
} = params;
const jobExecutionId = createW3CTraceId(
- `${matchingConfig.id}:${observation.span_id}`,
+ JSON.stringify([
+ "observation-eval",
+ matchingConfig.id,
+ observation.trace_id,
+ observation.span_id,
+ ]),
);
// Create job execution
diff --git a/worker/src/features/evaluation/observationEval/types.ts b/worker/src/features/evaluation/observationEval/types.ts
index 8ca7b9ad7b..98e0d7bee3 100644
--- a/worker/src/features/evaluation/observationEval/types.ts
+++ b/worker/src/features/evaluation/observationEval/types.ts
@@ -59,6 +59,7 @@ export interface ObservationEvalSchedulerDeps {
/** Upload observation data to S3 for later retrieval */
uploadObservationToS3: (params: {
projectId: string;
+ traceId: string;
observationId: string;
data: Record<string, unknown>;
}) => Promise<string>;