Skip to content

๐Ÿชข Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(ingestion): filter NULL string values from ingested events (#628)

Max Deichmanna0b84773dc74
diff --git a/src/__tests__/ingestion.servertest.ts b/src/__tests__/ingestion.servertest.ts
index f03e743f0e..d0b7d9f0b8 100644
--- a/src/__tests__/ingestion.servertest.ts
+++ b/src/__tests__/ingestion.servertest.ts
@@ -1,6 +1,7 @@
/** @jest-environment node */
import { makeAPICall, pruneDatabase } from "@/src/__tests__/test-utils";
+import { cleanEvent } from "@/src/pages/api/public/ingestion";
import { prisma } from "@/src/server/db";
import { v4 } from "uuid";
@@ -686,4 +687,80 @@ describe("/api/public/ingestion API Endpoint", () => {
expect(dbGeneration).toBeTruthy();
});
+
+ it("filters out NULL characters", async () => {
+ const traceId = v4();
+ const generationId = v4();
+
+ const responseOne = await makeAPICall("POST", "/api/public/ingestion", {
+ batch: [
+ {
+ id: v4(),
+ type: "trace-create",
+ timestamp: new Date().toISOString(),
+ body: {
+ id: traceId,
+ name: "trace-name",
+ },
+ },
+ {
+ id: v4(),
+ type: "observation-create",
+ timestamp: new Date().toISOString(),
+ body: {
+ id: generationId,
+ traceId: traceId,
+ type: "GENERATION",
+ name: "generation-name",
+ traceIdType: "LANGFUSE",
+ input: {
+ key: "IB\nibo.org Change site\nIB Home / . . . / News / News about the IB / Why ChatGPT is an opportunity for schools IB Home / News / News about the IB / Why ChatGPT is an opportunity for schools Why ChatGPT is an opportunity for schools Published: 06 March 2023 Last updated: 06 June 2023 Date published: 28 February 2023 Dr Matthew Glanville, Head of Assessment Principles and Practice Source: Why ChatGPT is an opportunity for schools | The Times Those of us who work in the schools or exam sector should not be terri \u0000 ed by ChatGPT and the rise of AI software โ€“ we should be excited. We should embrace it as an extraordinary opportunity. Contrary to some stark warnings, it is not the end of exams, nor even a huge threat to coursework, but it does bring into very sharp focus the impact that arti \u0000 ",
+ },
+ output: {
+ key: "์ œ์ ์ด ์žˆ์—ˆ์ฃ . ๊ทธ์ค‘ ํ•˜๋‚˜๋Š” ์ผ์ œ๊ฐ€ ํ•œ๊ตญ์˜ ์‹ ์šฉ์ฒด๊ณ„๋ฅผ ๋ง๊ฐ€๋œจ๋ฆฐ ์ฑ„ ํ•œ๊ตญ์„ ๋– ๋‚ฌ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ํ•ด๋ฐฉ์ „ ์ผ์ œ๋Š” ์กฐ์„ ์˜ ์‹ ์šฉ์ฒด๊ณ„๋ฅผ ๊ฑฐ์˜ ๋…์ ์ ์œผ๋กœ ์†Œ์œ ํ•œ ์ƒํ™ฉ์ด์—ˆ์Šต๋‹ˆ๋‹ค. 1945๋…„ 6์›” ๊ธฐ์ค€ ์ผ์ œ๋Š” ์กฐ์„ ์˜ ๋ณธ์ ์„ ๋‘” ์ „์ฒด์€ํ–‰ 5๊ฐœ์˜ ๋ถˆ์ž…์ž๋ณธ ์ด์•ก์˜ 89.7%๋ฅผ",
+ },
+ },
+ },
+ ],
+ });
+ expect(responseOne.status).toBe(207);
+
+ const dbTrace = await prisma.trace.findMany({
+ where: {
+ id: traceId,
+ },
+ });
+
+ expect(dbTrace.length).toEqual(1);
+
+ const dbGeneration = await prisma.observation.findUnique({
+ where: {
+ id: generationId,
+ },
+ });
+ expect(dbGeneration?.input).toStrictEqual({
+ key: `IB
+ibo.org Change site
+IB Home / . . . / News / News about the IB / Why ChatGPT is an opportunity for schools IB Home / News / News about the IB / Why ChatGPT is an opportunity for schools Why ChatGPT is an opportunity for schools Published: 06 March 2023 Last updated: 06 June 2023 Date published: 28 February 2023 Dr Matthew Glanville, Head of Assessment Principles and Practice Source: Why ChatGPT is an opportunity for schools | The Times Those of us who work in the schools or exam sector should not be terri ed by ChatGPT and the rise of AI software โ€“ we should be excited. We should embrace it as an extraordinary opportunity. Contrary to some stark warnings, it is not the end of exams, nor even a huge threat to coursework, but it does bring into very sharp focus the impact that arti `,
+ });
+ expect(dbGeneration?.output).toStrictEqual({
+ key: "์ œ์ ์ด ์žˆ์—ˆ์ฃ . ๊ทธ์ค‘ ํ•˜๋‚˜๋Š” ์ผ์ œ๊ฐ€ ํ•œ๊ตญ์˜ ์‹ ์šฉ์ฒด๊ณ„๋ฅผ ๋ง๊ฐ€๋œจ๋ฆฐ ์ฑ„ ํ•œ๊ตญ์„ ๋– ๋‚ฌ๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค. ํ•ด๋ฐฉ์ „ ์ผ์ œ๋Š” ์กฐ์„ ์˜ ์‹ ์šฉ์ฒด๊ณ„๋ฅผ ๊ฑฐ์˜ ๋…์ ์ ์œผ๋กœ ์†Œ์œ ํ•œ ์ƒํ™ฉ์ด์—ˆ์Šต๋‹ˆ๋‹ค. 1945๋…„ 6์›” ๊ธฐ์ค€ ์ผ์ œ๋Š” ์กฐ์„ ์˜ ๋ณธ์ ์„ ๋‘” ์ „์ฒด์€ํ–‰ 5๊ฐœ์˜ ๋ถˆ์ž…์ž๋ณธ ์ด์•ก์˜ 89.7%๋ฅผ",
+ });
+
+ expect(dbGeneration).toBeTruthy();
+ });
+
+ [
+ { input: "A\u0000hallo", expected: "Ahallo" },
+ { input: ["A\u0000hallo"], expected: ["Ahallo"] },
+ { input: { obj: ["A\u0000hallo"] }, expected: { obj: ["Ahallo"] } },
+ ].forEach(({ input, expected }) => {
+ it(`cleans events with null values ${JSON.stringify(
+ input,
+ )} ${JSON.stringify(expected)}`, () => {
+ const cleanedEvent = cleanEvent(input);
+ console.log(cleanedEvent);
+ expect(cleanedEvent).toStrictEqual(expected);
+ });
+ });
});
diff --git a/src/pages/api/public/ingestion.ts b/src/pages/api/public/ingestion.ts
index 8003c33428..d2a3a8430d 100644
--- a/src/pages/api/public/ingestion.ts
+++ b/src/pages/api/public/ingestion.ts
@@ -180,21 +180,23 @@ const handleSingleEvent = async (
JSON.stringify(event, null, 2),
);
- const { type } = event;
+ const cleanedEvent = singleEventSchema.parse(cleanEvent(event));
- await persistEventMiddleware(prisma, apiScope.projectId, req, event);
+ const { type } = cleanedEvent;
+
+ await persistEventMiddleware(prisma, apiScope.projectId, req, cleanedEvent);
let processor: EventProcessor;
switch (type) {
case eventTypes.TRACE_CREATE:
- processor = new TraceProcessor(event);
+ processor = new TraceProcessor(cleanedEvent);
break;
case eventTypes.OBSERVATION_CREATE:
case eventTypes.OBSERVAION_UPDATE:
- processor = new ObservationProcessor(event);
+ processor = new ObservationProcessor(cleanedEvent);
break;
case eventTypes.SCORE_CREATE: {
- processor = new ScoreProcessor(event);
+ processor = new ScoreProcessor(cleanedEvent);
break;
}
}
@@ -312,3 +314,24 @@ export const handleBatchResultLegacy = (
}
return res.status(200).send(results.length > 0 ? results[0]?.result : {});
};
+
+// cleans NULL characters from the event
+export function cleanEvent(obj: unknown): unknown {
+ if (typeof obj === "string") {
+ return obj.replace(/\u0000/g, "");
+ } else if (typeof obj === "object" && obj !== null) {
+ if (Array.isArray(obj)) {
+ return obj.map(cleanEvent);
+ } else {
+ // Here we assert that obj is a Record<string, unknown>
+ const objAsRecord = obj as Record<string, unknown>;
+ const newObj: Record<string, unknown> = {};
+ for (const key in objAsRecord) {
+ newObj[key] = cleanEvent(objAsRecord[key]);
+ }
+ return newObj;
+ }
+ } else {
+ return obj;
+ }
+}