🪢 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
refactor(evals): optimize variable extraction logic in EvaluationPromptPreview and useExtractVariables hooks (#7409)
marliessophie7a9c62b106c3
diff --git a/web/src/features/evals/components/evaluation-prompt-preview.tsx b/web/src/features/evals/components/evaluation-prompt-preview.tsxindex 888a0a42fd..4a4019db0a 100644--- a/web/src/features/evals/components/evaluation-prompt-preview.tsx+++ b/web/src/features/evals/components/evaluation-prompt-preview.tsx@@ -6,7 +6,7 @@ import { cn } from "@/src/utils/tailwind";import { type RouterOutput } from "@/src/utils/types";import { type EvalTemplate } from "@langfuse/shared";import Link from "next/link";-import { Fragment } from "react";+import { Fragment, useMemo } from "react";const VARIABLE_COLORS = ["text-primary-accent",@@ -128,9 +128,14 @@ export const EvaluationPromptPreview = ({className?: string;controlButtons?: React.ReactNode;}) => {+ const memoizedVariables = useMemo(+ () => variableMapping.map(({ templateVariable }) => templateVariable),+ [variableMapping],+ );+const { extractedVariables, isExtracting } = useExtractVariables({- variables: variableMapping.map(({ templateVariable }) => templateVariable),- variableMapping: variableMapping,+ variables: memoizedVariables,+ variableMapping,trace: trace,isLoading,});diff --git a/web/src/features/evals/components/inner-evaluator-form.tsx b/web/src/features/evals/components/inner-evaluator-form.tsxindex e9b5490ea4..de3ed14c1f 100644--- a/web/src/features/evals/components/inner-evaluator-form.tsx+++ b/web/src/features/evals/components/inner-evaluator-form.tsx@@ -396,13 +396,13 @@ export const InnerEvaluatorForm = (props: {<div className="flex items-center gap-2">{form.watch("target") === "trace" && !props.disabled && (<>- <span className="text-xs text-muted-foreground">Show preview</span>+ <span className="text-xs text-muted-foreground">Show Preview</span><Switchchecked={showPreview}onCheckedChange={setShowPreview}disabled={props.disabled}/>- {traceWithObservations && showPreview && (+ {traceWithObservations && showPreview ? (<DetailPageNavcurrentId={traceWithObservations.id}listKey="traces"@@ -410,6 +410,11 @@ export const InnerEvaluatorForm = (props: {`/project/${props.projectId}/evals/new?evaluator=${props.evalTemplate.id}&traceId=${entry.id}`}/>+ ) : (+ <div className="flex flex-row gap-1">+ <Skeleton className="h-8 w-[54px]" />+ <Skeleton className="h-8 w-[54px]" />+ </div>)}</>)}@@ -723,11 +728,12 @@ export const InnerEvaluatorForm = (props: {controlButtons={mappingControlButtons}/>) : (- <div className="flex max-h-full min-h-[200px] w-full flex-col gap-1 lg:w-2/3">- <div className="flex flex-row items-end justify-between">- <span className="h-fit px-1 text-start text-sm font-medium">- Evaluation Prompt- </span>+ <div className="flex max-h-full min-h-48 w-full flex-col gap-1 lg:w-2/3">+ <div className="flex flex-row items-center justify-between py-0 text-sm font-medium capitalize">+ <div className="flex flex-row items-center gap-2">+ Evaluation Prompt Preview+ <Skeleton className="h-[25px] w-[63px]" />+ </div><div className="flex justify-end">{mappingControlButtons}</div>diff --git a/web/src/features/evals/hooks/useExtractVariables.ts b/web/src/features/evals/hooks/useExtractVariables.tsindex cc1be77041..e3c1e12062 100644--- a/web/src/features/evals/hooks/useExtractVariables.ts+++ b/web/src/features/evals/hooks/useExtractVariables.ts@@ -44,7 +44,8 @@ export function useExtractVariables({const previousMappingRef = useRef<string>("");// Create a stable string representation of the current mapping for comparison- const currentMappingString = JSON.stringify(variableMapping);+ const currentMappingString =+ variables.length > 0 ? JSON.stringify(variableMapping) : "";// Create a stable reference to the trace IDconst traceId = trace?.id;@@ -59,10 +60,14 @@ export function useExtractVariables({useEffect(() => {// Return early conditions- if (isLoading || !variables.length) {- setExtractedVariables(- variables.map((variable) => ({ variable, value: "n/a" })),- );+ if (isLoading) {+ setExtractedVariables([]);+ return;+ }++ // If no variables, only update if current state is not empty+ if (!Boolean(variables.length)) {+ setExtractedVariables((prev) => (prev.length === 0 ? prev : []));return;}