Skip to content

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

feat(web): make markdown render character limit configurable at container runtime (#16166)

David Trainabc4f4d80a948
diff --git a/.env.prod.example b/.env.prod.example
index 57680fec41..59b685b0d8 100644
--- a/.env.prod.example
+++ b/.env.prod.example
@@ -33,8 +33,8 @@ ENCRYPTION_KEY="0000000000000000000000000000000000000000000000000000000000000000
# 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
+# Works at runtime with the pre-built docker image.
+# LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT=150000
# Docker only, optional
# PORT=3000
diff --git a/docker-compose.build.yml b/docker-compose.build.yml
index 822f82b65f..e1391f962d 100644
--- a/docker-compose.build.yml
+++ b/docker-compose.build.yml
@@ -35,6 +35,7 @@ services:
LANGFUSE_INIT_USER_EMAIL: ${LANGFUSE_INIT_USER_EMAIL:-}
LANGFUSE_INIT_USER_NAME: ${LANGFUSE_INIT_USER_NAME:-}
LANGFUSE_INIT_USER_PASSWORD: ${LANGFUSE_INIT_USER_PASSWORD:-}
+ LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT: ${LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT:-}
CLICKHOUSE_MIGRATION_URL: ${CLICKHOUSE_MIGRATION_URL:-clickhouse://clickhouse:9000}
CLICKHOUSE_URL: ${CLICKHOUSE_URL:-http://clickhouse:8123}
CLICKHOUSE_USER: ${CLICKHOUSE_USER:-clickhouse}
diff --git a/docker-compose.yml b/docker-compose.yml
index 8657364684..2778f135d4 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -114,6 +114,7 @@ services:
LANGFUSE_INIT_USER_EMAIL: ${LANGFUSE_INIT_USER_EMAIL:-}
LANGFUSE_INIT_USER_NAME: ${LANGFUSE_INIT_USER_NAME:-}
LANGFUSE_INIT_USER_PASSWORD: ${LANGFUSE_INIT_USER_PASSWORD:-}
+ LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT: ${LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT:-}
# In-app agent settings.
LANGFUSE_IN_APP_AGENT_MAX_ACTIVE_RUNS_PER_USER: ${LANGFUSE_IN_APP_AGENT_MAX_ACTIVE_RUNS_PER_USER:-}
LANGFUSE_IN_APP_AGENT_MAX_ACTIVE_RUNS_PER_ORG: ${LANGFUSE_IN_APP_AGENT_MAX_ACTIVE_RUNS_PER_ORG:-}
diff --git a/web/Dockerfile b/web/Dockerfile
index f49a3d4dfc..cc0b12d628 100644
--- a/web/Dockerfile
+++ b/web/Dockerfile
@@ -98,8 +98,6 @@ ARG NEXT_PUBLIC_SENTRY_DSN
ENV NEXT_PUBLIC_SENTRY_DSN=$NEXT_PUBLIC_SENTRY_DSN
ARG NEXT_PUBLIC_BASE_PATH
ENV 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.tsx
index c6cc035a10..4d2947ad26 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 { env } from "@/src/env.mjs";
+import { useMarkdownRenderCharacterLimit } from "@/src/hooks/useMarkdownRenderCharacterLimit";
type MarkdownJsonViewHeaderProps = {
title: string | React.ReactNode;
@@ -76,11 +76,11 @@ export function MarkdownJsonViewHeader({
*/
export const canRenderContentAsMarkdown = (
content: unknown,
+ characterLimit: number,
): content is z.input<typeof OpenAIContentSchema> =>
OpenAIContentSchema.safeParse(content).success &&
// Don't render if markdown content is huge
- JSON.stringify(content || {}).length <=
- env.NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT;
+ JSON.stringify(content || {}).length <= characterLimit;
// 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 globally
@@ -108,11 +108,15 @@ export function MarkdownJsonView({
the title can carry a message `name` instead of the role). */
isSystemPrompt?: boolean;
}) {
+ const characterLimit = useMarkdownRenderCharacterLimit();
// Boxed so a renderable `null` content stays distinguishable from
// "not renderable as markdown", without re-widening the narrowed type.
const markdownContent = useMemo(
- () => (canRenderContentAsMarkdown(content) ? { value: content } : null),
- [content],
+ () =>
+ canRenderContentAsMarkdown(content, characterLimit)
+ ? { value: content }
+ : null,
+ [content, characterLimit],
);
return (
diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsx
index 997a23d32c..0226407fb9 100644
--- a/web/src/components/ui/MarkdownViewer.tsx
+++ b/web/src/components/ui/MarkdownViewer.tsx
@@ -58,6 +58,7 @@ import {
getStandaloneMediaReferenceStrings,
} from "@/src/components/ui/markdown-media.utils";
import { exceedsMarkdownRenderLimits } from "@/src/components/ui/markdown-render-limits";
+import { useMarkdownRenderCharacterLimit } from "@/src/hooks/useMarkdownRenderCharacterLimit";
type ReactMarkdownNode = ReactMarkdownExtraProps["node"];
type ReactMarkdownNodeChildren = Exclude<
@@ -399,14 +400,15 @@ function MarkdownRenderer({
className?: string;
}) {
const promptReferenceProjectId = usePromptReferenceProjectId();
+ const characterLimit = useMarkdownRenderCharacterLimit();
// Guard against payloads that would overflow the JS call stack while
// react-markdown recursively walks the parsed tree (crashes Firefox, whose
// stack is much smaller than Chrome's). Rendered as plain text instead.
// See markdown-render-limits.ts for the mechanism.
const tooLargeOrDeep = useMemo(
- () => exceedsMarkdownRenderLimits(markdown),
- [markdown],
+ () => exceedsMarkdownRenderLimits(markdown, characterLimit),
+ [markdown, characterLimit],
);
if (tooLargeOrDeep) {
diff --git a/web/src/components/ui/PrettyJsonView.tsx b/web/src/components/ui/PrettyJsonView.tsx
index dfe5a5fe09..7a1d0913fb 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 { env } from "@/src/env.mjs";
+import { useMarkdownRenderCharacterLimit } from "@/src/hooks/useMarkdownRenderCharacterLimit";
import {
convertRowIdToKeyPath,
getRowChildren,
@@ -180,12 +180,15 @@ function isChatMLFormat(json: unknown): boolean {
return false;
}
-function isMarkdownContent(json: unknown): {
+function isMarkdownContent(
+ json: unknown,
+ characterLimit: number,
+): {
isMarkdown: boolean;
content?: string;
} {
const contentSize = JSON.stringify(json || {}).length;
- if (contentSize > env.NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT) {
+ if (contentSize > characterLimit) {
return { isMarkdown: false };
}
@@ -908,14 +911,15 @@ export function PrettyJsonView(props: {
useState<LangfuseExpandedState>({});
const isChatML = useMemo(() => isChatMLFormat(parsedJson), [parsedJson]);
+ const characterLimit = useMarkdownRenderCharacterLimit();
const { isMarkdown, content: markdownContent } = useMemo(
// Skip the markdown probe for gated large strings: isMarkdownContent runs
// `JSON.stringify` on the whole value, an O(n) pass over the multi-MB string.
() =>
largeStringValue !== null
? { isMarkdown: false as const, content: undefined }
- : isMarkdownContent(parsedJson),
- [parsedJson, largeStringValue],
+ : isMarkdownContent(parsedJson, characterLimit),
+ [parsedJson, largeStringValue, characterLimit],
);
const baseTableData = useMemo(() => {
diff --git a/web/src/components/ui/markdown-render-limits.clienttest.ts b/web/src/components/ui/markdown-render-limits.clienttest.ts
index 5a10ec77c4..a149dbf0ab 100644
--- a/web/src/components/ui/markdown-render-limits.clienttest.ts
+++ b/web/src/components/ui/markdown-render-limits.clienttest.ts
@@ -1,10 +1,13 @@
import {
MARKDOWN_MAX_NESTING_DEPTH,
- MARKDOWN_MAX_RENDER_BYTES,
estimateMarkdownNestingDepth,
exceedsMarkdownRenderLimits,
} from "@/src/components/ui/markdown-render-limits";
+const DEFAULT_CHARACTER_LIMIT = 150_000;
+const exceedsDefaultLimits = (content: string) =>
+ exceedsMarkdownRenderLimits(content, DEFAULT_CHARACTER_LIMIT);
+
describe("estimateMarkdownNestingDepth", () => {
it("returns 0 for plain text", () => {
expect(estimateMarkdownNestingDepth("just a plain sentence")).toBe(0);
@@ -70,54 +73,40 @@ 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();
+describe("exceedsMarkdownRenderLimits", () => {
+ it("allows normal content", () => {
+ expect(exceedsDefaultLimits("Hello **world**")).toBe(false);
+ expect(exceedsDefaultLimits("- a\n - b\n - c")).toBe(false);
});
- 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("flags content over the size preempt", () => {
+ expect(exceedsDefaultLimits("a".repeat(DEFAULT_CHARACTER_LIMIT + 1))).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);
- expect(exceedsMarkdownRenderLimits("- a\n - b\n - c")).toBe(false);
+ it("follows a raised character limit", () => {
+ expect(exceedsMarkdownRenderLimits("a".repeat(200_000), 300_000)).toBe(
+ false,
+ );
+ expect(exceedsMarkdownRenderLimits("a".repeat(300_001), 300_000)).toBe(
+ true,
+ );
});
- it("flags content over the size preempt", () => {
- expect(
- exceedsMarkdownRenderLimits("a".repeat(MARKDOWN_MAX_RENDER_BYTES + 1)),
- ).toBe(true);
+ it("never drops the size preempt below the 150k default", () => {
+ expect(exceedsMarkdownRenderLimits("a".repeat(100_000), 10)).toBe(false);
});
it("flags deeply nested content even when small", () => {
// A few KB of nested blockquotes overflows the parser/renderer in Firefox.
- expect(exceedsMarkdownRenderLimits("> ".repeat(2000) + "boom")).toBe(true);
- expect(exceedsMarkdownRenderLimits("*".repeat(2000))).toBe(true);
+ expect(exceedsDefaultLimits("> ".repeat(2000) + "boom")).toBe(true);
+ expect(exceedsDefaultLimits("*".repeat(2000))).toBe(true);
});
it("does not flag large-but-shallow markdown under the size cap", () => {
const shallow = "word ".repeat(10_000); // ~50KB, depth 0
- expect(shallow.length).toBeLessThan(MARKDOWN_MAX_RENDER_BYTES);
- expect(exceedsMarkdownRenderLimits(shallow)).toBe(false);
+ expect(shallow.length).toBeLessThan(DEFAULT_CHARACTER_LIMIT);
+ expect(exceedsDefaultLimits(shallow)).toBe(false);
});
});
diff --git a/web/src/components/ui/markdown-render-limits.ts b/web/src/components/ui/markdown-render-limits.ts
index ed42b03793..37bda1d1db 100644
--- a/web/src/components/ui/markdown-render-limits.ts
+++ b/web/src/components/ui/markdown-render-limits.ts
@@ -1,5 +1,3 @@
-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
@@ -17,23 +15,11 @@ import { env } from "@/src/env.mjs";
// overflows on the same pathological input. So the depth check below is a single
// pass, non-recursive scan that can never itself overflow.
-// Cheap preempt: individual strings larger than this are rendered as plain text
-// 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-overflowing
// payload needs hundreds+, so this threshold sits in a wide safe band.
export const MARKDOWN_MAX_NESTING_DEPTH = 100;
+export const DEFAULT_MARKDOWN_RENDER_CHARACTER_LIMIT = 150_000;
const isListBullet = (char: string): boolean =>
char === "-" || char === "+" || char === "*";
@@ -132,11 +118,25 @@ export function estimateMarkdownNestingDepth(content: string): number {
}
/**
- * Whether `content` should be rendered as plain text rather than parsed as GFM
- * markdown, to avoid stack-overflow crashes on pathologically large or deeply
- * nested payloads. See the module header for the mechanism.
+ * Whether `content` should be rendered as plain text rather than parsed as
+ * GFM markdown, to avoid stack-overflow crashes on pathologically large or
+ * deeply nested payloads (see the module header).
+ *
+ * The byte preempt follows `characterLimit` (the configured markdown render
+ * limit, see `useMarkdownRenderCharacterLimit`) floored at its 150_000
+ * default, so raising the limit also raises this backstop for surfaces
+ * without a caller-side gate (e.g. comments). The depth check remains the
+ * stack-overflow guard.
*/
-export function exceedsMarkdownRenderLimits(content: string): boolean {
- if (content.length > MARKDOWN_MAX_RENDER_BYTES) return true;
+export function exceedsMarkdownRenderLimits(
+ content: string,
+ characterLimit: number,
+): boolean {
+ if (
+ content.length >
+ Math.max(DEFAULT_MARKDOWN_RENDER_CHARACTER_LIMIT, characterLimit)
+ ) {
+ return true;
+ }
return estimateMarkdownNestingDepth(content) > MARKDOWN_MAX_NESTING_DEPTH;
}
diff --git a/web/src/env.mjs b/web/src/env.mjs
index 2092b3254d..3595dd1f7e 100644
--- a/web/src/env.mjs
+++ b/web/src/env.mjs
@@ -511,6 +511,15 @@ export const env = createEnv({
.enum(["legacy", "dual", "events_only"])
.default("events_only"),
+ // Character count above which trace/observation I/O is rendered as plain
+ // text instead of markdown. Served to the browser via the public tRPC
+ // router, so it works as a runtime env var on prebuilt Docker images.
+ LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT: z.coerce
+ .number()
+ .int()
+ .positive()
+ .default(150_000),
+
// Background-migration env gates. Mirror worker/src/env.ts (names, defaults)
// so the background-migrations status endpoint can tell dormant, env-gated
// rows apart from migrations the worker will actually pick up. The worker
@@ -606,13 +615,6 @@ 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),
},
/**
@@ -941,9 +943,6 @@ export const env = createEnv({
// Playground
NEXT_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 License
LANGFUSE_EE_LICENSE_KEY: process.env.LANGFUSE_EE_LICENSE_KEY,
ADMIN_API_KEY: process.env.ADMIN_API_KEY,
@@ -1020,6 +1019,8 @@ export const env = createEnv({
process.env.LANGFUSE_MIGRATION_V4_ALLOW_PREVIEW_OPT_IN,
LANGFUSE_MIGRATION_V4_WRITE_MODE:
process.env.LANGFUSE_MIGRATION_V4_WRITE_MODE,
+ LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT:
+ process.env.LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT,
LANGFUSE_BACKGROUND_MIGRATION_V4_ENABLE_HISTORIC_BACKFILL:
process.env.LANGFUSE_BACKGROUND_MIGRATION_V4_ENABLE_HISTORIC_BACKFILL,
LANGFUSE_BACKGROUND_MIGRATION_V4_DROP_PID_TID_SORTING_TABLES:
diff --git a/web/src/features/traces/components/ChatMessageList.clienttest.tsx b/web/src/features/traces/components/ChatMessageList.clienttest.tsx
new file mode 100644
index 0000000000..1399758c45
--- /dev/null
+++ b/web/src/features/traces/components/ChatMessageList.clienttest.tsx
@@ -0,0 +1,81 @@
+import { render, screen } from "@testing-library/react";
+import { vi } from "vitest";
+
+// The dedup under test lives in ChatMessageList; stub the per-message renderer
+// and the media strip to observe only which media survive the filter.
+vi.mock(
+ "@/src/features/traces/components/IOPreview/components/ChatMessage",
+ () => ({
+ ChatMessage: () => null,
+ }),
+);
+vi.mock(
+ "@/src/features/traces/components/IOPreview/components/SectionMedia",
+ () => ({
+ SectionMedia: ({ media }: { media: { mediaId: string }[] }) => (
+ <div
+ data-testid="section-media"
+ data-media-ids={media.map((m) => m.mediaId).join(",")}
+ />
+ ),
+ }),
+);
+
+import { ChatMessageList } from "@/src/features/traces/components/ChatMessageList";
+import { type ChatMlMessage } from "@/src/features/traces/fns/chatMessageUtils";
+import { type MediaReturnType } from "@/src/features/media/validation";
+
+// Pin the limit so the test does not depend on the ambient .env value.
+const { TEST_LIMIT } = vi.hoisted(() => ({ TEST_LIMIT: 1_000 }));
+vi.mock("@/src/hooks/useMarkdownRenderCharacterLimit", () => ({
+ useMarkdownRenderCharacterLimit: () => TEST_LIMIT,
+}));
+
+const MEDIA_REF = "@@@langfuseMedia:type=image/png|id=media-1|source=base64@@@";
+const AUDIO_REF = "@@@langfuseMedia:type=audio/wav|id=audio-1|source=base64@@@";
+
+const renderList = (message: ChatMlMessage, mediaIds: string[]) =>
+ render(
+ <ChatMessageList
+ messages={[message]}
+ shouldRenderMarkdown={true}
+ media={mediaIds.map((mediaId) => ({ mediaId }) as MediaReturnType)}
+ currentView="pretty"
+ messageToToolCallNumbers={new Map()}
+ />,
+ );
+
+describe("ChatMessageList media dedup", () => {
+ it("filters media that the markdown path renders inline", () => {
+ renderList({ role: "user", content: MEDIA_REF } as ChatMlMessage, [
+ "media-1",
+ ]);
+
+ expect(screen.queryByTestId("section-media")).not.toBeInTheDocument();
+ });
+
+ it("keeps referenced media and audio when over-limit content falls back to JSON", () => {
+ renderList(
+ {
+ role: "assistant",
+ content: MEDIA_REF.repeat(
+ Math.ceil((TEST_LIMIT + 1) / MEDIA_REF.length),
+ ),
+ audio: {
+ data: {
+ type: "base64",
+ id: "audio-1",
+ source: "",
+ referenceString: AUDIO_REF,
+ },
+ },
+ } as ChatMlMessage,
+ ["media-1", "audio-1"],
+ );
+
+ expect(screen.getByTestId("section-media")).toHaveAttribute(
+ "data-media-ids",
+ "media-1,audio-1",
+ );
+ });
+});
diff --git a/web/src/features/traces/components/ChatMessageList.tsx b/web/src/features/traces/components/ChatMessageList.tsx
index 17fa0e56de..750483e4cc 100644
--- a/web/src/features/traces/components/ChatMessageList.tsx
+++ b/web/src/features/traces/components/ChatMessageList.tsx
@@ -6,6 +6,7 @@ import {
getRenderedInlineMediaIds,
} from "@/src/components/ui/markdown-media.utils";
import { canRenderContentAsMarkdown } from "@/src/components/ui/MarkdownJsonView";
+import { useMarkdownRenderCharacterLimit } from "@/src/hooks/useMarkdownRenderCharacterLimit";
import {
ChatMessage,
type ViewMode,
@@ -88,6 +89,8 @@ export function ChatMessageList({
[isCollapsed, messagesToRender],
);
+ const characterLimit = useMarkdownRenderCharacterLimit();
+
const remainingMedia = useMemo(() => {
if (!shouldRenderMarkdown) {
return media ?? [];
@@ -96,29 +99,21 @@ export function ChatMessageList({
const renderedMediaIds = new Set<string>();
visibleMessages.forEach(({ message }) => {
- const content = message.content;
+ const content = message.content || "";
// Only content that ChatMessage renders through MarkdownView shows its
// media inline; anything falling back to a JSON table still needs the
// shared strip, so ask the renderer's own predicate.
- if (typeof content === "string" || canRenderContentAsMarkdown(content)) {
+ if (canRenderContentAsMarkdown(content, characterLimit)) {
getRenderedInlineMediaIds({
markdown: content,
audio: message.audio,
}).forEach((mediaId) => renderedMediaIds.add(mediaId));
- return;
- }
-
- if (message.audio) {
- getRenderedInlineMediaIds({
- markdown: "",
- audio: message.audio,
- }).forEach((mediaId) => renderedMediaIds.add(mediaId));
}
});
return filterAlreadyRenderedMedia(media, renderedMediaIds);
- }, [media, shouldRenderMarkdown, visibleMessages]);
+ }, [media, shouldRenderMarkdown, visibleMessages, characterLimit]);
return (
<div className="flex max-h-full min-h-0 flex-col gap-2">
diff --git a/web/src/features/traces/components/IOPreview/IOPreviewPretty.tsx b/web/src/features/traces/components/IOPreview/IOPreviewPretty.tsx
index c3c86dd38c..1fe7a6636a 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 { env } from "@/src/env.mjs";
+import { useMarkdownRenderCharacterLimit } from "@/src/hooks/useMarkdownRenderCharacterLimit";
import { type MediaReturnType } from "@/src/features/media/validation";
import {
type ChatMLParserResult,
@@ -202,6 +202,8 @@ export function IOPreviewPretty({
chatMLParserResult,
);
+ const characterLimit = useMarkdownRenderCharacterLimit();
+
// Determine if markdown is safe to render (content size check)
const shouldRenderMarkdown = useMemo(() => {
// Fast byte estimation without expensive JSON.stringify
@@ -233,11 +235,10 @@ export function IOPreviewPretty({
const messagesSize = estimateSize(allMessages);
const totalSize = inputSize + outputSize + messagesSize;
- const shouldRender =
- totalSize <= env.NEXT_PUBLIC_LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT;
+ const shouldRender = totalSize <= characterLimit;
return shouldRender;
- }, [parsedInput, parsedOutput, allMessages]);
+ }, [parsedInput, parsedOutput, allMessages, characterLimit]);
// Prepare additional input (only if non-empty)
const additionalInputToShow = useMemo(() => {
diff --git a/web/src/hooks/useMarkdownRenderCharacterLimit.clienttest.tsx b/web/src/hooks/useMarkdownRenderCharacterLimit.clienttest.tsx
new file mode 100644
index 0000000000..5a78b3ba98
--- /dev/null
+++ b/web/src/hooks/useMarkdownRenderCharacterLimit.clienttest.tsx
@@ -0,0 +1,58 @@
+import { renderHook } from "@testing-library/react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import { DEFAULT_MARKDOWN_RENDER_CHARACTER_LIMIT } from "@/src/components/ui/markdown-render-limits";
+import {
+ MarkdownRenderCharacterLimitProvider,
+ useMarkdownRenderCharacterLimit,
+} from "@/src/hooks/useMarkdownRenderCharacterLimit";
+
+// vi.hoisted so it exists before the hoisted vi.mock factories run.
+const { mockMarkdownRenderConfigQuery } = vi.hoisted(() => ({
+ mockMarkdownRenderConfigQuery: vi.fn(),
+}));
+
+vi.mock("@/src/utils/api", () => ({
+ api: {
+ public: {
+ markdownRenderConfig: {
+ useQuery: mockMarkdownRenderConfigQuery,
+ },
+ },
+ },
+}));
+
+const renderWithProvider = () =>
+ renderHook(() => useMarkdownRenderCharacterLimit(), {
+ wrapper: MarkdownRenderCharacterLimitProvider,
+ }).result.current;
+
+describe("useMarkdownRenderCharacterLimit", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("returns the server-configured limit once the provider fetched it", () => {
+ mockMarkdownRenderConfigQuery.mockReturnValue({
+ data: { characterLimit: 500_000 },
+ });
+ expect(renderWithProvider()).toBe(500_000);
+ });
+
+ it("uses the default while the query has no data and disables retries", () => {
+ mockMarkdownRenderConfigQuery.mockReturnValue({
+ data: undefined,
+ });
+ expect(renderWithProvider()).toBe(DEFAULT_MARKDOWN_RENDER_CHARACTER_LIMIT);
+ expect(mockMarkdownRenderConfigQuery).toHaveBeenCalledWith(
+ undefined,
+ expect.objectContaining({ retry: false }),
+ );
+ });
+
+ it("uses the default without a provider (stories, tests)", () => {
+ const { result } = renderHook(() => useMarkdownRenderCharacterLimit());
+ expect(result.current).toBe(DEFAULT_MARKDOWN_RENDER_CHARACTER_LIMIT);
+ expect(mockMarkdownRenderConfigQuery).not.toHaveBeenCalled();
+ });
+});
diff --git a/web/src/hooks/useMarkdownRenderCharacterLimit.tsx b/web/src/hooks/useMarkdownRenderCharacterLimit.tsx
new file mode 100644
index 0000000000..a739399996
--- /dev/null
+++ b/web/src/hooks/useMarkdownRenderCharacterLimit.tsx
@@ -0,0 +1,45 @@
+import { createContext, useContext, type ReactNode } from "react";
+
+import { DEFAULT_MARKDOWN_RENDER_CHARACTER_LIMIT } from "@/src/components/ui/markdown-render-limits";
+import { api } from "@/src/utils/api";
+
+const MarkdownRenderCharacterLimitContext = createContext<number>(
+ DEFAULT_MARKDOWN_RENDER_CHARACTER_LIMIT,
+);
+
+/**
+ * Provides the character count above which trace/observation I/O is rendered
+ * as plain text instead of markdown. A context (fetched once, in _app) rather
+ * than a per-consumer query keeps story-covered components free of tRPC
+ * fetches; consumers use the default until the runtime server value is fetched.
+ */
+export function MarkdownRenderCharacterLimitProvider({
+ children,
+}: {
+ children: ReactNode;
+}) {
+ const config = api.public.markdownRenderConfig.useQuery(undefined, {
+ staleTime: Infinity,
+ // The local default applies if the request fails. Do not retry this
+ // optional app-shell config query or refetch it during the session.
+ retry: false,
+ refetchOnMount: false,
+ refetchOnWindowFocus: false,
+ refetchOnReconnect: false,
+ throwOnError: false,
+ });
+
+ return (
+ <MarkdownRenderCharacterLimitContext.Provider
+ value={
+ config.data?.characterLimit ?? DEFAULT_MARKDOWN_RENDER_CHARACTER_LIMIT
+ }
+ >
+ {children}
+ </MarkdownRenderCharacterLimitContext.Provider>
+ );
+}
+
+export function useMarkdownRenderCharacterLimit(): number {
+ return useContext(MarkdownRenderCharacterLimitContext);
+}
diff --git a/web/src/pages/_app.tsx b/web/src/pages/_app.tsx
index a32aa54233..6d026f107a 100644
--- a/web/src/pages/_app.tsx
+++ b/web/src/pages/_app.tsx
@@ -84,6 +84,7 @@ import { DetailPageListsProvider } from "@/src/features/navigate-detail-pages/co
import { env } from "@/src/env.mjs";
import { ThemeProvider } from "@/src/features/theming/ThemeProvider";
import { MarkdownContextProvider } from "@/src/features/theming/useMarkdownContext";
+import { MarkdownRenderCharacterLimitProvider } from "@/src/hooks/useMarkdownRenderCharacterLimit";
import { SupportDrawerProvider } from "@/src/features/support-chat/SupportDrawerProvider";
import { V4MigrationPanelProvider } from "@/src/features/v4-migration/V4MigrationPanelProvider";
import { InAppAiAgentProvider } from "@/src/features/in-app-agent/components/InAppAiAgentProvider";
@@ -182,27 +183,29 @@ const MyApp: AppType<{ session: Session | null }> = ({
<ResilientSessionProvider basePath={authBasePath}>
<DetailPageListsProvider>
<MarkdownContextProvider>
- <ThemeProvider
- attribute="class"
- enableSystem
- disableTransitionOnChange
- >
- <ScoreCacheProvider>
- <CorrectionCacheProvider>
- <SupportDrawerProvider defaultOpen={false}>
- <V4MigrationPanelProvider defaultOpen={false}>
- <InAppAiAgentProvider defaultOpen={false}>
- {skipAppLayout ? (
- page
- ) : (
- <AppLayout>{page}</AppLayout>
- )}
- </InAppAiAgentProvider>
- </V4MigrationPanelProvider>
- </SupportDrawerProvider>
- </CorrectionCacheProvider>
- </ScoreCacheProvider>
- </ThemeProvider>
+ <MarkdownRenderCharacterLimitProvider>
+ <ThemeProvider
+ attribute="class"
+ enableSystem
+ disableTransitionOnChange
+ >
+ <ScoreCacheProvider>
+ <CorrectionCacheProvider>
+ <SupportDrawerProvider defaultOpen={false}>
+ <V4MigrationPanelProvider defaultOpen={false}>
+ <InAppAiAgentProvider defaultOpen={false}>
+ {skipAppLayout ? (
+ page
+ ) : (
+ <AppLayout>{page}</AppLayout>
+ )}
+ </InAppAiAgentProvider>
+ </V4MigrationPanelProvider>
+ </SupportDrawerProvider>
+ </CorrectionCacheProvider>
+ </ScoreCacheProvider>
+ </ThemeProvider>
+ </MarkdownRenderCharacterLimitProvider>
</MarkdownContextProvider>
</DetailPageListsProvider>
</ResilientSessionProvider>
diff --git a/web/src/server/api/routers/public.ts b/web/src/server/api/routers/public.ts
index b26a217cf0..82ea52ff36 100644
--- a/web/src/server/api/routers/public.ts
+++ b/web/src/server/api/routers/public.ts
@@ -21,6 +21,9 @@ export const publicRouter = createTRPCRouter({
traceReadConfig: publicProcedure.query(() => ({
v4WriteMode: env.LANGFUSE_MIGRATION_V4_WRITE_MODE,
})),
+ markdownRenderConfig: publicProcedure.query(() => ({
+ characterLimit: env.LANGFUSE_MARKDOWN_RENDER_CHARACTER_LIMIT,
+ })),
tracingSearchConfig: protectedProjectProcedure
.input(z.object({ projectId: z.string() }))
.query(() => ({