Skip to content

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

fix(trace-ui): render tagged images inline (#12681)

Nimar0690b07c4dd4
diff --git a/web/src/components/ui/MarkdownViewer.tsx b/web/src/components/ui/MarkdownViewer.tsx
index af050198a2..ca88590733 100644
--- a/web/src/components/ui/MarkdownViewer.tsx
+++ b/web/src/components/ui/MarkdownViewer.tsx
@@ -47,6 +47,11 @@ import {
PromptReferenceButton,
usePromptReferenceProjectId,
} from "@/src/components/ui/PromptReferences";
+import {
+ filterAlreadyRenderedMedia,
+ getRenderedInlineMediaIds,
+ getStandaloneMediaReferenceStrings,
+} from "@/src/components/ui/markdown-media.utils";
type ReactMarkdownNode = ReactMarkdownExtraProps["node"];
type ReactMarkdownNodeChildren = Exclude<
@@ -489,6 +494,15 @@ export function MarkdownView({
});
};
+ const inlineMediaReferenceStrings =
+ typeof markdown === "string"
+ ? getStandaloneMediaReferenceStrings(markdown)
+ : [];
+ const remainingMedia = filterAlreadyRenderedMedia(
+ media,
+ getRenderedInlineMediaIds({ markdown, audio }),
+ );
+
return (
<div className={cn("overflow-hidden")} key={theme}>
{title ? (
@@ -518,27 +532,38 @@ export function MarkdownView({
>
{typeof markdown === "string" ? (
// plain string
- <>
- <MarkdownRenderer
- markdown={
- shouldBeCollapsible && isCollapsed ? truncatedContent : markdown
- }
- theme={theme}
- customCodeHeaderClassName={customCodeHeaderClassName}
- />
- {shouldBeCollapsible && (
- <Button
- variant="ghost"
- size="xs"
- onClick={toggleCollapsed}
- className="w-fit text-xs underline"
- >
- {isCollapsed
- ? "Expand system prompt"
- : "Collapse system prompt"}
- </Button>
- )}
- </>
+ inlineMediaReferenceStrings.length > 0 ? (
+ inlineMediaReferenceStrings.map((referenceString, index) => (
+ <LangfuseMediaView
+ key={`${referenceString}-${index}`}
+ mediaReferenceString={referenceString}
+ />
+ ))
+ ) : (
+ <>
+ <MarkdownRenderer
+ markdown={
+ shouldBeCollapsible && isCollapsed
+ ? truncatedContent
+ : markdown
+ }
+ theme={theme}
+ customCodeHeaderClassName={customCodeHeaderClassName}
+ />
+ {shouldBeCollapsible && (
+ <Button
+ variant="ghost"
+ size="xs"
+ onClick={toggleCollapsed}
+ className="w-fit text-xs underline"
+ >
+ {isCollapsed
+ ? "Expand system prompt"
+ : "Collapse system prompt"}
+ </Button>
+ )}
+ </>
+ )
) : (
// content parts (multi-modal)
(markdown ?? []).map((content, index) =>
@@ -589,13 +614,13 @@ export function MarkdownView({
</>
) : null}
</div>
- {media && media.length > 0 && (
+ {remainingMedia.length > 0 && (
<>
<div className="text-muted-foreground mx-3 border-t px-2 py-1 text-xs">
Media
</div>
<div className="flex flex-wrap gap-2 p-4 pt-1">
- {media.map((m) => (
+ {remainingMedia.map((m) => (
<LangfuseMediaView
mediaAPIReturnValue={m}
asFileIcon={true}
diff --git a/web/src/components/ui/markdown-media.utils.ts b/web/src/components/ui/markdown-media.utils.ts
new file mode 100644
index 0000000000..d83056e4c0
--- /dev/null
+++ b/web/src/components/ui/markdown-media.utils.ts
@@ -0,0 +1,86 @@
+import {
+ MediaReferenceStringSchema,
+ isOpenAIImageContentPart,
+ type OpenAIContentSchema,
+ type OpenAIOutputAudioType,
+ type ParsedMediaReferenceType,
+} from "@langfuse/shared";
+import { type MediaReturnType } from "@/src/features/media/validation";
+import { type z } from "zod/v4";
+
+const MEDIA_REFERENCE_PATTERN = /@@@langfuseMedia:[\s\S]*?@@@/g;
+
+const getMediaReferenceId = (
+ value: string | ParsedMediaReferenceType | null | undefined,
+): string | null => {
+ if (!value) return null;
+
+ if (typeof value === "string") {
+ const parsedReference = MediaReferenceStringSchema.safeParse(value);
+ return parsedReference.success ? parsedReference.data.id : null;
+ }
+
+ return typeof value.id === "string" ? value.id : null;
+};
+
+export const getStandaloneMediaReferenceStrings = (value: string): string[] => {
+ const matches = value.match(MEDIA_REFERENCE_PATTERN) ?? [];
+ if (matches.length === 0) return [];
+
+ const remainingText = value.replace(MEDIA_REFERENCE_PATTERN, "").trim();
+ if (remainingText.length > 0) return [];
+
+ const parsedMatches = matches.map((match) =>
+ MediaReferenceStringSchema.safeParse(match),
+ );
+
+ return parsedMatches.every((result) => result.success) ? matches : [];
+};
+
+export const getRenderedInlineMediaIds = ({
+ markdown,
+ audio,
+}: {
+ markdown: string | z.infer<typeof OpenAIContentSchema>;
+ audio?: OpenAIOutputAudioType;
+}): Set<string> => {
+ const mediaIds = new Set<string>();
+
+ if (typeof markdown === "string") {
+ getStandaloneMediaReferenceStrings(markdown).forEach((referenceString) => {
+ const mediaId = getMediaReferenceId(referenceString);
+ if (mediaId) {
+ mediaIds.add(mediaId);
+ }
+ });
+ } else {
+ (markdown ?? []).forEach((content) => {
+ if (isOpenAIImageContentPart(content)) {
+ const mediaId = getMediaReferenceId(content.image_url.url);
+ if (mediaId) {
+ mediaIds.add(mediaId);
+ }
+ } else if (content.type === "input_audio") {
+ const mediaId = getMediaReferenceId(content.input_audio.data);
+ if (mediaId) {
+ mediaIds.add(mediaId);
+ }
+ }
+ });
+ }
+
+ if (audio) {
+ const mediaId = getMediaReferenceId(audio.data.referenceString);
+ if (mediaId) {
+ mediaIds.add(mediaId);
+ }
+ }
+
+ return mediaIds;
+};
+
+export const filterAlreadyRenderedMedia = (
+ media: MediaReturnType[] | undefined,
+ renderedMediaIds: Set<string>,
+): MediaReturnType[] =>
+ (media ?? []).filter((item) => !renderedMediaIds.has(item.mediaId));