Skip to content

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

fix(web): validate unsupported evaluator JSONPath (#15592)

Tobias Wochinger03c3d5ada00d
diff --git a/web/src/features/evals/components/eval-form-descriptions.tsx b/web/src/features/evals/components/eval-form-descriptions.tsx
index cbb622f4c3..976604de23 100644
--- a/web/src/features/evals/components/eval-form-descriptions.tsx
+++ b/web/src/features/evals/components/eval-form-descriptions.tsx
@@ -7,7 +7,7 @@ export function VariableMappingDescription(p: {
href: string;
}) {
return (
- <div className="flex w-1/2 items-center">
+ <div className="flex h-8 w-1/2 items-center">
<Label className="text-sm font-normal">{p.title}</Label>
<DocPopup description={p.description} href={p.href} />
</div>
diff --git a/web/src/features/evals/components/inner-evaluator-form.tsx b/web/src/features/evals/components/inner-evaluator-form.tsx
index 9f874bc687..b985449c08 100644
--- a/web/src/features/evals/components/inner-evaluator-form.tsx
+++ b/web/src/features/evals/components/inner-evaluator-form.tsx
@@ -45,6 +45,7 @@ import { Checkbox } from "@/src/components/design-system/Checkbox/Checkbox";
import { Switch } from "@/src/components/design-system/Switch/Switch";
import {
evalConfigFormSchema,
+ getActiveJsonPathCompatibilityWarning,
type EvalFormType,
getTargetDisplayName,
inferDefaultMapping,
@@ -337,7 +338,10 @@ export const InnerEvaluatorForm = (props: {
evalCapabilities: EvalCapabilities;
defaultRunOnLive?: boolean;
defaultTarget?: EvalTargetObject;
- renderFooter?: (params: { isLoading: boolean }) => React.ReactNode;
+ renderFooter?: (params: {
+ isLoading: boolean;
+ isSaveDisabled: boolean;
+ }) => React.ReactNode;
oldConfigId?: string;
}) => {
const capture = usePostHogClientCapture();
@@ -465,6 +469,9 @@ export const InnerEvaluatorForm = (props: {
}) as UseFormReturn<EvalFormType>;
const currentMapping = form.watch("mapping") ?? [];
+ const hasJsonPathCompatibilityError = currentMapping.some((mappingRow) =>
+ Boolean(getActiveJsonPathCompatibilityWarning(mappingRow)),
+ );
const syncStatus = useVariableMappingSync({
templateVars: isCodeEvalConfig ? [] : props.evalTemplate?.vars,
currentMapping: currentMapping,
@@ -1340,13 +1347,17 @@ export const InnerEvaluatorForm = (props: {
createJobMutation.isPending || updateJobMutation.isPending;
const formFooter = props.renderFooter ? (
- props.renderFooter({ isLoading: mutationIsLoading })
+ props.renderFooter({
+ isLoading: mutationIsLoading,
+ isSaveDisabled: hasJsonPathCompatibilityError,
+ })
) : (
<div className="flex w-full flex-col items-end gap-4">
{!props.disabled ? (
<Button
type="submit"
loading={mutationIsLoading}
+ disabled={hasJsonPathCompatibilityError}
className="mt-3 max-w-fit"
>
{props.mode === "edit" ? "Update" : "Execute"}
diff --git a/web/src/features/evals/components/variable-mapping-card.tsx b/web/src/features/evals/components/variable-mapping-card.tsx
index e92267a03e..24d5dbf61e 100644
--- a/web/src/features/evals/components/variable-mapping-card.tsx
+++ b/web/src/features/evals/components/variable-mapping-card.tsx
@@ -19,6 +19,7 @@ import { cn } from "@/src/utils/tailwind";
import {
type EvalFormType,
fieldHasJsonSelectorOption,
+ getJsonPathCompatibilityWarning,
} from "@/src/features/evals/utils/evaluator-form-utils";
import { EvalTargetObject } from "@langfuse/shared";
import { VariableMappingDescription } from "@/src/features/evals/components/eval-form-descriptions";
@@ -343,7 +344,7 @@ export const VariableMappingCard = ({
key={`${mappingField.id}-langfuseObject`}
name={`mapping.${index}.langfuseObject`}
render={({ field }) => (
- <div className="flex items-center gap-2">
+ <div className="flex items-start gap-2">
<VariableMappingDescription
title="Object"
description="Langfuse object to retrieve the data from."
@@ -404,7 +405,7 @@ export const VariableMappingCard = ({
(field.value &&
!nameOptions.includes(field.value));
return (
- <div className="flex items-center gap-2">
+ <div className="flex items-start gap-2">
<VariableMappingDescription
title="Object Name"
description="Name of the Langfuse object to retrieve the data from."
@@ -500,7 +501,7 @@ export const VariableMappingCard = ({
key={`${mappingField.id}-selectedColumnId`}
name={`mapping.${index}.selectedColumnId`}
render={({ field }) => (
- <div className="flex items-center gap-2">
+ <div className="flex items-start gap-2">
<VariableMappingDescription
title="Object Field"
description="Field on the Langfuse object to insert into the template."
@@ -563,26 +564,33 @@ export const VariableMappingCard = ({
control={form.control}
key={`${mappingField.id}-jsonSelector`}
name={`mapping.${index}.jsonSelector`}
- render={({ field }) => (
- <div className="flex items-center gap-2">
- <VariableMappingDescription
- title="JsonPath"
- description="Optional selection: Use JsonPath syntax to select from a JSON object stored on a trace. If not selected, we will pass the entire object into the prompt."
- href="https://langfuse.com/docs/evaluation/evaluation-methods/llm-as-a-judge"
- />
- <FormItem className="w-2/3">
- <FormControl>
- <Input
- {...field}
- value={field.value ?? ""}
- disabled={disabled}
- placeholder="Optional"
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- </div>
- )}
+ render={({ field }) => {
+ const compatibilityWarning =
+ getJsonPathCompatibilityWarning(field.value);
+
+ return (
+ <div className="flex items-start gap-2">
+ <VariableMappingDescription
+ title="JsonPath"
+ description="Optional selection: Use JsonPath syntax to select from a JSON object stored on a trace. If not selected, we will pass the entire object into the prompt."
+ href="https://langfuse.com/docs/evaluation/evaluation-methods/llm-as-a-judge"
+ />
+ <FormItem className="w-2/3">
+ <FormControl>
+ <Input
+ {...field}
+ value={field.value ?? ""}
+ disabled={disabled}
+ placeholder="Optional"
+ />
+ </FormControl>
+ <FormMessage>
+ {compatibilityWarning}
+ </FormMessage>
+ </FormItem>
+ </div>
+ );
+ }}
/>
) : undefined}
</Card>
@@ -605,7 +613,7 @@ export const VariableMappingCard = ({
/>
</div>
{hideAdvancedSettings && (
- <div className="flex items-center gap-2">
+ <div className="flex items-start gap-2">
<VariableMappingDescription
title="Object"
description="Type of object to retrieve the data from."
@@ -636,7 +644,7 @@ export const VariableMappingCard = ({
: experimentTargetEvalVariableColumns;
return (
- <div className="flex items-center gap-2">
+ <div className="flex items-start gap-2">
<VariableMappingDescription
title="Object Field"
description="Observation field to insert into the template."
@@ -677,26 +685,33 @@ export const VariableMappingCard = ({
control={form.control}
key={`${mappingField.id}-jsonSelector`}
name={`mapping.${index}.jsonSelector`}
- render={({ field }) => (
- <div className="flex items-center gap-2">
- <VariableMappingDescription
- title="JsonPath"
- description="Optional selection: Use JsonPath syntax to select from a JSON object. If not selected, we will pass the entire object into the prompt."
- href="https://langfuse.com/docs/evaluation/evaluation-methods/llm-as-a-judge"
- />
- <FormItem className="w-2/3">
- <FormControl>
- <Input
- {...field}
- value={field.value ?? ""}
- disabled={disabled}
- placeholder="Optional"
- />
- </FormControl>
- <FormMessage />
- </FormItem>
- </div>
- )}
+ render={({ field }) => {
+ const compatibilityWarning =
+ getJsonPathCompatibilityWarning(field.value);
+
+ return (
+ <div className="flex items-start gap-2">
+ <VariableMappingDescription
+ title="JsonPath"
+ description="Optional selection: Use JsonPath syntax to select from a JSON object. If not selected, we will pass the entire object into the prompt."
+ href="https://langfuse.com/docs/evaluation/evaluation-methods/llm-as-a-judge"
+ />
+ <FormItem className="w-2/3">
+ <FormControl>
+ <Input
+ {...field}
+ value={field.value ?? ""}
+ disabled={disabled}
+ placeholder="Optional"
+ />
+ </FormControl>
+ <FormMessage>
+ {compatibilityWarning}
+ </FormMessage>
+ </FormItem>
+ </div>
+ );
+ }}
/>
)}
</Card>
diff --git a/web/src/features/evals/pages/remap-evaluator.tsx b/web/src/features/evals/pages/remap-evaluator.tsx
index 5e2efae448..664f5a70a8 100644
--- a/web/src/features/evals/pages/remap-evaluator.tsx
+++ b/web/src/features/evals/pages/remap-evaluator.tsx
@@ -248,12 +248,13 @@ export default function RemapEvaluatorPage() {
hideAdvancedSettings={true}
evalCapabilities={evalCapabilities}
oldConfigId={evalConfigId}
- renderFooter={({ isLoading }) => (
+ renderFooter={({ isLoading, isSaveDisabled }) => (
<div className="flex w-full flex-col items-end gap-4">
<div className="flex items-center">
<Button
type="submit"
loading={isLoading}
+ disabled={isSaveDisabled}
className="mt-3 rounded-l-md rounded-r-none"
>
{legacyAction === "keep-active"
diff --git a/web/src/features/evals/utils/evaluator-form-utils.clienttest.ts b/web/src/features/evals/utils/evaluator-form-utils.clienttest.ts
new file mode 100644
index 0000000000..77edddc06f
--- /dev/null
+++ b/web/src/features/evals/utils/evaluator-form-utils.clienttest.ts
@@ -0,0 +1,112 @@
+import { describe, expect, it } from "vitest";
+import {
+ evalConfigFormSchema,
+ getJsonPathCompatibilityWarning,
+} from "./evaluator-form-utils";
+
+describe("getJsonPathCompatibilityWarning", () => {
+ it.each([
+ {
+ selector: "$.items[?(@.status === 'active')]",
+ expected:
+ "Filter expressions ([?...]) are not supported and will not be applied.",
+ },
+ {
+ selector: "$.items[[email protected]]",
+ expected:
+ "Filter expressions ([?...]) are not supported and will not be applied.",
+ },
+ {
+ selector: "$.items[(@.length - 1)]",
+ expected:
+ "Script expressions ([(...)]) are not supported. The evaluator will use the unfiltered value instead.",
+ },
+ {
+ selector: "$.items[-1]",
+ expected:
+ "Negative array indices (for example, [-1]) are not supported. Use a slice such as [-1:] instead.",
+ },
+ {
+ selector: "$[‘items’][[email protected]]",
+ expected:
+ "Filter expressions ([?...]) are not supported and will not be applied.",
+ },
+ ])("warns about unsupported selector $selector", ({ selector, expected }) => {
+ expect(getJsonPathCompatibilityWarning(selector)).toBe(expected);
+ });
+
+ it.each([
+ undefined,
+ "",
+ "$.items[0]",
+ "$.items[-1:]",
+ "$['property[-1]']",
+ "$['property[?(@.active)]']",
+ "$['property[(@.length - 1)]']",
+ '$["Don’t"]',
+ "$['it’s'].a",
+ "$.Don’t",
+ "$[‘items’]",
+ ])("does not warn about supported selector %s", (selector) => {
+ expect(getJsonPathCompatibilityWarning(selector)).toBeNull();
+ });
+});
+
+describe("evalConfigFormSchema", () => {
+ it("blocks configurations with unsupported JSONPath selectors", () => {
+ const result = evalConfigFormSchema.safeParse({
+ scoreName: "Correctness",
+ target: "trace",
+ filter: [],
+ mapping: [
+ {
+ templateVariable: "input",
+ langfuseObject: "trace",
+ objectName: null,
+ selectedColumnId: "input",
+ jsonSelector: "$.items[[email protected]]",
+ },
+ ],
+ sampling: 1,
+ delay: 0,
+ timeScope: ["NEW"],
+ runOnLive: true,
+ });
+
+ expect(result.success).toBe(false);
+ if (!result.success) {
+ expect(result.error.issues).toContainEqual(
+ expect.objectContaining({
+ path: ["mapping", 0, "jsonSelector"],
+ message:
+ "Filter expressions ([?...]) are not supported and will not be applied.",
+ }),
+ );
+ }
+ });
+
+ // A target switch nulls selectedColumnId but keeps jsonSelector, hiding the
+ // JsonPath input and the only place its warning could render.
+ it("ignores unsupported selectors on columns that have no JsonPath input", () => {
+ const result = evalConfigFormSchema.safeParse({
+ scoreName: "Correctness",
+ target: "event",
+ filter: [],
+ mapping: [
+ {
+ templateVariable: "input",
+ langfuseObject: "event",
+ objectName: null,
+ selectedColumnId: null,
+ jsonSelector: "$.items[[email protected]]",
+ },
+ ],
+ sampling: 1,
+ delay: 0,
+ timeScope: ["NEW"],
+ runOnLive: true,
+ });
+
+ expect(result.success).toBe(true);
+ });
+});
diff --git a/web/src/features/evals/utils/evaluator-form-utils.ts b/web/src/features/evals/utils/evaluator-form-utils.ts
index 97b962ce8d..b8d9deae87 100644
--- a/web/src/features/evals/utils/evaluator-form-utils.ts
+++ b/web/src/features/evals/utils/evaluator-form-utils.ts
@@ -13,16 +13,31 @@ import { OUTPUT_MAPPING } from "@/src/features/evals/utils/evaluator-constants";
export const isLegacyEvalTarget = (target: string): boolean =>
target === "trace" || target === "dataset";
-export const evalConfigFormSchema = z.object({
- scoreName: z.string(),
- target: EvalTargetObjectSchema,
- filter: z.array(singleFilter).nullable(), // reusing the filter type from the tables
- mapping: z.array(wipVariableMapping),
- sampling: z.coerce.number().gt(0).lte(1),
- delay: z.coerce.number().min(0).optional().default(10),
- timeScope: TimeScopeSchema,
- runOnLive: z.boolean().optional().default(true),
-});
+export const evalConfigFormSchema = z
+ .object({
+ scoreName: z.string(),
+ target: EvalTargetObjectSchema,
+ filter: z.array(singleFilter).nullable(), // reusing the filter type from the tables
+ mapping: z.array(wipVariableMapping),
+ sampling: z.coerce.number().gt(0).lte(1),
+ delay: z.coerce.number().min(0).optional().default(10),
+ timeScope: TimeScopeSchema,
+ runOnLive: z.boolean().optional().default(true),
+ })
+ .superRefine(({ mapping }, ctx) => {
+ mapping.forEach((mappingRow, index) => {
+ const compatibilityError =
+ getActiveJsonPathCompatibilityWarning(mappingRow);
+
+ if (compatibilityError) {
+ ctx.addIssue({
+ code: "custom",
+ path: ["mapping", index, "jsonSelector"],
+ message: compatibilityError,
+ });
+ }
+ });
+ });
export type EvalFormType = z.infer<typeof evalConfigFormSchema>;
@@ -54,6 +69,72 @@ export const fieldHasJsonSelectorOption = (
selectedColumnId === "experimentItemMetadata" ||
selectedColumnId === "toolCalls";
+const stripJsonPathStringLiterals = (selector: string): string => {
+ let quote: "'" | '"' | "`" | null = null;
+ let isEscaped = false;
+
+ // Ignore expression-like text inside quoted property names to avoid false warnings.
+ return [...selector]
+ .map((character) => {
+ if (quote) {
+ if (isEscaped) {
+ isEscaped = false;
+ } else if (character === "\\") {
+ isEscaped = true;
+ } else if (character === quote) {
+ quote = null;
+ }
+
+ return " ";
+ }
+
+ if (character === "'" || character === '"' || character === "`") {
+ quote = character;
+ return " ";
+ }
+
+ return character;
+ })
+ .join("");
+};
+
+export function getJsonPathCompatibilityWarning(
+ selector: string | null | undefined,
+): string | null {
+ if (!selector) return null;
+
+ const selectorWithoutStrings = stripJsonPathStringLiterals(selector);
+
+ if (/\[\s*\?/u.test(selectorWithoutStrings)) {
+ return "Filter expressions ([?...]) are not supported and will not be applied.";
+ }
+
+ if (/\[\s*\(/u.test(selectorWithoutStrings)) {
+ return "Script expressions ([(...)]) are not supported. The evaluator will use the unfiltered value instead.";
+ }
+
+ if (/\[\s*-\d+\s*\]/u.test(selectorWithoutStrings)) {
+ return "Negative array indices (for example, [-1]) are not supported. Use a slice such as [-1:] instead.";
+ }
+
+ return null;
+}
+
+/**
+ * Only warns while the row's JsonPath input is rendered: a target switch nulls
+ * selectedColumnId but keeps jsonSelector
+ * (`useEvaluatorTarget.transformMapping`), orphaning the value behind a hidden
+ * input. Reporting it then would disable submit with no visible cause.
+ */
+export function getActiveJsonPathCompatibilityWarning(mappingRow: {
+ selectedColumnId?: string | null;
+ jsonSelector?: string | null;
+}): string | null {
+ if (!fieldHasJsonSelectorOption(mappingRow.selectedColumnId)) return null;
+
+ return getJsonPathCompatibilityWarning(mappingRow.jsonSelector);
+}
+
export const getTargetDisplayName = (target: string): string => {
switch (target) {
case "trace":