Skip to content

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

fix(prompts): prompts with many labels look good (#7731)

Nimar7598c95fc3c9
diff --git a/packages/shared/scripts/seeder/utils/postgres-seed-constants.ts b/packages/shared/scripts/seeder/utils/postgres-seed-constants.ts
index 7f39d78de5..c2f21da231 100644
--- a/packages/shared/scripts/seeder/utils/postgres-seed-constants.ts
+++ b/packages/shared/scripts/seeder/utils/postgres-seed-constants.ts
@@ -271,6 +271,68 @@ export const SEED_TEXT_PROMPTS = [
labels: ["production", "latest"],
tags: ["tag1", "tag2"],
},
+ {
+ id: `prompt-with-many-labels`,
+ createdBy: "user-1",
+ prompt:
+ "This is a comprehensive prompt for testing multiple label scenarios. It demonstrates how prompts can be tagged with numerous labels for organization, categorization, and filtering purposes. Use this prompt to understand how label management works at scale. Variables: {{input}}",
+ name: "prompt-with-many-labels",
+ version: 1,
+ labels: [
+ "production",
+ "latest",
+ "v1",
+ "v2",
+ "stable",
+ "beta",
+ "alpha",
+ "test",
+ "development",
+ "staging",
+ "experimental",
+ "feature",
+ "bugfix",
+ "hotfix",
+ "critical",
+ "high-priority",
+ "medium-priority",
+ "low-priority",
+ "urgent",
+ "customer-facing",
+ "internal",
+ "public",
+ "private",
+ "confidential",
+ "ai",
+ "nlp",
+ "chatbot",
+ "assistant",
+ "automation",
+ "ml",
+ "data",
+ "analytics",
+ "monitoring",
+ "logging",
+ "debug",
+ "performance",
+ "security",
+ "compliance",
+ "audit",
+ "review",
+ "approved",
+ "rejected",
+ "pending",
+ "archived",
+ "deprecated",
+ "legacy",
+ "migration",
+ "upgrade",
+ "downgrade",
+ "template",
+ "example",
+ ],
+ tags: [],
+ },
];
export const SEED_CHAT_ML_PROMPTS = [
diff --git a/web/src/components/TruncatedLabels.tsx b/web/src/components/TruncatedLabels.tsx
new file mode 100644
index 0000000000..1b1b6b495b
--- /dev/null
+++ b/web/src/components/TruncatedLabels.tsx
@@ -0,0 +1,117 @@
+import React from "react";
+import { Button } from "@/src/components/ui/button";
+import {
+ HoverCard,
+ HoverCardContent,
+ HoverCardTrigger,
+} from "@/src/components/ui/hover-card";
+import { StatusBadge } from "@/src/components/layouts/status-badge";
+import { PRODUCTION_LABEL, LATEST_PROMPT_LABEL } from "@langfuse/shared";
+import { cn } from "@/src/utils/tailwind";
+
+interface TruncatedLabelsProps {
+ labels: string[];
+ maxVisibleLabels?: number;
+ className?: string;
+ badgeClassName?: string;
+ showSimpleBadges?: boolean;
+}
+
+export function TruncatedLabels({
+ labels,
+ maxVisibleLabels = 5,
+ className,
+ badgeClassName,
+ showSimpleBadges = false,
+}: TruncatedLabelsProps) {
+ // Enhanced sorting: prioritize latest and production labels
+ const sortedLabels = [...labels].sort((a, b) => {
+ // Production label comes first
+ if (a === PRODUCTION_LABEL) return -1;
+ if (b === PRODUCTION_LABEL) return 1;
+
+ // Latest label comes second
+ if (a === LATEST_PROMPT_LABEL) return -1;
+ if (b === LATEST_PROMPT_LABEL) return 1;
+
+ // Then alphabetically
+ return a.localeCompare(b);
+ });
+
+ // Split labels into visible and hidden
+ const visibleLabels = sortedLabels.slice(0, maxVisibleLabels);
+ const hiddenLabels = sortedLabels.slice(maxVisibleLabels);
+ const hasHiddenLabels = hiddenLabels.length > 0;
+
+ if (sortedLabels.length === 0) {
+ return null;
+ }
+
+ return (
+ <div className={cn("flex flex-wrap gap-1", className)}>
+ {visibleLabels.map((label) =>
+ showSimpleBadges ? (
+ <div
+ key={label}
+ className={cn(
+ "max-h-fit min-h-6 w-fit content-center rounded-sm bg-secondary px-1 text-left text-xs font-semibold text-secondary-foreground",
+ badgeClassName,
+ )}
+ >
+ {label}
+ </div>
+ ) : (
+ <StatusBadge
+ type={label}
+ key={label}
+ className={cn("break-all sm:break-normal", badgeClassName)}
+ isLive={label === PRODUCTION_LABEL}
+ />
+ ),
+ )}
+ {hasHiddenLabels && (
+ <HoverCard>
+ <HoverCardTrigger asChild>
+ <Button
+ variant="outline"
+ size="sm"
+ className="h-6 cursor-pointer text-xs text-muted-foreground hover:text-foreground"
+ >
+ +{hiddenLabels.length} more
+ </Button>
+ </HoverCardTrigger>
+ <HoverCardContent className="w-80 p-3" side="bottom" align="start">
+ <div className="space-y-2">
+ <h4 className="text-sm font-semibold">All Labels</h4>
+ <div className="flex flex-wrap gap-1">
+ {sortedLabels.map((label) =>
+ showSimpleBadges ? (
+ <div
+ key={label}
+ className={cn(
+ "max-h-fit min-h-6 w-fit content-center rounded-sm bg-secondary px-1 text-left text-xs font-semibold text-secondary-foreground",
+ badgeClassName,
+ )}
+ >
+ {label}
+ </div>
+ ) : (
+ <StatusBadge
+ type={label}
+ key={label}
+ className={cn(
+ "break-all sm:break-normal",
+ badgeClassName,
+ )}
+ isLive={label === PRODUCTION_LABEL}
+ />
+ ),
+ )}
+ </div>
+ </div>
+ </HoverCardContent>
+ </HoverCard>
+ )}
+ </div>
+ );
+}
diff --git a/web/src/features/prompts/components/NewPromptForm/ReviewPromptDialog.tsx b/web/src/features/prompts/components/NewPromptForm/ReviewPromptDialog.tsx
index 118aae3512..85ea9169a5 100644
--- a/web/src/features/prompts/components/NewPromptForm/ReviewPromptDialog.tsx
+++ b/web/src/features/prompts/components/NewPromptForm/ReviewPromptDialog.tsx
@@ -24,15 +24,19 @@ type ReviewPromptDialogProps = {
const formatMessages = (messages: any[], excludeKeys: string[] = []) => {
return JSON.stringify(
- messages.map(m =>
+ messages.map((m) =>
Object.fromEntries(
Object.entries(m)
- .filter(([k]) => !excludeKeys.includes(k) && (k !== "type" || m.type === "placeholder"))
- .sort(([a], [b]) => a.localeCompare(b))
- )
+ .filter(
+ ([k]) =>
+ !excludeKeys.includes(k) &&
+ (k !== "type" || m.type === "placeholder"),
+ )
+ .sort(([a], [b]) => a.localeCompare(b)),
+ ),
),
null,
- 2
+ 2,
);
};
@@ -51,13 +55,15 @@ export const ReviewPromptDialog: React.FC<ReviewPromptDialogProps> = (
}
}, [open, setNewPromptValues, getNewPromptValues]);
- const initialPromptContent = initialPrompt.type === "text"
- ? (initialPrompt.prompt as string)
- : formatMessages(initialPrompt.prompt as any[]);
-
- const newPromptContent = initialPrompt.type === "text"
- ? (newPromptValue?.textPrompt ?? "")
- : formatMessages(newPromptValue?.chatPrompt ?? [], ["id"]);
+ const initialPromptContent =
+ initialPrompt.type === "text"
+ ? (initialPrompt.prompt as string)
+ : formatMessages(initialPrompt.prompt as any[]);
+
+ const newPromptContent =
+ initialPrompt.type === "text"
+ ? (newPromptValue?.textPrompt ?? "")
+ : formatMessages(newPromptValue?.chatPrompt ?? [], ["id"]);
const newConfig = JSON.stringify(
JSON.parse(newPromptValue?.config ?? "{}"),
diff --git a/web/src/features/prompts/components/PromptVersionDiffDialog.tsx b/web/src/features/prompts/components/PromptVersionDiffDialog.tsx
index a49353b2d7..6eac17d6f8 100644
--- a/web/src/features/prompts/components/PromptVersionDiffDialog.tsx
+++ b/web/src/features/prompts/components/PromptVersionDiffDialog.tsx
@@ -22,28 +22,37 @@ type PromptVersionDiffDialogProps = {
};
// Create a word-based diff that preserves JSON structure
-const createSmartDiff = (oldPrompt: Prompt, newPrompt: Prompt): { oldString: string; newString: string } => {
+const createSmartDiff = (
+ oldPrompt: Prompt,
+ newPrompt: Prompt,
+): { oldString: string; newString: string } => {
if (oldPrompt.type === "text" || newPrompt.type === "text") {
return {
- oldString: oldPrompt.type === "text" ? (oldPrompt.prompt as string) : JSON.stringify(oldPrompt.prompt, null, 2),
- newString: newPrompt.type === "text" ? (newPrompt.prompt as string) : JSON.stringify(newPrompt.prompt, null, 2)
+ oldString:
+ oldPrompt.type === "text"
+ ? (oldPrompt.prompt as string)
+ : JSON.stringify(oldPrompt.prompt, null, 2),
+ newString:
+ newPrompt.type === "text"
+ ? (newPrompt.prompt as string)
+ : JSON.stringify(newPrompt.prompt, null, 2),
};
}
- const formatMessages = (messages: any[]) =>
+ const formatMessages = (messages: any[]) =>
JSON.stringify(
- messages.map(m =>
+ messages.map((m) =>
Object.fromEntries(
- Object.entries(m).sort(([a], [b]) => a.localeCompare(b))
- )
+ Object.entries(m).sort(([a], [b]) => a.localeCompare(b)),
+ ),
),
null,
- 2
+ 2,
);
return {
oldString: formatMessages(oldPrompt.prompt as any[]),
- newString: formatMessages(newPrompt.prompt as any[])
+ newString: formatMessages(newPrompt.prompt as any[]),
};
};
diff --git a/web/src/features/prompts/components/SetPromptVersionLabels/index.tsx b/web/src/features/prompts/components/SetPromptVersionLabels/index.tsx
index 05227c8301..2e184c162e 100644
--- a/web/src/features/prompts/components/SetPromptVersionLabels/index.tsx
+++ b/web/src/features/prompts/components/SetPromptVersionLabels/index.tsx
@@ -20,7 +20,7 @@ import { AddLabelForm } from "./AddLabelForm";
import { LabelCommandItem } from "./LabelCommandItem";
import { usePostHogClientCapture } from "@/src/features/posthog-analytics/usePostHogClientCapture";
import { isReservedPromptLabel } from "@/src/features/prompts/utils";
-import { StatusBadge } from "@/src/components/layouts/status-badge";
+import { TruncatedLabels } from "@/src/components/TruncatedLabels";
import { cn } from "@/src/utils/tailwind";
export function SetPromptVersionLabels({
@@ -30,6 +30,7 @@ export function SetPromptVersionLabels({
setIsOpen,
title,
showOnlyOnHover = false,
+ maxVisibleLabels = 8,
}: {
promptLabels: string[];
prompt: Prompt;
@@ -37,6 +38,7 @@ export function SetPromptVersionLabels({
setIsOpen: (isOpen: boolean) => void;
title?: ReactNode;
showOnlyOnHover?: boolean;
+ maxVisibleLabels?: number;
}) {
const projectId = useProjectIdFromURL();
const utils = api.useUtils();
@@ -82,12 +84,6 @@ export function SetPromptVersionLabels({
},
});
- const sortedLabels = [...promptLabels].sort((a, b) => {
- if (a === PRODUCTION_LABEL) return -1;
- if (b === PRODUCTION_LABEL) return 1;
- return a.localeCompare(b);
- });
-
const handleSubmitLabels = async () => {
try {
if (!projectId) {
@@ -123,14 +119,10 @@ export function SetPromptVersionLabels({
)}
>
{title && title}
- {sortedLabels.map((label) => (
- <StatusBadge
- type={label}
- key={label}
- className="break-all sm:break-normal"
- isLive={label === PRODUCTION_LABEL}
- />
- ))}
+ <TruncatedLabels
+ labels={promptLabels}
+ maxVisibleLabels={maxVisibleLabels}
+ />
<Button
variant="outline"
title="Add prompt version label"
diff --git a/web/src/pages/project/[projectId]/prompts/metrics.tsx b/web/src/pages/project/[projectId]/prompts/metrics.tsx
index 2e327351b0..0a05d51c87 100644
--- a/web/src/pages/project/[projectId]/prompts/metrics.tsx
+++ b/web/src/pages/project/[projectId]/prompts/metrics.tsx
@@ -24,6 +24,7 @@ import { useIndividualScoreColumns } from "@/src/features/scores/hooks/useIndivi
import useColumnOrder from "@/src/features/column-visibility/hooks/useColumnOrder";
import Page from "@/src/components/layouts/page";
import { DetailPageNav } from "@/src/features/navigate-detail-pages/DetailPageNav";
+import { TruncatedLabels } from "@/src/components/TruncatedLabels";
export type PromptVersionTableRow = {
version: number;
@@ -176,16 +177,12 @@ export default function PromptVersionTable({
const values: string[] = row.getValue("labels");
return (
values && (
- <div className="-mr-8 flex max-h-full flex-wrap gap-1">
- {values.map((value) => (
- <div
- key={value}
- className="max-h-fit min-h-6 w-fit content-center rounded-sm bg-secondary px-1 text-left text-xs font-semibold text-secondary-foreground"
- >
- {value}
- </div>
- ))}
- </div>
+ <TruncatedLabels
+ labels={values}
+ maxVisibleLabels={3}
+ className="-mr-8 flex max-h-full flex-wrap gap-1"
+ showSimpleBadges={true}
+ />
)
);
},