🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(evals): show alert if variable mapping drifts between template and config (#12616)
marliessophie4d971579d95b
diff --git a/web/src/features/evals/components/inner-evaluator-form.tsx b/web/src/features/evals/components/inner-evaluator-form.tsxindex f5c3c7963f..258e4de140 100644--- a/web/src/features/evals/components/inner-evaluator-form.tsx+++ b/web/src/features/evals/components/inner-evaluator-form.tsx@@ -51,6 +51,7 @@ import {type LangfuseObject,} from "@/src/features/evals/utils/evaluator-form-utils";import { validateAndTransformVariableMapping } from "@/src/features/evals/utils/variable-mapping-validation";+import { useVariableMappingSync } from "@/src/features/evals/hooks/useVariableMappingSync";import { EvalTargetObject } from "@langfuse/shared";import { ExecutionCountTooltip } from "@/src/features/evals/components/execution-count-tooltip";import { Suspense, lazy } from "react";@@ -355,8 +356,19 @@ export const InnerEvaluatorForm = (props: {},}) as UseFormReturn<EvalFormType>;+ const currentMapping = form.watch("mapping") ?? [];+ const syncStatus = useVariableMappingSync({+ templateVars: props.evalTemplate?.vars,+ currentMapping: currentMapping,+ });+useEffect(() => {- if (props.evalTemplate && form.getValues("mapping").length === 0) {+ if (!props.evalTemplate) return;++ const mapping = form.getValues("mapping");++ if (mapping.length === 0) {+ // Initialize mapping for new evaluatorsconst target = form.getValues("target");form.setValue("mapping",@@ -369,8 +381,38 @@ export const InnerEvaluatorForm = (props: {})),);form.setValue("scoreName", `${props.evalTemplate.name}`);+ } else if (+ props.existingEvaluator &&+ !props.disabled &&+ !syncStatus.inSync+ ) {+ // Reconcile mapping when edit mode is enabled+ const target = form.getValues("target");++ // Keep mappings for unchanged variables+ const preservedMappings = mapping.filter((m) =>+ syncStatus.unchanged.includes(m.templateVariable),+ );++ // Add mappings for new variables+ const newMappings = syncStatus.added.map((varName) => ({+ templateVariable: varName,+ langfuseObject: isLegacyEvalTarget(target)+ ? ("trace" as const)+ : undefined,+ ...inferDefaultMapping(varName),+ }));++ // Combine and update form+ form.setValue("mapping", [...preservedMappings, ...newMappings]);}- }, [form, props.evalTemplate]);+ }, [+ form,+ props.evalTemplate,+ props.disabled,+ props.existingEvaluator,+ syncStatus,+ ]);const utils = api.useUtils();const createJobMutation = api.evals.createJob.useMutation({diff --git a/web/src/features/evals/components/variable-mapping-card.tsx b/web/src/features/evals/components/variable-mapping-card.tsxindex c1caf5b1af..1106621b20 100644--- a/web/src/features/evals/components/variable-mapping-card.tsx+++ b/web/src/features/evals/components/variable-mapping-card.tsx@@ -46,6 +46,9 @@ import { Switch } from "@/src/components/ui/switch";import { DetailPageNav } from "@/src/features/navigate-detail-pages/DetailPageNav";import { useEvalConfigMappingData } from "@/src/features/evals/hooks/useEvalConfigMappingData";import { useEffect, useState } from "react";+import { Alert, AlertTitle, AlertDescription } from "@/src/components/ui/alert";+import { AlertCircle } from "lucide-react";+import { useVariableMappingSync } from "@/src/features/evals/hooks/useVariableMappingSync";export const VariableMappingCard = ({projectId,@@ -75,6 +78,11 @@ export const VariableMappingCard = ({name: "mapping",});+ const syncStatus = useVariableMappingSync({+ templateVars: evalTemplate?.vars,+ currentMapping: fields,+ });+const { namesByObject, isLoading, previewData } = useEvalConfigMappingData(projectId,form,@@ -214,6 +222,19 @@ export const VariableMappingCard = ({!shouldWrapVariables && "lg:w-1/3",)}>+ {disabled && !syncStatus.inSync && (+ <Alert className="text-sm" variant="destructive">+ <AlertCircle className="h-4 w-4" />+ <AlertTitle className="text-base">+ Variable mapping is out of sync+ </AlertTitle>+ <AlertDescription>+ The template has {syncStatus.added.length} new+ variable(s) and {syncStatus.removed.length} removed+ variable(s). Toggle Edit Mode to update the mapping.+ </AlertDescription>+ </Alert>+ )}{isLegacyEvalTarget(form.watch("target")) // Complex variable mapping for trace/dataset targets (legacy)? fields.map((mappingField, index) => (<Card className="flex flex-col gap-2 p-4" key={index}>diff --git a/web/src/features/evals/hooks/useVariableMappingSync.ts b/web/src/features/evals/hooks/useVariableMappingSync.tsnew file mode 100644index 0000000000..458dd5fcd5--- /dev/null+++ b/web/src/features/evals/hooks/useVariableMappingSync.ts@@ -0,0 +1,30 @@+import { useMemo } from "react";++export function useVariableMappingSync(params: {+ templateVars?: string[];+ currentMapping: { templateVariable: string }[];+}) {+ return useMemo(() => {+ if (!params.templateVars) {+ return { inSync: true, added: [], removed: [], unchanged: [] };+ }++ const templateVarSet = new Set(params.templateVars);+ const mappingVarSet = new Set(+ params.currentMapping.map((m) => m.templateVariable),+ );++ const added = params.templateVars.filter((v) => !mappingVarSet.has(v));+ const removed = params.currentMapping+ .map((m) => m.templateVariable)+ .filter((v) => !templateVarSet.has(v));+ const unchanged = params.templateVars.filter((v) => mappingVarSet.has(v));++ return {+ inSync: added.length === 0 && removed.length === 0,+ added,+ removed,+ unchanged,+ };+ }, [params.templateVars, params.currentMapping]);+}