🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
feat(web): make markdown render character limit configurable (#15965)
David Trainabbbd75bf91d8
diff --git a/.env.prod.example b/.env.prod.exampleindex 846a7fd5c0..41f31fb6c1 100644--- a/.env.prod.example+++ b/.env.prod.example@@ -32,6 +32,10 @@ ENCRYPTION_KEY="0000000000000000000000000000000000000000000000000000000000000000# Note: You need to build the docker image with the base path set and cannot use the pre-built docker image if you set this.# NEXT_PUBLIC_BASE_PATH="/app"+# Character count above which trace/observation I/O is rendered as plain text instead of markdown, optional+# Note: You need to build the docker image with this set and cannot use the pre-built docker image if you set this.+# NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT=150000+# Docker only, optional# PORT=3000# HOSTNAME=localhostdiff --git a/web/Dockerfile b/web/Dockerfileindex 4f27ebe725..d8f380565a 100644--- a/web/Dockerfile+++ b/web/Dockerfile@@ -98,6 +98,8 @@ ARG NEXT_PUBLIC_SENTRY_DSNENV NEXT_PUBLIC_SENTRY_DSN=$NEXT_PUBLIC_SENTRY_DSNARG NEXT_PUBLIC_BASE_PATHENV NEXT_PUBLIC_BASE_PATH=$NEXT_PUBLIC_BASE_PATH+ARG NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT+ENV NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT=$NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT# Sentry already needs to be set on build time to upload sourcemaps# This must not be set for OSS releases as we would share our Sentry secret.diff --git a/web/src/components/ui/MarkdownJsonView.tsx b/web/src/components/ui/MarkdownJsonView.tsxindex d4d4307676..c6ac153e85 100644--- a/web/src/components/ui/MarkdownJsonView.tsx+++ b/web/src/components/ui/MarkdownJsonView.tsx@@ -10,7 +10,7 @@ import { type MediaReturnType } from "@/src/features/media/validation";import { Check, Copy } from "lucide-react";import { useMemo, useState } from "react";import { type z } from "zod";-import { MARKDOWN_RENDER_CHARACTER_LIMIT } from "@/src/utils/constants";+import { env } from "@/src/env.mjs";type MarkdownJsonViewHeaderProps = {title: string | React.ReactNode;@@ -77,7 +77,8 @@ export const canRenderContentAsMarkdown = (): content is z.input<typeof OpenAIContentSchema> =>OpenAIContentSchema.safeParse(content).success &&// Don't render if markdown content is huge- JSON.stringify(content || {}).length <= MARKDOWN_RENDER_CHARACTER_LIMIT;+ JSON.stringify(content || {}).length <=+ env.NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT;// MarkdownJsonView will render markdown if `isMarkdownEnabled` (global context) is true and the content is valid markdown// otherwise, if content is valid markdown will render JSON with switch to enable markdown globallydiff --git a/web/src/components/ui/PrettyJsonView.tsx b/web/src/components/ui/PrettyJsonView.tsxindex 904fa0caf0..6b860d7cee 100644--- a/web/src/components/ui/PrettyJsonView.tsx+++ b/web/src/components/ui/PrettyJsonView.tsx@@ -48,7 +48,7 @@ import {StringOrMarkdownSchema,containsAnyMarkdown,} from "@/src/components/schemas/MarkdownSchema";-import { MARKDOWN_RENDER_CHARACTER_LIMIT } from "@/src/utils/constants";+import { env } from "@/src/env.mjs";import {convertRowIdToKeyPath,getRowChildren,@@ -185,7 +185,7 @@ function isMarkdownContent(json: unknown): {content?: string;} {const contentSize = JSON.stringify(json || {}).length;- if (contentSize > MARKDOWN_RENDER_CHARACTER_LIMIT) {+ if (contentSize > env.NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT) {return { isMarkdown: false };}diff --git a/web/src/components/ui/markdown-render-limits.clienttest.ts b/web/src/components/ui/markdown-render-limits.clienttest.tsindex 63fba49964..5a10ec77c4 100644--- a/web/src/components/ui/markdown-render-limits.clienttest.ts+++ b/web/src/components/ui/markdown-render-limits.clienttest.ts@@ -70,6 +70,33 @@ describe("estimateMarkdownNestingDepth", () => {});});+describe("MARKDOWN_MAX_RENDER_BYTES derivation", () => {+ const importWithCharacterLimit = async (limit: number) => {+ vi.resetModules();+ vi.doMock("@/src/env.mjs", () => ({+ env: { NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT: limit },+ }));+ return await import("@/src/components/ui/markdown-render-limits");+ };++ afterEach(() => {+ vi.doUnmock("@/src/env.mjs");+ vi.resetModules();+ });++ it("follows a raised character limit", async () => {+ const mod = await importWithCharacterLimit(300_000);+ expect(mod.MARKDOWN_MAX_RENDER_BYTES).toBe(300_000);+ expect(mod.exceedsMarkdownRenderLimits("a".repeat(200_000))).toBe(false);+ expect(mod.exceedsMarkdownRenderLimits("a".repeat(300_001))).toBe(true);+ });++ it("never drops below the 150k default", async () => {+ const mod = await importWithCharacterLimit(10);+ expect(mod.MARKDOWN_MAX_RENDER_BYTES).toBe(150_000);+ });+});+describe("exceedsMarkdownRenderLimits", () => {it("allows normal content", () => {expect(exceedsMarkdownRenderLimits("Hello **world**")).toBe(false);diff --git a/web/src/components/ui/markdown-render-limits.ts b/web/src/components/ui/markdown-render-limits.tsindex 7e779a9d0b..ed42b03793 100644--- a/web/src/components/ui/markdown-render-limits.ts+++ b/web/src/components/ui/markdown-render-limits.ts@@ -1,3 +1,5 @@+import { env } from "@/src/env.mjs";+// Safety limits for rendering user/model content as GFM markdown.//// react-markdown parses content to an mdast tree and then walks it recursively@@ -16,10 +18,17 @@// pass, non-recursive scan that can never itself overflow.// Cheap preempt: individual strings larger than this are rendered as plain text-// instead of markdown. Mirrors MARKDOWN_RENDER_CHARACTER_LIMIT (the caller-side-// input+output+messages gate) as a per-string backstop for surfaces that do not-// apply that gate (e.g. comments), and avoids scanning/rendering huge blobs.-export const MARKDOWN_MAX_RENDER_BYTES = 150_000;+// instead of markdown. Follows the configurable+// NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT (the caller-side+// input+output+messages gate), floored at its 150_000 default, so raising the+// limit also raises this per-string backstop for surfaces that do not apply+// that gate (e.g. comments). Raising it is safe: flat byte size never+// overflows the stack (see above); the fixed depth check below is the+// overflow guard.+export const MARKDOWN_MAX_RENDER_BYTES = Math.max(+ 150_000,+ env.NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT,+);// Content whose estimated markdown nesting depth exceeds this is rendered as// plain text. Legitimate markdown nests <20-30 levels deep; a stack-overflowingdiff --git a/web/src/env.mjs b/web/src/env.mjsindex 497d75fb39..ecbf5a6066 100644--- a/web/src/env.mjs+++ b/web/src/env.mjs@@ -598,6 +598,13 @@ export const env = createEnv({.enum(["true", "false"]).optional().default("true"),+ // Content larger than this is rendered as plain text instead of markdown,+ // as react-markdown is too slow for large payloads.+ NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT: z.coerce+ .number()+ .int()+ .positive()+ .default(150_000),},/**@@ -935,6 +942,9 @@ export const env = createEnv({// PlaygroundNEXT_PUBLIC_LANGFUSE_PLAYGROUND_STREAMING_ENABLED_DEFAULT:process.env.NEXT_PUBLIC_LANGFUSE_PLAYGROUND_STREAMING_ENABLED_DEFAULT,+ // Markdown rendering+ NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT:+ process.env.NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT,// EE LicenseLANGFUSE_EE_LICENSE_KEY: process.env.LANGFUSE_EE_LICENSE_KEY,ADMIN_API_KEY: process.env.ADMIN_API_KEY,diff --git a/web/src/features/traces/components/IOPreview/IOPreviewPretty.tsx b/web/src/features/traces/components/IOPreview/IOPreviewPretty.tsxindex cde50bf792..c3c86dd38c 100644--- a/web/src/features/traces/components/IOPreview/IOPreviewPretty.tsx+++ b/web/src/features/traces/components/IOPreview/IOPreviewPretty.tsx@@ -2,7 +2,7 @@ import { useMemo } from "react";import { type Prisma, type ScoreDomain, deepParseJson } from "@langfuse/shared";import { PrettyJsonView } from "@/src/components/ui/PrettyJsonView";import { type MetadataFilterActions } from "@/src/components/table/ValueCell";-import { MARKDOWN_RENDER_CHARACTER_LIMIT } from "@/src/utils/constants";+import { env } from "@/src/env.mjs";import { type MediaReturnType } from "@/src/features/media/validation";import {type ChatMLParserResult,@@ -233,7 +233,8 @@ export function IOPreviewPretty({const messagesSize = estimateSize(allMessages);const totalSize = inputSize + outputSize + messagesSize;- const shouldRender = totalSize <= MARKDOWN_RENDER_CHARACTER_LIMIT;+ const shouldRender =+ totalSize <= env.NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT;return shouldRender;}, [parsedInput, parsedOutput, allMessages]);diff --git a/web/src/utils/constants.ts b/web/src/utils/constants.tsdeleted file mode 100644index f5a6260bb2..0000000000--- a/web/src/utils/constants.ts+++ /dev/null@@ -1,3 +0,0 @@-// Don't render markdown in the frontend if the content exceeds the character count-// react-markdown is unfortunately too slow to bear this for now.-export const MARKDOWN_RENDER_CHARACTER_LIMIT = 150000;