Skip to content

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

fix(otel): store number-like metadata strings without their JSON quotes (#19076)

Niklas Semmler067c33c41db6
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.metadataDropped.test.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.metadataDropped.test.ts
index 5f2e7f1d15..132fc4db96 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.metadataDropped.test.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.metadataDropped.test.ts
@@ -45,10 +45,6 @@ import {
} from "./OtelIngestionProcessor";
import * as serverBarrel from "../index";
import { flattenJsonToPathArrays } from "./utils";
-import {
- metadataArraysToRecord,
- parseMetadataCHRecordToDomain,
-} from "../utils/metadata_conversion";
const METRIC = "langfuse.ingestion.metadata_dropped";
const ARRAY_ATTRIBUTE_DROPPED_METRIC =
@@ -540,18 +536,15 @@ describe("per-key metadata values from structured-metadata SDK majors", () => {
);
expect(fromBlob).toMatchObject(metadata);
- // Strings the metadata read path would parse as JSON keep their
- // encoding, so they read back as strings. A blob cannot tell them apart.
- const expected = { ...fromBlob, digits: '"42"', version: '"1.0"' };
- expect(fromPerKey).toEqual(expected);
+ expect(fromPerKey).toEqual(fromBlob);
expect(flattenJsonToPathArrays(fromPerKey)).toEqual(
- flattenJsonToPathArrays(expected),
+ flattenJsonToPathArrays(fromBlob),
);
},
);
it.each(["v3", "v4"] as const)(
- "reads per-key values back with the type they were sent with (%s)",
+ "stores per-key string values as sent, even when they look like JSON (%s)",
async (path) => {
const sent = {
digits: "42",
@@ -562,9 +555,6 @@ describe("per-key metadata values from structured-metadata SDK majors", () => {
nullish: "null",
label: "",
environment: "prod",
- retries: 3,
- enabled: true,
- steps: [1, "two"],
};
const stored = await processMetadata(
path,
@@ -574,14 +564,8 @@ describe("per-key metadata values from structured-metadata SDK majors", () => {
})),
);
- const { names, values } = flattenJsonToPathArrays(stored);
- const read = parseMetadataCHRecordToDomain(
- metadataArraysToRecord(names, values as string[]) ?? {},
- );
-
- expect(read).toMatchObject(sent);
- // Plain strings are stored raw, so string metadata filters match them.
- expect(stored).toMatchObject({ environment: "prod", label: "" });
+ // String metadata filters compare against the stored value.
+ expect(stored).toMatchObject(sent);
},
);
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index 71c806d200..791ca88de9 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -43,7 +43,6 @@ import { OtelIngestionQueue } from "../redis/otelIngestionQueue";
import { isValidDateString, flattenJsonToPathArrays } from "./utils";
import { convertDateToClickhouseDateTime } from "../clickhouse/client";
import { isNullOrUndefined } from "../../utils/isNullOrUndefined";
-import { safeJsonParse } from "../../utils/json";
import { defineOwnValue } from "../../utils/objects";
export const AI_GATEWAY_INSTRUMENTATION_SCOPE_NAME = "langfuse-ai-gateway";
@@ -3706,14 +3705,7 @@ export class OtelIngestionProcessor {
// undecodable value (e.g. cut by an attribute length limit) is kept as
// sent so its content is not lost.
try {
- const decoded = JSON.parse(value);
- // Strings are stored raw and metadata reads JSON-parse every stored
- // value, so a string such as "42" or '{"a":1}' would read back as a
- // number or object. Keeping its encoding preserves the string type.
- metadata[metadataKey] =
- typeof decoded === "string" && safeJsonParse(decoded) !== undefined
- ? value
- : decoded;
+ metadata[metadataKey] = JSON.parse(value);
} catch {
metadata[metadataKey] = value;
recordIncrement("langfuse.ingestion.metadata_undecoded", 1, {
diff --git a/worker/src/__tests__/otelReplay.integration.test.ts b/worker/src/__tests__/otelReplay.integration.test.ts
index 0b824cbe1d..bd84f0f18e 100644
--- a/worker/src/__tests__/otelReplay.integration.test.ts
+++ b/worker/src/__tests__/otelReplay.integration.test.ts
@@ -725,16 +725,13 @@ describe(
imageBytes.toString("base64"),
"@@@langfuseMedia:type=image/png|id=overflow-media-id|source=bytes@@@",
);
- // A string value that looks like JSON keeps its per-key encoding.
- // The reference path does not scan inside that encoding and leaves
- // its media inline; only the early path extracts it.
- const expandedJsonString = JSON.stringify(
- JSON.stringify({ image: expandedImage }),
- );
+ // The TS text scan misses escaped MIME separators and bare base64
+ // provider data; only native extraction handles them.
+ const expandedJsonString = JSON.stringify({ image: expandedImage });
const earlyExpandedValues = {
json_string: expandedJsonString,
escaped_json_string: expandedJsonString,
- quoted_provider: JSON.stringify(expandedProvider),
+ quoted_provider: expandedProvider,
};
expectSdkMetadataExpansion(
comparison,
@@ -764,11 +761,9 @@ describe(
? "@@@langfuseMedia:type=image/png|id=overflow-media-id|source=base64_data_uri@@@"
: dataUri;
const inlineJsonLookingValues = {
- json_string: JSON.stringify(structuredMetadata.jsonString),
- escaped_json_string: JSON.stringify(
- structuredMetadata.escapedJsonString,
- ),
- quoted_provider: JSON.stringify(structuredMetadata.providerJson),
+ json_string: JSON.stringify({ image: expectedImage }),
+ escaped_json_string: structuredMetadata.escapedJsonString,
+ quoted_provider: structuredMetadata.providerJson,
};
expect(comparison.originalTs.storedRows).toHaveLength(1);
const originalMetadata = Object.fromEntries(