Skip to content

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

chore: skip zod parse for json parsing (#5063)

Steffen Schmitz6a6abc6bd8ae
diff --git a/packages/shared/src/utils/json.ts b/packages/shared/src/utils/json.ts
index b7e45eb2fe..b481b86243 100644
--- a/packages/shared/src/utils/json.ts
+++ b/packages/shared/src/utils/json.ts
@@ -1,6 +1,6 @@
import { z } from "zod";
import { merge } from "lodash";
-import { JsonNested, jsonSchema, jsonSchemaNullable } from "./zod";
+import { JsonNested, jsonSchema } from "./zod";
/**
* Deeply parses a JSON string or object for nested stringified JSON
@@ -52,13 +52,11 @@ export const mergeJson = (
export const parseJsonPrioritised = (
json: string,
-): z.infer<typeof jsonSchema> | string | undefined => {
+): JsonNested | string | undefined => {
try {
- const parsedJson = JSON.parse(json);
- return jsonSchema.parse(parsedJson);
+ return JSON.parse(json);
} catch (error) {
- const parsed = jsonSchema.safeParse(json);
- return parsed.success ? parsed.data : json;
+ return json;
}
};
diff --git a/web/src/__tests__/zod.servertest.ts b/web/src/__tests__/zod.servertest.ts
index 02f155e559..583a3d03c9 100644
--- a/web/src/__tests__/zod.servertest.ts
+++ b/web/src/__tests__/zod.servertest.ts
@@ -37,9 +37,10 @@ describe("parseJsonPrioritised", () => {
["42", 42], // Number
["true", true], // Boolean true
["false", false], // Boolean false
- ["null", "null"], // Null
+ ["null", null], // Null
["", ""], // Empty string
["invalid{json}", "invalid{json}"], // Invalid JSON string
+ ['{"hello": "world"', '{"hello": "world"'], // Invalid JSON string
['[null, 123, "abc"]', [null, 123, "abc"]], // Mixed array
[
'{"array": [1, 2], "nested": {"key": "value"}}',
diff --git a/web/src/features/public-api/server/observations.ts b/web/src/features/public-api/server/observations.ts
index 9ce9b304d4..623deea265 100644
--- a/web/src/features/public-api/server/observations.ts
+++ b/web/src/features/public-api/server/observations.ts
@@ -4,7 +4,6 @@ import {
type ObservationRecordReadType,
queryClickhouse,
convertObservationToView,
- queryClickhouseStream,
} from "@langfuse/shared/src/server";
type QueryType = {
@@ -67,7 +66,7 @@ export const generateObservationsForPublicApi = async (props: QueryType) => {
${props.limit !== undefined && props.page !== undefined ? `LIMIT {limit: Int32} OFFSET {offset: Int32}` : ""}
`;
- const asyncGenerator = queryClickhouseStream<ObservationRecordReadType>({
+ const result = await queryClickhouse<ObservationRecordReadType>({
query,
params: {
...appliedFilter.params,
@@ -78,10 +77,6 @@ export const generateObservationsForPublicApi = async (props: QueryType) => {
: {}),
},
});
- const result: Array<ObservationRecordReadType> = [];
- for await (const row of asyncGenerator) {
- result.push(row);
- }
return result.map(convertObservationToView);
};
diff --git a/web/src/features/public-api/server/traces.ts b/web/src/features/public-api/server/traces.ts
index 4402f562b0..ec60aeb24b 100644
--- a/web/src/features/public-api/server/traces.ts
+++ b/web/src/features/public-api/server/traces.ts
@@ -6,7 +6,6 @@ import {
orderByToClickhouseSql,
type DateTimeFilter,
parseClickhouseUTCDateTimeFormat,
- queryClickhouseStream,
} from "@langfuse/shared/src/server";
import {
convertRecordToJsonSchema,
@@ -109,7 +108,7 @@ export const generateTracesForPublicApi = async (
${props.limit !== undefined && props.page !== undefined ? `LIMIT {limit: Int32} OFFSET {offset: Int32}` : ""}
`;
- const asyncGenerator = queryClickhouseStream<
+ const result = await queryClickhouse<
Trace & {
observations: string[];
scores: string[];
@@ -134,19 +133,6 @@ export const generateTracesForPublicApi = async (
},
});
- const result: Array<
- Trace & {
- observations: string[];
- scores: string[];
- totalCost: number;
- latency: number;
- htmlPath: string;
- }
- > = [];
- for await (const row of asyncGenerator) {
- result.push(row);
- }
-
return result.map((trace) => ({
...trace,
timestamp: parseClickhouseUTCDateTimeFormat(trace.timestamp.toString()),