Skip to content

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

fix(ingestion): no processing of update events only with no existing record (#2955)

Hassieb Pakzada84d826e2200
diff --git a/packages/shared/src/server/services/PromptService.ts b/packages/shared/src/server/services/PromptService.ts
index c57c3ea5fd..3d8b535fad 100644
--- a/packages/shared/src/server/services/PromptService.ts
+++ b/packages/shared/src/server/services/PromptService.ts
@@ -18,12 +18,6 @@ export class PromptService {
(cacheEnabled || env.LANGFUSE_CACHE_PROMPT_ENABLED === "true");
this.ttlSeconds = env.LANGFUSE_CACHE_PROMPT_TTL_SECONDS;
-
- if (this.cacheEnabled) {
- this.logInfo("Prompt cache enabled with TTL seconds", this.ttlSeconds);
- } else {
- this.logInfo("Prompt cache disabled");
- }
}
public async getPrompt(params: PromptParams): Promise<Prompt | null> {
diff --git a/worker/src/features/tokenisation/usage.ts b/worker/src/features/tokenisation/usage.ts
index 24c1542219..1813c42f40 100644
--- a/worker/src/features/tokenisation/usage.ts
+++ b/worker/src/features/tokenisation/usage.ts
@@ -47,7 +47,10 @@ export function tokenCount(p: {
} else if (p.model.tokenizerId === "claude") {
return claudeTokenCount(p.text);
} else {
- console.error(`Unknown tokenizer ${p.model.tokenizerId}`);
+ if (p.model.tokenizerId) {
+ console.error(`Unknown tokenizer ${p.model.tokenizerId}`);
+ }
+
return undefined;
}
}
diff --git a/worker/src/services/IngestionService/index.ts b/worker/src/services/IngestionService/index.ts
index 4c28dc2e21..4bf8fa28f2 100644
--- a/worker/src/services/IngestionService/index.ts
+++ b/worker/src/services/IngestionService/index.ts
@@ -28,7 +28,6 @@ import {
IngestionUtils,
ClickhouseEntityType,
PromptService,
- QueueJobs,
} from "@langfuse/shared/src/server";
import { tokenCount } from "../../features/tokenisation/usage";
@@ -166,10 +165,21 @@ export class IngestionService {
const scoreRecords = await Promise.all(scoreRecordPromises);
+ const clickhouseScoreRecord = await this.getClickhouseRecord({
+ projectId,
+ entityId,
+ table: TableName.Scores,
+ });
+
+ if (!clickhouseScoreRecord && !this.hasCreateEvent(scoreEventList)) {
+ throw new Error(
+ `No create event or existing record found for score with id ${entityId} in project ${projectId}`
+ );
+ }
+
const finalScoreRecord: ScoreRecordInsertType =
await this.mergeScoreRecords({
- projectId,
- entityId,
+ clickhouseScoreRecord,
scoreRecords,
});
@@ -193,9 +203,20 @@ export class IngestionService {
traceEventList: timeSortedEvents,
});
- const finalTraceRecord = await this.mergeTraceRecords({
+ const clickhouseTraceRecord = await this.getClickhouseRecord({
projectId,
entityId,
+ table: TableName.Traces,
+ });
+
+ if (!clickhouseTraceRecord && !this.hasCreateEvent(traceEventList)) {
+ throw new Error(
+ `No create event or existing record found for trace with id ${entityId} in project ${projectId}`
+ );
+ }
+
+ const finalTraceRecord = await this.mergeTraceRecords({
+ clickhouseTraceRecord,
traceRecords,
});
@@ -221,10 +242,25 @@ export class IngestionService {
prompt,
});
- const finalObservationRecord = await this.mergeObservationRecords({
+ const clickhouseObservationRecord = await this.getClickhouseRecord({
projectId,
entityId,
+ table: TableName.Observations,
+ });
+
+ if (
+ !clickhouseObservationRecord &&
+ !this.hasCreateEvent(observationEventList)
+ ) {
+ throw new Error(
+ `No create event or existing record found for observation with id ${entityId} in project ${projectId}`
+ );
+ }
+
+ const finalObservationRecord = await this.mergeObservationRecords({
+ projectId,
observationRecords,
+ clickhouseObservationRecord,
});
// Backward compat: create wrapper trace for SDK < 2.0.0 events that do not have a traceId
@@ -253,17 +289,10 @@ export class IngestionService {
}
private async mergeScoreRecords(params: {
- projectId: string;
- entityId: string;
scoreRecords: ScoreRecordInsertType[];
+ clickhouseScoreRecord?: ScoreRecordInsertType | null;
}): Promise<ScoreRecordInsertType> {
- const { projectId, entityId, scoreRecords } = params;
-
- const clickhouseScoreRecord = await this.getClickhouseRecord({
- projectId,
- entityId,
- table: TableName.Scores,
- });
+ const { scoreRecords, clickhouseScoreRecord } = params;
const recordsToMerge = clickhouseScoreRecord
? [clickhouseScoreRecord, ...scoreRecords]
@@ -278,17 +307,10 @@ export class IngestionService {
}
private async mergeTraceRecords(params: {
- projectId: string;
- entityId: string;
traceRecords: TraceRecordInsertType[];
+ clickhouseTraceRecord?: TraceRecordInsertType | null;
}): Promise<TraceRecordInsertType> {
- const { projectId, entityId, traceRecords } = params;
-
- const clickhouseTraceRecord = await this.getClickhouseRecord({
- projectId,
- entityId,
- table: TableName.Traces,
- });
+ const { traceRecords, clickhouseTraceRecord } = params;
const recordsToMerge = clickhouseTraceRecord
? [clickhouseTraceRecord, ...traceRecords]
@@ -304,19 +326,14 @@ export class IngestionService {
private async mergeObservationRecords(params: {
projectId: string;
- entityId: string;
observationRecords: ObservationRecordInsertType[];
+ clickhouseObservationRecord?: ObservationRecordInsertType | null;
}): Promise<ObservationRecordInsertType> {
- const { projectId, entityId, observationRecords } = params;
-
- const existingObservationRecord = await this.getClickhouseRecord({
- projectId,
- entityId,
- table: TableName.Observations,
- });
+ const { projectId, observationRecords, clickhouseObservationRecord } =
+ params;
- const recordsToMerge = existingObservationRecord
- ? [existingObservationRecord, ...observationRecords]
+ const recordsToMerge = clickhouseObservationRecord
+ ? [clickhouseObservationRecord, ...observationRecords]
: observationRecords;
const mergedRecord = this.mergeRecords(
@@ -756,6 +773,14 @@ export class IngestionService {
private getMillisecondTimestamp(timestamp?: string | null): number {
return timestamp ? new Date(timestamp).getTime() : Date.now();
}
+
+ private hasCreateEvent(
+ eventList: ObservationEvent[] | ScoreEventType[] | TraceEventType[]
+ ): boolean {
+ return eventList.some((event) =>
+ event.type.toLowerCase().includes("create")
+ );
+ }
}
type ObservationPrompt = Pick<Prompt, "id" | "name" | "version">;
diff --git a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
index ddd15c64b9..8c1a6f3d0c 100644
--- a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
+++ b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
@@ -896,6 +896,110 @@ describe("Ingestion end-to-end tests", () => {
expect(trace.tags.length).toBe(4);
});
+ it("should fail if no create event AND no existing record in CH", async () => {
+ const traceId = randomUUID();
+ const generationId = randomUUID();
+
+ // First flush
+ const traceEventList1: TraceEventType[] = [
+ {
+ id: randomUUID(),
+ type: "trace-create",
+ timestamp: new Date().toISOString(),
+ body: {
+ id: traceId,
+ name: "trace-name",
+ userId: "user-1",
+ metadata: { key: "value" },
+ release: "1.0.0",
+ version: "2.0.0",
+ tags: ["tag-1", "tag-2", "tag-2"],
+ },
+ },
+ ];
+
+ const generationEventListNoCreate: ObservationEvent[] = [
+ {
+ id: randomUUID(),
+ type: "observation-update",
+ timestamp: new Date().toISOString(),
+ body: {
+ id: generationId,
+ traceId: traceId,
+ type: "GENERATION",
+ output: { key: "this is a great gpt output" },
+ },
+ },
+ ];
+
+ await ingestionService.processTraceEventList({
+ projectId,
+ entityId: traceId,
+ traceEventList: traceEventList1,
+ });
+
+ expect(
+ ingestionService.processObservationEventList({
+ projectId,
+ entityId: generationId,
+ observationEventList: generationEventListNoCreate,
+ })
+ ).rejects.toThrow();
+
+ const generationEventListWithCreate: ObservationEvent[] = [
+ {
+ id: randomUUID(),
+ type: "observation-create",
+ timestamp: new Date().toISOString(),
+ body: {
+ id: generationId,
+ traceId: traceId,
+ type: "GENERATION",
+ input: "This is a great prompt",
+ },
+ },
+ ];
+
+ await ingestionService.processObservationEventList({
+ projectId,
+ entityId: generationId,
+ observationEventList: generationEventListWithCreate,
+ });
+
+ await clickhouseWriter.flushAll(true);
+
+ vi.useRealTimers();
+ await new Promise((resolve) => setTimeout(resolve, 1000));
+ vi.useFakeTimers();
+
+ // Now the generation update should work
+ await ingestionService.processObservationEventList({
+ projectId,
+ entityId: generationId,
+ observationEventList: generationEventListNoCreate,
+ });
+
+ await clickhouseWriter.flushAll(true);
+
+ vi.useRealTimers();
+ await new Promise((resolve) => setTimeout(resolve, 1000));
+ vi.useFakeTimers();
+
+ const generation = await getClickhouseRecord(
+ TableName.Observations,
+ generationId
+ );
+
+ expect(generation.id).toBe(generationId);
+ expect(generation.trace_id).toBe(traceId);
+ expect(generation.output).toEqual(
+ JSON.stringify({
+ key: "this is a great gpt output",
+ })
+ );
+ expect(generation.input).toEqual("This is a great prompt");
+ });
+
it("should upsert traces in the right order", async () => {
const traceId = randomUUID();
@@ -1048,10 +1152,6 @@ describe("Ingestion end-to-end tests", () => {
]);
await clickhouseWriter.flushAll(true);
- console.log(
- "generation1",
- await getClickhouseRecord(TableName.Observations, generationId)
- );
const generationEventList2: ObservationEvent[] = [
{