Skip to content

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

fix: prompt var validation in form should not block (#1659)

Marc Klingene938b33bd4b6
diff --git a/web/src/features/prompts/components/NewPromptForm/index.tsx b/web/src/features/prompts/components/NewPromptForm/index.tsx
index 84a1cf44db..ee26c5ff4c 100644
--- a/web/src/features/prompts/components/NewPromptForm/index.tsx
+++ b/web/src/features/prompts/components/NewPromptForm/index.tsx
@@ -28,7 +28,7 @@ import {
} from "@/src/features/prompts/server/validation";
import useProjectIdFromURL from "@/src/hooks/useProjectIdFromURL";
import { api } from "@/src/utils/api";
-import { extractVariables } from "@/src/utils/string";
+import { extractVariables, getIsCharOrUnderscore } from "@/src/utils/string";
import { zodResolver } from "@hookform/resolvers/zod";
import type { Prompt } from "@langfuse/shared";
import { PromptChatMessages } from "./PromptChatMessages";
@@ -37,7 +37,6 @@ import {
type NewPromptFormSchemaType,
PromptContentSchema,
type PromptContentType,
- getIsCharOrUnderscore,
} from "./validation";
import { Input } from "@/src/components/ui/input";
import Link from "next/link";
diff --git a/web/src/features/prompts/components/NewPromptForm/validation.ts b/web/src/features/prompts/components/NewPromptForm/validation.ts
index a9c11a86ed..0612276537 100644
--- a/web/src/features/prompts/components/NewPromptForm/validation.ts
+++ b/web/src/features/prompts/components/NewPromptForm/validation.ts
@@ -1,5 +1,4 @@
import { z } from "zod";
-import { extractVariables } from "@/src/utils/string";
import { PromptType } from "@/src/features/prompts/server/validation";
import { ChatMessageRole } from "@langfuse/shared";
@@ -9,13 +8,7 @@ const ChatMessageSchema = z.object({
});
export const ChatMessageListSchema = z.array(ChatMessageSchema);
-export const TextPromptSchema = z
- .string()
- .min(1, "Enter a prompt")
- .refine(
- validateVariables,
- "Variables must only contain letters and underscores (_)",
- );
+export const TextPromptSchema = z.string().min(1, "Enter a prompt");
const NewPromptBaseSchema = z.object({
name: z.string().min(1, "Enter a name"),
@@ -30,9 +23,6 @@ const NewChatPromptSchema = NewPromptBaseSchema.extend({
chatPrompt: ChatMessageListSchema.refine(
(messages) => messages.every((message) => message.content.length > 0),
"Enter a chat message or remove the empty message",
- ).refine(
- (messages) => validateVariables(messages.map((m) => m.content).join("\n")),
- "Variables must only contain letters and underscores (_)",
),
textPrompt: z.string(),
});
@@ -61,16 +51,6 @@ export const PromptContentSchema = z.union([
]);
export type PromptContentType = z.infer<typeof PromptContentSchema>;
-export function getIsCharOrUnderscore(value: string): boolean {
- const charOrUnderscore = /^[A-Za-z_]+$/;
-
- return charOrUnderscore.test(value);
-}
-
-function validateVariables(content: string): boolean {
- return extractVariables(content).every(getIsCharOrUnderscore);
-}
-
function validateJson(content: string): boolean {
try {
JSON.parse(content);
diff --git a/web/src/utils/string.ts b/web/src/utils/string.ts
index 7cf052bbc3..cb04b49387 100644
--- a/web/src/utils/string.ts
+++ b/web/src/utils/string.ts
@@ -10,6 +10,16 @@ export function truncate(str: string, n: number = 16) {
return str;
}
+export function getIsCharOrUnderscore(value: string): boolean {
+ const charOrUnderscore = /^[A-Za-z_]+$/;
+
+ return charOrUnderscore.test(value);
+}
+
+export function validateVariables(content: string): boolean {
+ return extractVariables(content).every(getIsCharOrUnderscore);
+}
+
export function extractVariables(mustacheString: string): string[] {
const mustacheRegex = /\{\{(.*?)\}\}/g;
const uniqueVariables = new Set<string>();
@@ -18,5 +28,12 @@ export function extractVariables(mustacheString: string): string[] {
uniqueVariables.add(match[1]);
}
+ for (const variable of uniqueVariables) {
+ // if validated fails, remove from set
+ if (!getIsCharOrUnderscore(variable)) {
+ uniqueVariables.delete(variable);
+ }
+ }
+
return Array.from(uniqueVariables);
}