Skip to content

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

fix: safely parse big numbers in api responses (#6136)

Steffen Schmitz129805ebde4c
diff --git a/packages/shared/package.json b/packages/shared/package.json
index 24485413b5..e8a374c305 100644
--- a/packages/shared/package.json
+++ b/packages/shared/package.json
@@ -84,6 +84,7 @@
"langchain": "^0.3.15",
"langfuse-langchain": "3.30.3",
"lodash": "^4.17.21",
+ "lossless-json": "^4.0.2",
"next-auth": "^4.24.11",
"nodemailer": "^6.9.15",
"prisma-extension-kysely": "^2.1.0",
diff --git a/packages/shared/src/utils/json.ts b/packages/shared/src/utils/json.ts
index 22a6cc28d1..28e10c0f10 100644
--- a/packages/shared/src/utils/json.ts
+++ b/packages/shared/src/utils/json.ts
@@ -1,4 +1,5 @@
import { JsonNested } from "./zod";
+import { parse, isSafeNumber, isNumber } from "lossless-json";
/**
* Deeply parses a JSON string or object for nested stringified JSON
@@ -41,7 +42,18 @@ export const parseJsonPrioritised = (
json: string,
): JsonNested | string | undefined => {
try {
- return JSON.parse(json);
+ return parse(json, null, (value) => {
+ if (isNumber(value)) {
+ if (isSafeNumber(value)) {
+ // Safe numbers (integers and decimals) can be converted to Number
+ return Number(value.valueOf());
+ } else {
+ // For large integers beyond safe limits, preserve string representation
+ return value.toString();
+ }
+ }
+ return value;
+ }) as JsonNested;
} catch (error) {
return json;
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index d2e35c56b4..44aa139c75 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -224,6 +224,9 @@ importers:
lodash:
specifier: ^4.17.21
version: 4.17.21
+ lossless-json:
+ specifier: ^4.0.2
+ version: 4.0.2
next-auth:
specifier: ^4.24.11
@@ -8905,6 +8908,9 @@ packages:
resolution: {integrity: sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==}
hasBin: true
+ resolution: {integrity: sha512-+z0EaLi2UcWi8MZRxA5iTb6m4Ys4E80uftGY+yG5KNFJb5EceQXOhdW/pWJZ8m97s26u7yZZAYMcKWNztSZssA==}
+
resolution: {integrity: sha512-edNu/8D5MKVfGVFRhFf8aAxiTM6Wumfz5XsaatSxlD3w4R1d/WEKUTydCdPGbl9K7QG/Ca3GnDV2sIKIpXRQcw==}
@@ -22182,6 +22188,8 @@ snapshots:
dependencies:
js-tokens: 4.0.0
+
dependencies:
get-func-name: 2.0.2
diff --git a/web/src/__tests__/async/ingestion-api.servertest.ts b/web/src/__tests__/async/ingestion-api.servertest.ts
index d95c12919f..e77eeadba2 100644
--- a/web/src/__tests__/async/ingestion-api.servertest.ts
+++ b/web/src/__tests__/async/ingestion-api.servertest.ts
@@ -497,6 +497,88 @@ describe("/api/public/ingestion API Endpoint", () => {
});
});
+ it.each([
+ ["string", { testId: "this is a string metadata" }],
+ ["big-number", { testId: "1983516295378495150" }],
+ ["small-number", { testId: 5 }],
+ ["float-number", { testId: 5.5 }],
+ ])(
+ "#6123: should treat %s metadata for traces as such",
+ async (_type, metadataValue) => {
+ const traceId = randomUUID();
+
+ const entity = {
+ id: randomUUID(),
+ type: "trace-create",
+ timestamp: new Date().toISOString(),
+ body: {
+ id: traceId,
+ timestamp: new Date().toISOString(),
+ metadata: metadataValue,
+ },
+ };
+
+ const response = await makeAPICall("POST", "/api/public/ingestion", {
+ batch: [entity],
+ });
+
+ expect(response.status).toBe(207);
+
+ await waitForExpect(async () => {
+ const trace = await getTraceById(traceId, projectId);
+ expect(trace).toBeDefined();
+ expect(trace!.id).toBe(traceId);
+ expect(JSON.stringify(trace!.metadata)).toBe(
+ JSON.stringify(metadataValue),
+ );
+ });
+ },
+ );
+
+ it.each([
+ ["string", { testId: "this is a string metadata" }],
+ ["big-number", { testId: "1983516295378495150" }],
+ ["small-number", { testId: 5 }],
+ ["float-number", { testId: 5.5 }],
+ ])(
+ "#6123: should treat %s metadata for observations as such",
+ async (_type, metadataValue) => {
+ const observationId = randomUUID();
+ const traceId = randomUUID();
+
+ const entity = {
+ id: randomUUID(),
+ type: "span-create",
+ timestamp: new Date().toISOString(),
+ body: {
+ id: observationId,
+ traceId: traceId,
+ startTime: new Date().toISOString(),
+ metadata: metadataValue,
+ },
+ };
+
+ const response = await makeAPICall("POST", "/api/public/ingestion", {
+ batch: [entity],
+ });
+
+ expect(response.status).toBe(207);
+
+ await waitForExpect(async () => {
+ const observation = await getObservationById(
+ observationId,
+ projectId,
+ true,
+ );
+ expect(observation).toBeDefined();
+ expect(observation!.id).toBe(observationId);
+ expect(JSON.stringify(observation!.metadata)).toBe(
+ JSON.stringify(metadataValue),
+ );
+ });
+ },
+ );
+
it("#4900: should clear score comment on update with `null`", async () => {
const scoreId = randomUUID();
const score1 = {
diff --git a/web/src/__tests__/zod.servertest.ts b/web/src/__tests__/zod.servertest.ts
index 583a3d03c9..61f5542a46 100644
--- a/web/src/__tests__/zod.servertest.ts
+++ b/web/src/__tests__/zod.servertest.ts
@@ -46,6 +46,8 @@ describe("parseJsonPrioritised", () => {
'{"array": [1, 2], "nested": {"key": "value"}}',
{ array: [1, 2], nested: { key: "value" } },
], // Complex object
+ ["1983516295378495150", "1983516295378495150"], // Large number
+ ["3.4", 3.4], // Decimal number
])(
"should parse input correctly (%s, %s)",
(input: string, expectedOutput: any) => {