Skip to content

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

fix(trace): decode unicode escapes in trace JSON download (#14667)

Yuto Toya407433eb98f4
diff --git a/packages/shared/src/server/utils/transforms/stringify.ts b/packages/shared/src/server/utils/transforms/stringify.ts
index 261b26dda6..4fad83feaf 100644
--- a/packages/shared/src/server/utils/transforms/stringify.ts
+++ b/packages/shared/src/server/utils/transforms/stringify.ts
@@ -1,20 +1,21 @@
import { decodeUnicodeEscapesOnly } from "../../../utils/unicode";
-export const stringify = (data: any, key?: string): string => {
+// Replacer that keeps bigints JSON-serializable and decodes \uXXXX escapes so
+// that non-ASCII content (e.g. Japanese ingested with Python ensure_ascii=True)
+// renders as real characters instead of escape sequences.
+const stringifyReplacer = (_key: string, value: unknown) => {
+ if (typeof value === "bigint") return Number.parseInt(value.toString());
+ if (typeof value === "string") return decodeUnicodeEscapesOnly(value, true);
+ return value;
+};
+
+export const stringify = (data: any, key?: string, indent?: number): string => {
// For comment fields, use pretty-print formatting for better readability
- // Other fields use compact format to reduce file size
- const indent = key === "comments" ? 2 : undefined;
+ // Other fields use compact format to reduce file size, unless an explicit
+ // indent is requested by the caller (e.g. human-readable trace downloads).
+ const resolvedIndent = indent ?? (key === "comments" ? 2 : undefined);
- return JSON.stringify(
- data,
- (k, value) => {
- if (typeof value === "bigint") return Number.parseInt(value.toString());
- if (typeof value === "string")
- return decodeUnicodeEscapesOnly(value, true);
- return value;
- },
- indent,
- );
+ return JSON.stringify(data, stringifyReplacer, resolvedIndent);
};
/**
diff --git a/web/src/pages/api/traces/[traceId]/download.ts b/web/src/pages/api/traces/[traceId]/download.ts
index a2a012dac8..5327d41df4 100644
--- a/web/src/pages/api/traces/[traceId]/download.ts
+++ b/web/src/pages/api/traces/[traceId]/download.ts
@@ -1,4 +1,5 @@
import { InvalidRequestError, UnauthorizedError } from "@langfuse/shared";
+import { stringify } from "@langfuse/shared/src/server";
import { withMiddlewares } from "@/src/features/public-api/server/withMiddlewares";
import {
buildTraceExport,
@@ -67,6 +68,10 @@ export default withMiddlewares({
`attachment; filename="trace-export.json"; filename*=UTF-8''${encodeURIComponent(downloadFilename)}`,
);
- return res.status(200).send(JSON.stringify(payload, null, 2));
+ // Use the shared stringify helper (not the raw JSON.stringify) so that
+ // \uXXXX escapes in string fields (e.g. Japanese ingested with Python
+ // ensure_ascii=True) are decoded to real characters. Pass indent=2 to keep
+ // the download human-readable, matching the previous pretty-printed output.
+ return res.status(200).send(stringify(payload, undefined, 2));
},
});
diff --git a/worker/src/__tests__/stringify.test.ts b/worker/src/__tests__/stringify.test.ts
index 117899fdab..1368e2176e 100644
--- a/worker/src/__tests__/stringify.test.ts
+++ b/worker/src/__tests__/stringify.test.ts
@@ -29,6 +29,27 @@ describe("stringify", () => {
const parsed = JSON.parse(result);
expect(parsed.text).toBe('line1\\nline2\\t"quoted"');
});
+
+ it("applies an explicit indent when provided", () => {
+ const data = { text: "hello" };
+ const result = stringify(data, undefined, 2);
+ expect(result).toContain("\n");
+ expect(result).toContain(' "text": "hello"');
+ });
+
+ it("decodes unicode escapes while keeping an explicit indent (trace download)", () => {
+ // Mirrors the trace-download payload shape: input is a stringified JSON
+ // blob whose Japanese characters were escaped by Python ensure_ascii=True.
+ const data = {
+ observations: [
+ { input: '{"text": "\\u3053\\u3093\\u306b\\u3061\\u306f"}' },
+ ],
+ };
+ const result = stringify(data, undefined, 2);
+ expect(result).toContain("\n"); // pretty-printed
+ expect(result).toContain("こんにちは"); // decoded, not こ...
+ expect(result).not.toContain("\\u3053");
+ });
});
describe("stringifyForCsv", () => {