Skip to content

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

feat(corrections): add JSON validation toggle (#11520)

marliessophie2cdf13dc0dd0
diff --git a/web/src/components/trace2/components/IOPreview/components/CorrectedOutputField.tsx b/web/src/components/trace2/components/IOPreview/components/CorrectedOutputField.tsx
index 21eb5ebe10..4640d03392 100644
--- a/web/src/components/trace2/components/IOPreview/components/CorrectedOutputField.tsx
+++ b/web/src/components/trace2/components/IOPreview/components/CorrectedOutputField.tsx
@@ -8,6 +8,8 @@ import { useCorrectionEditor } from "./hooks/useCorrectionEditor";
import { useMemo } from "react";
import { CodeMirrorEditor } from "@/src/components/editor/CodeMirrorEditor";
import { useHasProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
+import { Switch } from "@/src/components/ui/switch";
+import useLocalStorage from "@/src/components/useLocalStorage";
interface CorrectedOutputFieldProps {
projectId: string;
@@ -28,6 +30,12 @@ export function CorrectedOutputField({
}: CorrectedOutputFieldProps) {
const hasAccess = useHasProjectAccess({ projectId, scope: "scores:CUD" });
+ // JSON validation toggle (persisted in localStorage)
+ const [strictJsonMode, setStrictJsonMode] = useLocalStorage(
+ "correctionStrictJsonMode",
+ false,
+ );
+
// Merge cache + server data
const { effectiveCorrection, correctionValue } = useCorrectionData(
existingCorrection,
@@ -52,6 +60,7 @@ export function CorrectedOutputField({
actualOutput,
onSave: handleSave,
setSaveStatus,
+ strictJsonMode,
});
const hasContent = value.trim().length > 0;
@@ -75,54 +84,66 @@ export function CorrectedOutputField({
return (
<div className="px-2">
<div className="group relative rounded-md">
- <div className="flex items-center justify-between bg-muted/30 py-1.5">
+ <div className="flex items-center justify-between py-1.5">
<span className="text-sm font-medium">Corrected Output (Beta)</span>
- <div className="-mr-1 flex items-center -space-x-1 opacity-0 transition-opacity group-hover:opacity-100">
- {!isValidJson && isEditing && hasContent && (
- <span className="mr-2 text-xs text-red-500">
- Invalid JSON - fix to save
- </span>
- )}
- {isValidJson &&
- saveStatus === "idle" &&
- isEditing &&
- hasContent && (
- <span className="mr-2 text-xs text-muted-foreground">
- DRAFT - Type any character to save
+ <div className="-mr-1 flex items-center">
+ <div className="flex items-center -space-x-1 opacity-0 transition-opacity group-hover:opacity-100">
+ {!isValidJson && isEditing && hasContent && (
+ <span className="mr-2 text-xs text-red-500">
+ {strictJsonMode
+ ? "Invalid JSON - fix to save"
+ : "Cannot save empty content"}
</span>
)}
- {isValidJson && saveStatus === "saving" && (
- <span className="mr-2 text-xs text-muted-foreground">
- Saving...
- </span>
- )}
- {isValidJson && saveStatus === "saved" && (
- <span className="mr-2 text-xs">Saved ✓</span>
- )}
- {hasContent && !isEditing && (
- <>
- <Button
- size="icon-xs"
- variant="ghost"
- onClick={handleEdit}
- disabled={!hasAccess}
- className="hover:bg-border"
- title="Edit corrected output"
- >
- <Pencil className="h-3 w-3" />
- </Button>
- <Button
- size="icon-xs"
- variant="ghost"
- onClick={handleDelete}
- disabled={!hasAccess}
- className="hover:bg-border"
- title="Delete corrected output"
- >
- <Trash className="h-3 w-3" />
- </Button>
- </>
- )}
+ {isValidJson &&
+ saveStatus === "idle" &&
+ isEditing &&
+ hasContent && (
+ <span className="mr-2 text-xs text-muted-foreground">
+ DRAFT - Type any character to save
+ </span>
+ )}
+ {isValidJson && saveStatus === "saving" && (
+ <span className="mr-2 w-fit text-xs text-muted-foreground">
+ Saving...
+ </span>
+ )}
+ {isValidJson && saveStatus === "saved" && (
+ <span className="mr-2 w-fit text-xs">Saved ✓</span>
+ )}
+ {hasContent && !isEditing && (
+ <>
+ <Button
+ size="icon-xs"
+ variant="ghost"
+ onClick={handleEdit}
+ disabled={!hasAccess}
+ className="hover:bg-border"
+ title="Edit corrected output"
+ >
+ <Pencil className="h-3 w-3" />
+ </Button>
+ <Button
+ size="icon-xs"
+ variant="ghost"
+ onClick={handleDelete}
+ disabled={!hasAccess}
+ className="hover:bg-border"
+ title="Delete corrected output"
+ >
+ <Trash className="h-3 w-3" />
+ </Button>
+ </>
+ )}
+ </div>
+ <div className="flex items-center">
+ <Switch
+ checked={strictJsonMode}
+ onCheckedChange={setStrictJsonMode}
+ className="scale-75"
+ />
+ <span className="text-xs text-muted-foreground">JSON</span>
+ </div>
</div>
</div>
@@ -140,15 +161,15 @@ export function CorrectedOutputField({
<CodeMirrorEditor
value={displayValue}
onChange={handleEditorChange}
- mode="json"
+ mode={strictJsonMode ? "json" : "text"}
minHeight={200}
- placeholder="Enter corrected output as JSON..."
+ placeholder="Enter corrected output..."
className="bg-accent-light-green"
/>
) : (
<CodeMirrorEditor
value={displayValue}
- mode="json"
+ mode={strictJsonMode ? "json" : "text"}
minHeight={200}
editable={false}
className="bg-accent-light-green"
diff --git a/web/src/components/trace2/components/IOPreview/components/hooks/useCorrectionEditor.ts b/web/src/components/trace2/components/IOPreview/components/hooks/useCorrectionEditor.ts
index bf7c1e4c00..eed803a6d1 100644
--- a/web/src/components/trace2/components/IOPreview/components/hooks/useCorrectionEditor.ts
+++ b/web/src/components/trace2/components/IOPreview/components/hooks/useCorrectionEditor.ts
@@ -6,6 +6,7 @@ interface UseCorrectionEditorParams {
onSave: (value: string) => void;
setSaveStatus: (status: "idle" | "saving" | "saved") => void;
debounceMs?: number;
+ strictJsonMode?: boolean;
}
/**
@@ -17,6 +18,7 @@ export function useCorrectionEditor({
onSave,
setSaveStatus,
debounceMs = 500,
+ strictJsonMode = false,
}: UseCorrectionEditorParams) {
const [isEditing, setIsEditing] = useState(false);
const [value, setValue] = useState(correctionValue);
@@ -30,6 +32,25 @@ export function useCorrectionEditor({
setValue(correctionValue);
}, [correctionValue]);
+ useEffect(() => {
+ let valid = false;
+ if (strictJsonMode) {
+ try {
+ if (value.trim()) {
+ JSON.parse(value);
+ valid = true;
+ }
+ } catch {
+ valid = false;
+ }
+ } else {
+ valid = value.trim().length > 0;
+ }
+ setIsValidJson(valid);
+ // We only need to re-validate when strictJsonMode changes
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [strictJsonMode]);
+
const handleEdit = useCallback(() => {
setIsEditing(true);
// Prepopulate with actual output if no existing correction
@@ -54,19 +75,25 @@ export function useCorrectionEditor({
// Update local state immediately for responsive typing
setValue(newValue);
- // Validate JSON
+ // Validate based on mode
let valid = false;
- try {
- if (newValue.trim()) {
- JSON.parse(newValue);
- valid = true;
+ if (strictJsonMode) {
+ // Strict: only valid JSON
+ try {
+ if (newValue.trim()) {
+ JSON.parse(newValue);
+ valid = true;
+ }
+ } catch {
+ valid = false;
}
- } catch {
- valid = false;
+ } else {
+ // Lenient: any non-empty string
+ valid = newValue.trim().length > 0;
}
setIsValidJson(valid);
- // Only save if valid JSON
+ // Save if valid
if (valid) {
setSaveStatus("saving");
@@ -81,13 +108,13 @@ export function useCorrectionEditor({
}, debounceMs);
} else {
setSaveStatus("idle");
- // Clear timeout if JSON becomes invalid
+ // Clear timeout if invalid
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
}
},
- [onSave, setSaveStatus, debounceMs],
+ [onSave, setSaveStatus, debounceMs, strictJsonMode],
);
return {