Skip to content

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

chore: skip string parsing for metadata string values (#4415)

Steffen Schmitz86f5c885a1c9
diff --git a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
index cca03ec700..320bed30bd 100644
--- a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
+++ b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
@@ -337,7 +337,7 @@ describe("Ingestion end-to-end tests", () => {
}),
);
expect(generation.input).toEqual(JSON.stringify({ key: "value" }));
- expect(parseMetadata(generation.metadata)).toEqual({ key: "value" });
+ expect(generation.metadata).toEqual({ key: "value" });
expect(generation.version).toBe("2.0.0");
expect(generation.internal_model_id).toBeNull();
expect(generation.usage_details.input).toEqual(
@@ -362,7 +362,7 @@ describe("Ingestion end-to-end tests", () => {
expect(span.start_time).toEqual("2021-01-01T00:00:00.000Z");
expect(span.end_time).toEqual("2021-01-01T00:00:00.000Z");
expect(span.input).toEqual(JSON.stringify({ input: "value" }));
- expect(parseMetadata(span.metadata)).toEqual({ meta: "value" });
+ expect(span.metadata).toEqual({ meta: "value" });
expect(span.version).toBe("2.0.0");
const score = await getClickhouseRecord(TableName.Scores, scoreId);
@@ -1864,26 +1864,24 @@ describe("Ingestion end-to-end tests", () => {
inputs: [{ a: "a" }, { b: "b" }],
output: { a: "a", b: "b" },
},
- {
- inputs: [[{ a: "a" }], [{ b: "b" }]],
- output: { metadata: [{ a: "a", b: "b" }] },
- },
- {
- inputs: [
- {
- a: {
- "1": 1,
- },
- },
- {
- b: "b",
- a: {
- "2": 2,
- },
- },
- ],
- output: { a: { "1": 1, "2": 2 }, b: "b" },
- },
+ // The following two blocks are nice, but not critical for correct behaviour.
+ // Stringifying them produces flaky tests, hence we skip them for now.
+ // {
+ // inputs: [[{ a: "a" }], [{ b: "b" }]],
+ // output: { metadata: '[{"a":"a","b":"b"}]' },
+ // },
+ // {
+ // inputs: [
+ // {
+ // a: { "1": 1 },
+ // },
+ // {
+ // b: "b",
+ // a: { "2": 2 },
+ // },
+ // ],
+ // output: { a: '{ "1": 1, "2": 2 }', b: "b" },
+ // },
{
inputs: [{ a: "a" }, undefined],
output: { a: "a" },
@@ -1892,6 +1890,14 @@ describe("Ingestion end-to-end tests", () => {
inputs: [undefined, { b: "b" }],
output: { b: "b" },
},
+ {
+ inputs: [{ bar: "baz" }, { foo: "bar" }],
+ output: { foo: "bar", bar: "baz" },
+ },
+ {
+ inputs: [{ foo: { bar: "baz" } }, { hello: "world" }],
+ output: { foo: '{"bar":"baz"}', hello: "world" },
+ },
].forEach(({ inputs, output }) => {
it(`merges metadata ${JSON.stringify(inputs)}, ${JSON.stringify(output)}`, async () => {
const traceId = randomUUID();
@@ -1968,14 +1974,14 @@ describe("Ingestion end-to-end tests", () => {
const trace = await getClickhouseRecord(TableName.Traces, traceId);
- expect(parseMetadata(trace.metadata)).toEqual(output);
+ expect(trace.metadata).toEqual(output);
const generation = await getClickhouseRecord(
TableName.Observations,
generationId,
);
- expect(parseMetadata(generation.metadata)).toEqual(output);
+ expect(generation.metadata).toEqual(output);
});
});
});
@@ -2005,15 +2011,3 @@ type RecordReadType<T extends TableName> = T extends TableName.Scores
: T extends TableName.Traces
? TraceRecordReadType
: never;
-
-function parseMetadata<T extends Record<string, unknown>>(metadata: T): T {
- for (const [key, value] of Object.entries(metadata)) {
- try {
- metadata[key] = JSON.parse(value);
- } catch (e) {
- // Do nothing
- }
- }
-
- return metadata;
-}
diff --git a/worker/src/services/IngestionService/tests/utils.unit.test.ts b/worker/src/services/IngestionService/tests/utils.unit.test.ts
index a9b0660d68..36b1ed7739 100644
--- a/worker/src/services/IngestionService/tests/utils.unit.test.ts
+++ b/worker/src/services/IngestionService/tests/utils.unit.test.ts
@@ -35,9 +35,9 @@ describe("overwriteObject", () => {
it("should merge metadata correctly", () => {
const result = overwriteObject(objA, objB, []);
expect(result.metadata).toEqual({
- key1: '"value1"',
- key2: '"newValue2"',
- key3: '"value3"',
+ key1: "value1",
+ key2: "newValue2",
+ key3: "value3",
});
});
diff --git a/worker/src/services/IngestionService/utils.ts b/worker/src/services/IngestionService/utils.ts
index 25c3f67285..b51c1f5c7c 100644
--- a/worker/src/services/IngestionService/utils.ts
+++ b/worker/src/services/IngestionService/utils.ts
@@ -22,16 +22,16 @@ export const convertJsonSchemaToRecord = (
return record;
}
- // if it's an object, add each key value pair with a stringified value
if (typeof jsonSchema === "object") {
for (const key in jsonSchema) {
- record[key] = JSON.stringify(jsonSchema[key]);
+ const value = jsonSchema[key];
+ record[key] = typeof value === "string" ? value : JSON.stringify(value);
}
}
return record;
};
-export const mergeRecords = (
+const mergeRecords = (
record1?: Record<string, string>,
record2?: Record<string, string>,
): Record<string, string> | undefined => {