Skip to content

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

fix(web): show existing corrected output by default (#18209)

Trang Nguyen39dee4a10a77
diff --git a/web/src/__tests__/server/scores-trpc.servertest.ts b/web/src/__tests__/server/scores-trpc.servertest.ts
index ebbbef1e91..2217744e9a 100644
--- a/web/src/__tests__/server/scores-trpc.servertest.ts
+++ b/web/src/__tests__/server/scores-trpc.servertest.ts
@@ -46,6 +46,7 @@ import {
BatchActionQueue,
QueueJobs,
createOrgProjectAndApiKey,
+ getScoreById,
} from "@langfuse/shared/src/server";
import { env } from "@/src/env.mjs";
import { observationScopeFilter } from "@/src/features/filters/config/scores-config";
@@ -799,6 +800,29 @@ describe("scores trpc", () => {
});
});
+ describe("scores.deleteAnnotationScore", () => {
+ it("deletes a correction ingested via the API", async () => {
+ const correction = createTraceScore({
+ project_id: projectId,
+ name: "output",
+ source: "API",
+ data_type: "CORRECTION",
+ value: 0,
+ long_string_value: "corrected response",
+ });
+ await createScoresCh([correction]);
+
+ await caller.scores.deleteAnnotationScore({
+ projectId,
+ id: correction.id,
+ });
+
+ expect(
+ await getScoreById({ projectId, scoreId: correction.id }),
+ ).toBeUndefined();
+ });
+ });
+
describe("scores.deleteMany", () => {
it("should delete scores by ids", async () => {
// Setup
diff --git a/web/src/features/traces/components/IOPreview/components/CorrectedOutputDiffDialog.tsx b/web/src/features/traces/components/IOPreview/components/CorrectedOutputDiffDialog.tsx
index 1e4072f957..a9f28d6f92 100644
--- a/web/src/features/traces/components/IOPreview/components/CorrectedOutputDiffDialog.tsx
+++ b/web/src/features/traces/components/IOPreview/components/CorrectedOutputDiffDialog.tsx
@@ -61,15 +61,34 @@ const formatOutputForDiff = (
export const CorrectedOutputDiffDialog: React.FC<
CorrectedOutputDiffDialogProps
-> = ({
- isOpen,
- setIsOpen,
+> = ({ isOpen, setIsOpen, ...bodyProps }) => {
+ return (
+ <Dialog open={isOpen} onOpenChange={setIsOpen}>
+ <DialogContent size="xl">
+ <DialogHeader>
+ <DialogTitle>Output Correction Diff</DialogTitle>
+ <DialogDescription>
+ Compare the original output with the corrected version
+ </DialogDescription>
+ </DialogHeader>
+
+ <CorrectedOutputDiffBody {...bodyProps} />
+
+ <DialogFooter>
+ <Button onClick={() => setIsOpen(false)}>Close</Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+};
+
+// Rendered inside DialogContent so formatting only runs while the dialog is shown.
+const CorrectedOutputDiffBody = ({
actualOutput,
correctedOutput,
strictJsonMode,
actualOutputTooLarge = false,
-}) => {
- // Format both outputs for comparison
+}: Omit<CorrectedOutputDiffDialogProps, "isOpen" | "setIsOpen">) => {
const formattedActualOutput = formatOutputForDiff(
actualOutput,
strictJsonMode,
@@ -87,60 +106,45 @@ export const CorrectedOutputDiffDialog: React.FC<
(actualOutput === null || actualOutput === undefined);
return (
- <Dialog open={isOpen} onOpenChange={setIsOpen}>
- <DialogContent size="xl">
- <DialogHeader>
- <DialogTitle>Output Correction Diff</DialogTitle>
- <DialogDescription>
- Compare the original output with the corrected version
- </DialogDescription>
- </DialogHeader>
-
- <DialogBody>
- {actualOutputTooLarge ? (
- <div className="space-y-4">
- <div className="text-muted-foreground rounded-md border border-dashed p-4 text-sm">
- <p className="text-foreground font-bold">
- Original output too large to diff
- </p>
- <p className="mt-1">
- The original output is too large to load here, so it cannot be
- compared side by side. Your correction is shown below and will
- be saved as-is.
- </p>
- </div>
- <div>
- <p className="mb-1 text-sm font-bold">Corrected Output</p>
- <pre className="ph-no-capture bg-muted/30 max-h-[50vh] overflow-auto rounded-md border p-3 text-xs break-words whitespace-pre-wrap">
- {formattedCorrectedOutput}
- </pre>
- </div>
- </div>
- ) : hasNoOriginalOutput ? (
- <div className="flex flex-col items-center justify-center p-8 text-center">
- <div className="text-muted-foreground">
- <p className="text-lg font-bold">No original output</p>
- <p className="mt-2 text-sm">
- There is no original output to compare with the correction.
- </p>
- </div>
- </div>
- ) : (
- <div className="ph-no-capture space-y-4">
- <DiffViewer
- oldString={formattedActualOutput}
- newString={formattedCorrectedOutput}
- oldLabel="Original Output"
- newLabel="Corrected Output"
- />
- </div>
- )}
- </DialogBody>
-
- <DialogFooter>
- <Button onClick={() => setIsOpen(false)}>Close</Button>
- </DialogFooter>
- </DialogContent>
- </Dialog>
+ <DialogBody>
+ {actualOutputTooLarge ? (
+ <div className="space-y-4">
+ <div className="text-muted-foreground rounded-md border border-dashed p-4 text-sm">
+ <p className="text-foreground font-bold">
+ Original output too large to diff
+ </p>
+ <p className="mt-1">
+ The original output is too large to load here, so it cannot be
+ compared side by side. Your correction is shown below and will be
+ saved as-is.
+ </p>
+ </div>
+ <div>
+ <p className="mb-1 text-sm font-bold">Corrected Output</p>
+ <pre className="ph-no-capture bg-muted/30 max-h-[50vh] overflow-auto rounded-md border p-3 text-xs break-words whitespace-pre-wrap">
+ {formattedCorrectedOutput}
+ </pre>
+ </div>
+ </div>
+ ) : hasNoOriginalOutput ? (
+ <div className="flex flex-col items-center justify-center p-8 text-center">
+ <div className="text-muted-foreground">
+ <p className="text-lg font-bold">No original output</p>
+ <p className="mt-2 text-sm">
+ There is no original output to compare with the correction.
+ </p>
+ </div>
+ </div>
+ ) : (
+ <div className="ph-no-capture space-y-4">
+ <DiffViewer
+ oldString={formattedActualOutput}
+ newString={formattedCorrectedOutput}
+ oldLabel="Original Output"
+ newLabel="Corrected Output"
+ />
+ </div>
+ )}
+ </DialogBody>
);
};
diff --git a/web/src/features/traces/components/IOPreview/components/CorrectedOutputField.clienttest.tsx b/web/src/features/traces/components/IOPreview/components/CorrectedOutputField.clienttest.tsx
new file mode 100644
index 0000000000..6fcedc0e45
--- /dev/null
+++ b/web/src/features/traces/components/IOPreview/components/CorrectedOutputField.clienttest.tsx
@@ -0,0 +1,145 @@
+import { fireEvent, render, screen } from "@testing-library/react";
+
+vi.mock("../hooks/useCorrectionData", () => ({
+ useCorrectionData: (existingCorrection?: { longStringValue?: string }) => ({
+ effectiveCorrection: existingCorrection,
+ correctionValue: existingCorrection?.longStringValue ?? "",
+ }),
+}));
+
+vi.mock("../hooks/useCorrectionMutations", () => ({
+ useCorrectionMutations: () => ({
+ saveStatus: "idle",
+ setSaveStatus: vi.fn(),
+ handleSave: vi.fn(),
+ handleDelete: vi.fn(),
+ }),
+}));
+
+vi.mock("../hooks/useCorrectionEditor", () => ({
+ useCorrectionEditor: ({ correctionValue }: { correctionValue: string }) => ({
+ isEditing: false,
+ setIsEditing: vi.fn(),
+ value: correctionValue,
+ isValidJson: true,
+ handleEdit: vi.fn(),
+ handleChange: vi.fn(),
+ }),
+}));
+
+vi.mock("@/src/features/rbac", () => ({
+ useHasProjectAccess: () => true,
+}));
+
+vi.mock("@/src/features/posthog-analytics", () => ({
+ usePostHogClientCapture: () => vi.fn(),
+}));
+
+vi.mock("@/src/components/useLocalStorage", () => ({
+ default: () => [false, vi.fn()],
+}));
+
+vi.mock("@/src/components/editor/CodeMirrorEditor", () => ({
+ CodeMirrorEditor: ({ value }: { value: string }) => (
+ <pre data-testid="correction-editor">{value}</pre>
+ ),
+}));
+
+vi.mock("@/src/components/design-system/Switch/Switch", () => ({
+ Switch: () => null,
+}));
+
+vi.mock("@/src/components/ui/hover-card", () => ({
+ HoverCard: ({ children }: { children: React.ReactNode }) => children,
+ HoverCardContent: ({ children }: { children: React.ReactNode }) => children,
+ HoverCardTrigger: ({ children }: { children: React.ReactNode }) => children,
+}));
+
+vi.mock("./CorrectedOutputDiffDialog", () => ({
+ CorrectedOutputDiffDialog: () => null,
+}));
+
+import { CorrectedOutputField } from "./CorrectedOutputField";
+
+const baseProps = {
+ projectId: "project-id",
+ traceId: "trace-id",
+ environment: "default",
+};
+
+describe("CorrectedOutputField visibility", () => {
+ it("shows an existing correction without requiring a click", () => {
+ render(
+ <CorrectedOutputField
+ {...baseProps}
+ existingCorrection={
+ {
+ id: "correction-id",
+ longStringValue: "corrected response",
+ } as never
+ }
+ />,
+ );
+
+ expect(screen.getByTestId("correction-editor")).toHaveTextContent(
+ "corrected response",
+ );
+ expect(
+ screen.queryByRole("button", { name: "Corrected output" }),
+ ).not.toBeInTheDocument();
+ });
+
+ it("shows an existing empty-valued correction without requiring a click", () => {
+ render(
+ <CorrectedOutputField
+ {...baseProps}
+ existingCorrection={
+ {
+ id: "empty-correction-id",
+ longStringValue: "",
+ } as never
+ }
+ />,
+ );
+
+ expect(screen.getByText("Corrected Output")).toBeInTheDocument();
+ expect(
+ screen.queryByRole("button", { name: "Correct output" }),
+ ).not.toBeInTheDocument();
+ });
+
+ it("collapses back to the link after deleting a correction", () => {
+ const { rerender } = render(
+ <CorrectedOutputField {...baseProps} existingCorrection={null} />,
+ );
+ fireEvent.click(screen.getByRole("button", { name: "Correct output" }));
+ rerender(
+ <CorrectedOutputField
+ {...baseProps}
+ existingCorrection={
+ {
+ id: "correction-id",
+ longStringValue: "corrected response",
+ } as never
+ }
+ />,
+ );
+
+ fireEvent.click(screen.getByTitle("Delete corrected output"));
+ rerender(<CorrectedOutputField {...baseProps} existingCorrection={null} />);
+
+ expect(
+ screen.getByRole("button", { name: "Correct output" }),
+ ).toBeInTheDocument();
+ expect(screen.queryByText("Corrected Output")).not.toBeInTheDocument();
+ });
+
+ it("keeps the section collapsed when no correction exists", () => {
+ render(<CorrectedOutputField {...baseProps} existingCorrection={null} />);
+
+ expect(
+ screen.getByRole("button", { name: "Correct output" }),
+ ).toBeInTheDocument();
+ expect(screen.queryByTestId("correction-editor")).not.toBeInTheDocument();
+ });
+});
diff --git a/web/src/features/traces/components/IOPreview/components/CorrectedOutputField.tsx b/web/src/features/traces/components/IOPreview/components/CorrectedOutputField.tsx
index 381485b66c..ee7ec9dff0 100644
--- a/web/src/features/traces/components/IOPreview/components/CorrectedOutputField.tsx
+++ b/web/src/features/traces/components/IOPreview/components/CorrectedOutputField.tsx
@@ -59,7 +59,7 @@ export function CorrectedOutputField({
// Diff dialog state
const [isDiffDialogOpen, setIsDiffDialogOpen] = useState(false);
- // One-line link by default; the full section renders on demand.
+ // When no correction exists, keep the section collapsed until it is opened.
const [isExpanded, setIsExpanded] = useState(false);
// Merge cache + server data
@@ -159,11 +159,10 @@ export function CorrectedOutputField({
const handleDeleteWithExitEdit = () => {
handleDelete();
setIsEditing(false);
+ setIsExpanded(false);
};
- // Collapsed to a one-line link by default: corrections are used by a small
- // share of users, so the full editor UI only takes space once asked for.
- if (!isExpanded) {
+ if (!isExpanded && !effectiveCorrection) {
return (
<div className="px-2 py-2">
<button
@@ -178,7 +177,7 @@ export function CorrectedOutputField({
className="text-muted-foreground hover:text-foreground inline-flex items-center gap-1 text-xs hover:underline"
>
<Pencil className="size-3 shrink-0" aria-hidden />
- {hasContent ? "Corrected output" : "Correct output"}
+ Correct output
</button>
</div>
);
diff --git a/web/src/server/api/routers/scores.ts b/web/src/server/api/routers/scores.ts
index c00d2086bc..58ce47f2c8 100644
--- a/web/src/server/api/routers/scores.ts
+++ b/web/src/server/api/routers/scores.ts
@@ -973,12 +973,16 @@ export const scoresRouter = createTRPCRouter({
scope: "scores:CUD",
});
- // Fetch the current score from Clickhouse
- const clickhouseScore = await getScoreById({
+ // Corrections can be ingested via the API and are still deletable in the UI
+ const fetchedScore = await getScoreById({
projectId: input.projectId,
scoreId: input.id,
- source: ScoreSourceEnum.ANNOTATION,
});
+ const clickhouseScore =
+ fetchedScore?.source === ScoreSourceEnum.ANNOTATION ||
+ fetchedScore?.dataType === ScoreDataTypeEnum.CORRECTION
+ ? fetchedScore
+ : undefined;
if (!clickhouseScore) {
logger.warn(
`No annotation score with id ${input.id} in project ${input.projectId} in Clickhouse`,