Skip to content

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

fix(trace-ui): properly render attached images (#12672)

Nimar3b20b1b0e91e
diff --git a/packages/shared/scripts/seeder/seed-media.ts b/packages/shared/scripts/seeder/seed-media.ts
index 3b1be2a60c..7f064ab72e 100644
--- a/packages/shared/scripts/seeder/seed-media.ts
+++ b/packages/shared/scripts/seeder/seed-media.ts
@@ -28,6 +28,16 @@ interface MediaFile {
filePath: string;
}
+export type SeedMediaKey = "image" | "pdf" | "audio";
+
+export interface SeedMediaFixture {
+ name: string;
+ contentType: string;
+ mediaId: string;
+ referenceString: string;
+ source: string;
+}
+
const MEDIA_FILES: Record<string, MediaFile> = {
image: {
name: "langfuse-logo.png",
@@ -49,6 +59,9 @@ const MEDIA_FILES: Record<string, MediaFile> = {
},
};
+const SEED_MEDIA_REFERENCE_SOURCE = "base64_data_uri";
+const seedMediaFixtureCache = new Map<SeedMediaKey, SeedMediaFixture | null>();
+
// Deterministic trace IDs for media test traces
export const MEDIA_TEST_TRACE_IDS = {
imageOnly: "seed-media-image-only",
@@ -56,6 +69,42 @@ export const MEDIA_TEST_TRACE_IDS = {
allTypesChatML: "seed-media-all-types-chatml",
} as const;
+export function getSeedMediaFixture(
+ mediaKey: SeedMediaKey,
+): SeedMediaFixture | null {
+ if (seedMediaFixtureCache.has(mediaKey)) {
+ return seedMediaFixtureCache.get(mediaKey) ?? null;
+ }
+
+ const mediaFile = MEDIA_FILES[mediaKey];
+
+ if (!fs.existsSync(mediaFile.filePath)) {
+ logger.warn(
+ `[seed-media] Test file not found: ${mediaFile.filePath}, media fixture unavailable`,
+ );
+ seedMediaFixtureCache.set(mediaKey, null);
+ return null;
+ }
+
+ const fileBytes = fs.readFileSync(mediaFile.filePath);
+ const sha256Hash = crypto
+ .createHash("sha256")
+ .update(fileBytes)
+ .digest("base64");
+ const mediaId = getMediaIdFromHash(sha256Hash);
+ const fixture = {
+ name: mediaFile.name,
+ contentType: mediaFile.contentType,
+ mediaId,
+ source: SEED_MEDIA_REFERENCE_SOURCE,
+ referenceString: `@@@langfuseMedia:type=${mediaFile.contentType}|id=${mediaId}|source=${SEED_MEDIA_REFERENCE_SOURCE}@@@`,
+ };
+
+ seedMediaFixtureCache.set(mediaKey, fixture);
+
+ return fixture;
+}
+
/**
* Derive media ID from SHA256 hash (first 22 chars, URL-safe)
*/
diff --git a/packages/shared/scripts/seeder/utils/seeder-orchestrator.ts b/packages/shared/scripts/seeder/utils/seeder-orchestrator.ts
index 1a891f6d66..f39f6c7c22 100644
--- a/packages/shared/scripts/seeder/utils/seeder-orchestrator.ts
+++ b/packages/shared/scripts/seeder/utils/seeder-orchestrator.ts
@@ -7,7 +7,7 @@ import { DataGenerator } from "./data-generators";
import { ClickHouseQueryBuilder } from "./clickhouse-builder";
import { FrameworkTraceLoader } from "./framework-traces/framework-trace-loader";
import { EVAL_TRACE_COUNT, SEED_DATASETS } from "./postgres-seed-constants";
-import { MEDIA_TEST_TRACE_IDS } from "../seed-media";
+import { MEDIA_TEST_TRACE_IDS, getSeedMediaFixture } from "../seed-media";
import {
clickhouseClient,
createTrace,
@@ -464,6 +464,23 @@ export class SeederOrchestrator {
);
const now = Date.now();
+ const imageFixture = getSeedMediaFixture("image");
+ const pdfFixture = getSeedMediaFixture("pdf");
+ const audioFixture = getSeedMediaFixture("audio");
+
+ const getMediaMetadata = (
+ field: "input" | "output" | "metadata",
+ fixture: ReturnType<typeof getSeedMediaFixture>,
+ ): Record<string, string> =>
+ fixture
+ ? {
+ [`${field}_media_id`]: fixture.mediaId,
+ [`${field}_media_content_type`]: fixture.contentType,
+ [`${field}_media_reference_string`]: fixture.referenceString,
+ }
+ : {
+ [`${field}_media_status`]: "missing-seed-media-fixture",
+ };
for (const projectId of projectIds) {
logger.info(`Processing media test traces for project ${projectId}`);
@@ -475,12 +492,38 @@ export class SeederOrchestrator {
project_id: projectId,
name: "Media Test: Image Only",
timestamp: now,
- input: JSON.stringify({
- message: "This trace has an image attachment in input",
- description: "Used for testing media rendering in JSON Beta view",
- }),
- output: JSON.stringify({ status: "success" }),
- metadata: { test_type: "media", media_types: "image" },
+ input: JSON.stringify([
+ {
+ role: "user",
+ content: imageFixture
+ ? [
+ {
+ type: "text",
+ text: "Please analyze the seeded image attachment.",
+ },
+ {
+ type: "image_url",
+ image_url: {
+ url: imageFixture.referenceString,
+ },
+ },
+ ]
+ : "Please analyze the seeded image attachment. The media fixture is missing.",
+ },
+ ]),
+ output: JSON.stringify([
+ {
+ role: "assistant",
+ content:
+ "This trace is meant to render one inline seeded image in the input and expose its metadata on the trace.",
+ },
+ ]),
+ metadata: {
+ test_type: "media",
+ media_types: "image",
+ render_mode: "chatml-inline-image",
+ ...getMediaMetadata("input", imageFixture),
+ },
tags: ["media-test", "image"],
environment: "default",
}),
@@ -491,18 +534,36 @@ export class SeederOrchestrator {
name: "Media Test: All Types",
timestamp: now + 1000,
input: JSON.stringify({
- message: "This trace has an image in input",
- description: "Testing image attachment",
+ message: "This trace has an image attachment in input",
+ description: "Testing trace-level input attachment metadata",
+ attachment: imageFixture
+ ? {
+ mediaId: imageFixture.mediaId,
+ contentType: imageFixture.contentType,
+ referenceString: imageFixture.referenceString,
+ }
+ : "seed media fixture missing",
}),
output: JSON.stringify({
- message: "This trace has a PDF in output",
- description: "Testing PDF attachment",
+ message: "This trace has a PDF attachment in output",
+ description: "Testing trace-level output attachment metadata",
+ attachment: pdfFixture
+ ? {
+ mediaId: pdfFixture.mediaId,
+ contentType: pdfFixture.contentType,
+ referenceString: pdfFixture.referenceString,
+ }
+ : "seed media fixture missing",
}),
metadata: {
- message: "This trace has audio in metadata",
- description: "Testing audio attachment",
+ message: "This trace has an audio attachment in metadata",
+ description: "Testing trace-level metadata attachment metadata",
test_type: "media",
media_types: "image,pdf,audio",
+ render_mode: "json-with-attachment-metadata",
+ ...getMediaMetadata("input", imageFixture),
+ ...getMediaMetadata("output", pdfFixture),
+ ...getMediaMetadata("metadata", audioFixture),
},
tags: ["media-test", "all-types"],
environment: "default",
@@ -521,23 +582,41 @@ export class SeederOrchestrator {
},
{
role: "user",
- content:
- "Please analyze the attached image and describe what you see.",
+ content: imageFixture
+ ? [
+ {
+ type: "text",
+ text: "Please analyze the attached seeded image and describe what you see.",
+ },
+ {
+ type: "image_url",
+ image_url: {
+ url: imageFixture.referenceString,
+ },
+ },
+ ]
+ : "Please analyze the attached seeded image and describe what you see. The media fixture is missing.",
},
]),
output: JSON.stringify([
{
role: "assistant",
content:
- "I can see the Langfuse logo in the image. It appears to be a modern, clean design with distinctive branding elements. The attached PDF contains additional documentation about the Bitcoin whitepaper.",
+ "I can see the Langfuse logo in the image. The trace also includes a PDF attachment on the output field and an audio attachment on the metadata field. Check the trace metadata for the deterministic media IDs and reference strings.",
},
]),
metadata: {
- message: "This trace has audio in metadata",
- description: "Testing audio attachment with ChatML format",
+ message:
+ "This trace has audio in metadata and explicit media metadata for every field",
+ description:
+ "Testing ChatML media rendering with deterministic media references",
test_type: "media",
media_types: "image,pdf,audio",
format: "chatml",
+ render_mode: "chatml-inline-image-plus-trace-metadata",
+ ...getMediaMetadata("input", imageFixture),
+ ...getMediaMetadata("output", pdfFixture),
+ ...getMediaMetadata("metadata", audioFixture),
},
tags: ["media-test", "all-types", "chatml"],
environment: "default",
diff --git a/web/src/components/ui/LangfuseMediaView.tsx b/web/src/components/ui/LangfuseMediaView.tsx
index 5ee2a476bc..90840e014e 100644
--- a/web/src/components/ui/LangfuseMediaView.tsx
+++ b/web/src/components/ui/LangfuseMediaView.tsx
@@ -1,4 +1,7 @@
import { api } from "@/src/utils/api";
+import { cn } from "@/src/utils/tailwind";
+import { useState } from "react";
+import { Button } from "@/src/components/ui/button";
import { ImageOff } from "lucide-react";
import {
@@ -11,7 +14,13 @@ import {
type MediaContentType,
type MediaReturnType,
} from "@/src/features/media/validation";
-import { File, Image as ImageIcon, Volume2 } from "lucide-react";
+import {
+ ExternalLink,
+ File,
+ Image as ImageIcon,
+ Video,
+ Volume2,
+} from "lucide-react";
export const LangfuseMediaView = ({
mediaReferenceString,
@@ -104,6 +113,8 @@ function FileViewer({
src?: string;
contentType: MediaContentType;
}) {
+ const [isExpanded, setIsExpanded] = useState(false);
+
if (!src) return null;
const mimeType = String(contentType);
@@ -111,35 +122,93 @@ function FileViewer({
const fileName = src.split("/").pop()?.split("?")[0] || "";
const fileType = mimeType.split("/")[0];
const fileExtension = mimeType.split("/")[1]?.toUpperCase() || "FILE";
+ const isImage = fileType === "image";
+ const isAudio = fileType === "audio";
+ const isVideo = fileType === "video";
+ const isPreviewable = isImage || isAudio || isVideo;
const openInNewTab = () => {
window.open(src, "_blank", "noopener,noreferrer");
};
+ const iconTile = (
+ <div className="flex flex-col items-center gap-2">
+ {isImage ? (
+ <ImageIcon className="h-5 w-5 transition-transform group-hover:scale-110" />
+ ) : isAudio ? (
+ <Volume2 className="h-5 w-5 transition-transform group-hover:scale-110" />
+ ) : isVideo ? (
+ <Video className="h-5 w-5 transition-transform group-hover:scale-110" />
+ ) : (
+ <File className="h-5 w-5 transition-transform group-hover:scale-110" />
+ )}
+
+ <div className="flex flex-col items-center gap-1">
+ <span className="text-sm font-medium">{fileExtension}</span>
+ <span className="text-muted-foreground text-xs">
+ {fileName.length > 5 ? `${fileName.slice(0, 5)}...` : fileName}
+ </span>
+ </div>
+ </div>
+ );
+
+ const previewContent = isImage ? (
+ <ResizableImage
+ src={src}
+ alt={fileName}
+ isDefaultVisible={true}
+ shouldValidateImageSource={false}
+ />
+ ) : isAudio ? (
+ <AudioPlayer src={src} />
+ ) : isVideo ? (
+ <VideoPlayer src={src} />
+ ) : null;
+
return (
- <button
- onClick={openInNewTab}
- aria-label={`Open ${fileName} in new tab`}
- title={fileName}
- className="from-accent-light-green/30 to-muted hover:from-accent-light-green/40 hover:to-muted/90 dark:from-accent-dark-green/20 dark:to-muted dark:hover:from-accent-dark-green/30 group relative flex h-24 w-24 flex-col items-center justify-center gap-2 rounded-md border bg-linear-to-br px-2 transition-colors"
+ <div
+ className={cn(
+ "flex flex-col gap-2",
+ isPreviewable && isExpanded ? "basis-full" : "shrink-0",
+ )}
>
- <div className="flex flex-col items-center gap-2">
- {fileType === "image" ? (
- <ImageIcon className="h-5 w-5 transition-transform group-hover:scale-110" />
- ) : fileType === "audio" ? (
- <Volume2 className="h-5 w-5 transition-transform group-hover:scale-110" />
- ) : (
- <File className="h-5 w-5 transition-transform group-hover:scale-110" />
- )}
-
- <div className="flex flex-col items-center gap-1">
- <span className="text-sm font-medium">{fileExtension}</span>
- <span className="text-muted-foreground text-xs">
- {fileName.length > 5 ? `${fileName.slice(0, 5)}...` : fileName}
- </span>
+ {isPreviewable && isExpanded ? (
+ <div className="flex max-w-3xl items-start gap-2">
+ <div className="min-w-0 flex-1">
+ {isAudio ? (
+ <div className="max-w-xl min-w-72">{previewContent}</div>
+ ) : (
+ previewContent
+ )}
+ </div>
+ <Button
+ type="button"
+ variant="outline"
+ size="icon-sm"
+ onClick={openInNewTab}
+ aria-label={`Open ${fileName} in new tab`}
+ title={`Open ${fileName} in new tab`}
+ className="shrink-0"
+ >
+ <ExternalLink className="h-4 w-4" />
+ </Button>
</div>
- </div>
- </button>
+ ) : (
+ <button
+ onClick={() => (isPreviewable ? setIsExpanded(true) : openInNewTab())}
+ aria-label={
+ isPreviewable
+ ? `Show ${fileName} inline`
+ : `Open ${fileName} in new tab`
+ }
+ aria-expanded={isPreviewable ? isExpanded : undefined}
+ title={fileName}
+ className="from-accent-light-green/30 to-muted hover:from-accent-light-green/40 hover:to-muted/90 dark:from-accent-dark-green/20 dark:to-muted dark:hover:from-accent-dark-green/30 group relative flex h-24 w-24 flex-col items-center justify-center gap-2 rounded-md border bg-linear-to-br px-2 transition-colors"
+ >
+ {iconTile}
+ </button>
+ )}
+ </div>
);
}
diff --git a/web/src/components/ui/resizable-image.tsx b/web/src/components/ui/resizable-image.tsx
index f99e2b2740..1386166a17 100644
--- a/web/src/components/ui/resizable-image.tsx
+++ b/web/src/components/ui/resizable-image.tsx
@@ -8,6 +8,7 @@ import { api } from "@/src/utils/api";
import { Skeleton } from "@/src/components/ui/skeleton";
import { captureException } from "@sentry/nextjs";
import { useSession } from "next-auth/react";
+import { buildResizableImageSrc } from "./resizable-image.utils";
/**
* Implemented customLoader as we cannot whitelist user provided image domains
@@ -22,11 +23,7 @@ const customLoader = ({
src: string;
width: number;
quality?: number;
-}) => {
- if (!width || !quality) return src;
- const separator = src.includes("?") ? "&" : "?";
- return `${src}${separator}w=${width}&q=${quality || 75}`;
-};
+}) => buildResizableImageSrc({ src, width, quality });
const ImageErrorDisplay = ({
src,
diff --git a/web/src/components/ui/resizable-image.utils.ts b/web/src/components/ui/resizable-image.utils.ts
new file mode 100644
index 0000000000..c4e22f82fd
--- /dev/null
+++ b/web/src/components/ui/resizable-image.utils.ts
@@ -0,0 +1,15 @@
+export const buildResizableImageSrc = ({
+ src,
+ width,
+ quality,
+}: {
+ src: string;
+ width: number;
+ quality?: number;
+}) => {
+ if (!width || !quality) return src;
+
+ const separator = src.includes("?") ? "&" : "?";
+
+ return `${src}${separator}w=${width}&q=${quality || 75}`;
+};