🪢 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
chore(utils): remove handlebars dependency (#10866)
Hassieb Pakzad859881b05032
diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex df886af9cc..fda3c7937c 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -968,9 +968,6 @@ importers:express:specifier: ^5.2.1version: 5.2.1- handlebars:- specifier: ^4.7.8- version: 4.7.8helmet:specifier: ^7.1.0version: 7.1.0@@ -25540,6 +25537,7 @@ snapshots:wordwrap: 1.0.0optionalDependencies:uglify-js: 3.18.0+ optional: true@@ -31233,7 +31231,8 @@ snapshots:triple-beam: 1.4.1winston-transport: 4.7.1- [email protected]: {}+ optional: truedependencies:diff --git a/web/src/features/playground/page/components/Variables.tsx b/web/src/features/playground/page/components/Variables.tsxindex 3072cbdb57..bca36bfe4e 100644--- a/web/src/features/playground/page/components/Variables.tsx+++ b/web/src/features/playground/page/components/Variables.tsx@@ -10,7 +10,7 @@ export const Variables = () => {<div className="text-xs"><p className="mb-2">No variables defined.</p><p>- Use handlebars in your prompts to add a variable:+ Use double curly braces in your prompts to add a variable:{{exampleVariable}}</p></div>diff --git a/worker/package.json b/worker/package.jsonindex 1c9b897f8b..52a314d322 100644--- a/worker/package.json+++ b/worker/package.json@@ -49,7 +49,6 @@"dotenv": "^16.4.5","exponential-backoff": "^3.1.2","express": "^5.2.1",- "handlebars": "^4.7.8","helmet": "^7.1.0","ioredis": "^5.8.2","jsonpath-plus": "10.3.0",diff --git a/worker/src/__tests__/evalService.test.ts b/worker/src/__tests__/evalService.test.tsindex aaf0b117f3..26e2a8c94b 100644--- a/worker/src/__tests__/evalService.test.ts+++ b/worker/src/__tests__/evalService.test.ts@@ -25,7 +25,7 @@ import Decimal from "decimal.js";import { sql } from "kysely";import { afterEach } from "node:test";import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";-import { compileHandlebarString } from "../features/utils/utilities";+import { compileTemplateString } from "../features/utils/utilities";import { OpenAIServer } from "./network";import {createEvalJobs,@@ -73,14 +73,509 @@ afterAll(openAIServer.teardown);describe("eval service tests", () => {describe("compile prompt", () => {- test("compile handlebars template", async () => {+ test("compile template string with basic variables", async () => {const template = "Please evaluate toxicity {{input}} {{output}}";- const compiledString = compileHandlebarString(template, {+ const compiledString = compileTemplateString(template, {input: "foo",output: "bar",});expect(compiledString).toBe("Please evaluate toxicity foo bar");});++ test("compile template string with single variable", async () => {+ const template = "Hello {{name}}!";+ const compiledString = compileTemplateString(template, {+ name: "World",+ });+ expect(compiledString).toBe("Hello World!");+ });++ test("compile template string with no variables", async () => {+ const template = "This is a plain string without variables";+ const compiledString = compileTemplateString(template, {+ input: "foo",+ });+ expect(compiledString).toBe("This is a plain string without variables");+ });++ test("compile template string with empty context keeps original variables", async () => {+ const template = "Hello {{name}}!";+ const compiledString = compileTemplateString(template, {});+ expect(compiledString).toBe("Hello {{name}}!");+ });++ test("compile template string with missing variable keeps original variable", async () => {+ const template = "Hello {{name}}, your score is {{score}}";+ const compiledString = compileTemplateString(template, {+ name: "Alice",+ });+ expect(compiledString).toBe("Hello Alice, your score is {{score}}");+ });++ test("compile template string with all variables missing keeps all original", async () => {+ const template = "{{greeting}} {{name}}, welcome to {{place}}!";+ const compiledString = compileTemplateString(template, {});+ expect(compiledString).toBe(+ "{{greeting}} {{name}}, welcome to {{place}}!",+ );+ });++ test("compile template string with whitespace variable missing keeps original with whitespace", async () => {+ const template = "Hello {{ name }}!";+ const compiledString = compileTemplateString(template, {});+ expect(compiledString).toBe("Hello {{ name }}!");+ });++ test("compile template string with null value returns empty string", async () => {+ const template = "Value: {{value}}";+ const compiledString = compileTemplateString(template, {+ value: null,+ });+ expect(compiledString).toBe("Value: ");+ });++ test("compile template string with undefined value returns empty string", async () => {+ const template = "Value: {{value}}";+ const compiledString = compileTemplateString(template, {+ value: undefined,+ });+ expect(compiledString).toBe("Value: ");+ });++ test("compile template string with numeric value", async () => {+ const template = "Score: {{score}}";+ const compiledString = compileTemplateString(template, {+ score: 42,+ });+ expect(compiledString).toBe("Score: 42");+ });++ test("compile template string with zero value", async () => {+ const template = "Count: {{count}}";+ const compiledString = compileTemplateString(template, {+ count: 0,+ });+ expect(compiledString).toBe("Count: 0");+ });++ test("compile template string with boolean true value", async () => {+ const template = "Active: {{active}}";+ const compiledString = compileTemplateString(template, {+ active: true,+ });+ expect(compiledString).toBe("Active: true");+ });++ test("compile template string with boolean false value", async () => {+ const template = "Active: {{active}}";+ const compiledString = compileTemplateString(template, {+ active: false,+ });+ expect(compiledString).toBe("Active: false");+ });++ test("compile template string with object value stringifies object", async () => {+ const template = "Data: {{data}}";+ const compiledString = compileTemplateString(template, {+ data: { key: "value" },+ });+ expect(compiledString).toBe("Data: [object Object]");+ });++ test("compile template string with array value stringifies array", async () => {+ const template = "Items: {{items}}";+ const compiledString = compileTemplateString(template, {+ items: [1, 2, 3],+ });+ expect(compiledString).toBe("Items: 1,2,3");+ });++ test("compile template string with whitespace around variable name", async () => {+ const template = "Hello {{ name }}!";+ const compiledString = compileTemplateString(template, {+ name: "World",+ });+ expect(compiledString).toBe("Hello World!");+ });++ test("compile template string with extra whitespace around variable name", async () => {+ const template = "Hello {{ name }}!";+ const compiledString = compileTemplateString(template, {+ name: "World",+ });+ expect(compiledString).toBe("Hello World!");+ });++ test("compile template string with tabs around variable name", async () => {+ const template = "Hello {{\tname\t}}!";+ const compiledString = compileTemplateString(template, {+ name: "World",+ });+ expect(compiledString).toBe("Hello World!");+ });++ test("compile template string with mixed whitespace around variable name", async () => {+ const template = "Hello {{ \t name \t }}!";+ const compiledString = compileTemplateString(template, {+ name: "World",+ });+ expect(compiledString).toBe("Hello World!");+ });++ test("compile template string with newline inside", async () => {+ const template = "Hello {{\nname\n}}!";+ const compiledString = compileTemplateString(template, {+ name: "World",+ });+ // Newlines are not matched by \s* in the regex pattern (which only matches spaces)+ expect(compiledString).toBe("Hello World!");+ });++ test("compile template string with leading whitespace only", async () => {+ const template = "Hello {{ name}}!";+ const compiledString = compileTemplateString(template, {+ name: "World",+ });+ expect(compiledString).toBe("Hello World!");+ });++ test("compile template string with trailing whitespace only", async () => {+ const template = "Hello {{name }}!";+ const compiledString = compileTemplateString(template, {+ name: "World",+ });+ expect(compiledString).toBe("Hello World!");+ });++ test("compile template string with excessive whitespace", async () => {+ const template = "Hello {{ name }}!";+ const compiledString = compileTemplateString(template, {+ name: "World",+ });+ expect(compiledString).toBe("Hello World!");+ });++ test("compile template string whitespace variations with multiple variables", async () => {+ const template = "{{ input }} and {{output}} and {{ expected }}";+ const compiledString = compileTemplateString(template, {+ input: "A",+ output: "B",+ expected: "C",+ });+ expect(compiledString).toBe("A and B and C");+ });++ test("compile template string with multiple occurrences of same variable", async () => {+ const template = "{{name}} said hello to {{name}}";+ const compiledString = compileTemplateString(template, {+ name: "Alice",+ });+ expect(compiledString).toBe("Alice said hello to Alice");+ });++ test("compile template string with special characters in value", async () => {+ const template = "Query: {{query}}";+ const compiledString = compileTemplateString(template, {+ query: "SELECT * FROM users WHERE name = 'test'",+ });+ expect(compiledString).toBe(+ "Query: SELECT * FROM users WHERE name = 'test'",+ );+ });++ test("compile template string with HTML in value (no escaping)", async () => {+ const template = "Content: {{content}}";+ const compiledString = compileTemplateString(template, {+ content: "<script>alert('xss')</script>",+ });+ expect(compiledString).toBe("Content: <script>alert('xss')</script>");+ });++ test("compile template string with newlines in value", async () => {+ const template = "Message: {{message}}";+ const compiledString = compileTemplateString(template, {+ message: "Line 1\nLine 2\nLine 3",+ });+ expect(compiledString).toBe("Message: Line 1\nLine 2\nLine 3");+ });++ test("compile template string with empty string value", async () => {+ const template = "Name: {{name}}";+ const compiledString = compileTemplateString(template, {+ name: "",+ });+ expect(compiledString).toBe("Name: ");+ });++ test("compile template string with JSON string value", async () => {+ const template = "Data: {{data}}";+ const jsonData = JSON.stringify({ input: "test", output: "result" });+ const compiledString = compileTemplateString(template, {+ data: jsonData,+ });+ expect(compiledString).toBe('Data: {"input":"test","output":"result"}');+ });++ test("compile template string with underscore in variable name", async () => {+ const template = "Value: {{my_variable}}";+ const compiledString = compileTemplateString(template, {+ my_variable: "test",+ });+ expect(compiledString).toBe("Value: test");+ });++ test("compile template string with numbers in variable name", async () => {+ const template = "Value: {{var1}}";+ const compiledString = compileTemplateString(template, {+ var1: "test",+ });+ expect(compiledString).toBe("Value: test");+ });++ test("compile template string with dot notation in variable name", async () => {+ const template = "Value: {{user.name}}";+ const compiledString = compileTemplateString(template, {+ "user.name": "Alice",+ });+ expect(compiledString).toBe("Value: Alice");+ });++ test("compile template string preserves curly braces that are not variables", async () => {+ const template = "JSON: {key: value} and {{variable}}";+ const compiledString = compileTemplateString(template, {+ variable: "test",+ });+ expect(compiledString).toBe("JSON: {key: value} and test");+ });++ test("compile template string with triple braces preserves outer braces", async () => {+ const template = "{{{variable}}}";+ const compiledString = compileTemplateString(template, {+ variable: "test",+ });+ expect(compiledString).toBe("{test}");+ });++ test("compile template string with complex LLM eval prompt", async () => {+ const template = `You are an expert evaluator. Please evaluate the following:++Input: {{input}}+Output: {{output}}+Expected: {{expected}}++Provide a score from 0 to 1 based on correctness.`;++ const compiledString = compileTemplateString(template, {+ input: "What is 2+2?",+ output: "4",+ expected: "4",+ });++ expect(compiledString)+ .toBe(`You are an expert evaluator. Please evaluate the following:++Input: What is 2+2?+Output: 4+Expected: 4++Provide a score from 0 to 1 based on correctness.`);+ });++ test("compile template string with very long value", async () => {+ const template = "Content: {{content}}";+ const longValue = "a".repeat(10000);+ const compiledString = compileTemplateString(template, {+ content: longValue,+ });+ expect(compiledString).toBe(`Content: ${longValue}`);+ });++ test("compile template string with unicode characters in value", async () => {+ const template = "Message: {{message}}";+ const compiledString = compileTemplateString(template, {+ message: "Hello 世界 🌍 مرحبا",+ });+ expect(compiledString).toBe("Message: Hello 世界 🌍 مرحبا");+ });++ test("compile template string with unicode characters in template", async () => {+ const template = "你好 {{name}} 🎉";+ const compiledString = compileTemplateString(template, {+ name: "World",+ });+ expect(compiledString).toBe("你好 World 🎉");+ });++ test("compile template string returns original on regex error", async () => {+ // This test verifies error handling - the function should gracefully handle errors+ const template = "Test {{var}}";+ // Normal context should work+ const compiledString = compileTemplateString(template, {+ var: "value",+ });+ expect(compiledString).toBe("Test value");+ });++ test("compile template string with many variables", async () => {+ const template =+ "{{a}} {{b}} {{c}} {{d}} {{e}} {{f}} {{g}} {{h}} {{i}} {{j}}";+ const compiledString = compileTemplateString(template, {+ a: "1",+ b: "2",+ c: "3",+ d: "4",+ e: "5",+ f: "6",+ g: "7",+ h: "8",+ i: "9",+ j: "10",+ });+ expect(compiledString).toBe("1 2 3 4 5 6 7 8 9 10");+ });++ test("compile template string with adjacent variables", async () => {+ const template = "{{first}}{{second}}{{third}}";+ const compiledString = compileTemplateString(template, {+ first: "A",+ second: "B",+ third: "C",+ });+ expect(compiledString).toBe("ABC");+ });++ test("compile template string with variable at start", async () => {+ const template = "{{greeting}} there!";+ const compiledString = compileTemplateString(template, {+ greeting: "Hello",+ });+ expect(compiledString).toBe("Hello there!");+ });++ test("compile template string with variable at end", async () => {+ const template = "Hello {{name}}";+ const compiledString = compileTemplateString(template, {+ name: "World",+ });+ expect(compiledString).toBe("Hello World");+ });++ test("compile template string with only variable", async () => {+ const template = "{{value}}";+ const compiledString = compileTemplateString(template, {+ value: "Just this",+ });+ expect(compiledString).toBe("Just this");+ });++ test("compile template string with empty template", async () => {+ const template = "";+ const compiledString = compileTemplateString(template, {+ value: "test",+ });+ expect(compiledString).toBe("");+ });++ test("compile template string with float value", async () => {+ const template = "Score: {{score}}";+ const compiledString = compileTemplateString(template, {+ score: 0.95,+ });+ expect(compiledString).toBe("Score: 0.95");+ });++ test("compile template string with negative number", async () => {+ const template = "Temperature: {{temp}}";+ const compiledString = compileTemplateString(template, {+ temp: -15,+ });+ expect(compiledString).toBe("Temperature: -15");+ });++ test("compile template string with Infinity value", async () => {+ const template = "Value: {{val}}";+ const compiledString = compileTemplateString(template, {+ val: Infinity,+ });+ expect(compiledString).toBe("Value: Infinity");+ });++ test("compile template string with NaN value", async () => {+ const template = "Value: {{val}}";+ const compiledString = compileTemplateString(template, {+ val: NaN,+ });+ expect(compiledString).toBe("Value: NaN");+ });++ test("compile template string escapes regex special characters in values", async () => {+ const template = "Pattern: {{pattern}}";+ const compiledString = compileTemplateString(template, {+ pattern: ".*+?^${}()|[]\\",+ });+ expect(compiledString).toBe("Pattern: .*+?^${}()|[]\\");+ });++ test("compile template string with backslash in value", async () => {+ const template = "Path: {{path}}";+ const compiledString = compileTemplateString(template, {+ path: "C:\\Users\\test",+ });+ expect(compiledString).toBe("Path: C:\\Users\\test");+ });++ test("compile template string with dollar sign in value", async () => {+ const template = "Price: {{price}}";+ const compiledString = compileTemplateString(template, {+ price: "$100",+ });+ expect(compiledString).toBe("Price: $100");+ });++ test("compile template string does not process nested braces as variables", async () => {+ const template = "Code: {{ {{nested}} }}";+ const compiledString = compileTemplateString(template, {+ nested: "value",+ });+ // The outer braces should match the inner {{nested}} first+ expect(compiledString).toBe("Code: {{ value }}");+ });++ test("compile template string handles real-world eval template", async () => {+ const template = `You are evaluating an AI assistant's response.++## Task+Evaluate how well the assistant's response matches the expected output.++## Input+The user asked: {{input}}++## Assistant's Response+{{output}}++## Expected Response+{{expected_output}}++## Instructions+1. Compare the assistant's response with the expected output+2. Consider semantic similarity, not just exact matching+3. Provide a score from 0.0 to 1.0++Respond with JSON: {"score": <number>, "reasoning": "<explanation>"}`;++ const compiledString = compileTemplateString(template, {+ input: "What is the capital of France?",+ output: "The capital of France is Paris.",+ expected_output: "Paris is the capital of France.",+ });++ expect(compiledString).toContain("What is the capital of France?");+ expect(compiledString).toContain("The capital of France is Paris.");+ expect(compiledString).toContain("Paris is the capital of France.");+ expect(compiledString).not.toContain("{{input}}");+ expect(compiledString).not.toContain("{{output}}");+ expect(compiledString).not.toContain("{{expected_output}}");+ });});describe("create eval jobs", () => {diff --git a/worker/src/__tests__/experimentsService.test.ts b/worker/src/__tests__/experimentsService.test.tsindex a08b024b8e..bed080124e 100644--- a/worker/src/__tests__/experimentsService.test.ts+++ b/worker/src/__tests__/experimentsService.test.ts@@ -6,15 +6,6 @@ import { LLMAdapter } from "@langfuse/shared";import { encrypt } from "@langfuse/shared/encryption";import { createExperimentJobClickhouse } from "../features/experiments/experimentServiceClickhouse";import { logger } from "@langfuse/shared/src/server";-import { fetchLLMCompletion } from "@langfuse/shared/src/server";--// Mock LLM completion call-vi.mock("../features/utils/utilities", () => ({- compileHandlebarString: vi.fn().mockImplementation((str, context) => {- // Simple mock that replaces handlebars variables with their values- return str.replace(/\{\{(\w+)\}\}/g, (_, key) => context[key] || "");- }),-}));// Mock the logger to capture log callsvi.mock("@langfuse/shared/src/server", async () => {diff --git a/worker/src/features/evaluation/evalService.ts b/worker/src/features/evaluation/evalService.tsindex 2e2e38dc30..7d99be5d8e 100644--- a/worker/src/features/evaluation/evalService.ts+++ b/worker/src/features/evaluation/evalService.ts@@ -52,7 +52,7 @@ import {DatasetItem,} from "@langfuse/shared";import { kyselyPrisma, prisma } from "@langfuse/shared/src/db";-import { compileHandlebarString, createW3CTraceId } from "../utils";+import { compileTemplateString, createW3CTraceId } from "../utils";import { env } from "../../env";import { JSONPath } from "jsonpath-plus";import { UnrecoverableError } from "../../errors/UnrecoverableError";@@ -709,7 +709,7 @@ export const evaluate = async ({// compile the prompt and send out the LLM requestlet prompt;try {- prompt = compileHandlebarString(template.prompt, {+ prompt = compileTemplateString(template.prompt, {...Object.fromEntries(mappingResult.map(({ var: key, value }) => [key, value]),),diff --git a/worker/src/features/experiments/utils.ts b/worker/src/features/experiments/utils.tsindex cb98674350..d2fdac7f46 100644--- a/worker/src/features/experiments/utils.ts+++ b/worker/src/features/experiments/utils.ts@@ -11,7 +11,7 @@ import {PromptType,stringifyValue,} from "@langfuse/shared";-import { compileHandlebarString } from "../utils/utilities";+import { compileTemplateString } from "../utils/utilities";import {logger,PromptService,@@ -81,16 +81,16 @@ export const replaceVariablesInPrompt = (}const processContent = (content: string) => {- // Extract only Handlebars variables from itemInput (exclude message placeholders)+ // Extract only template variables from itemInput (exclude message placeholders)const filteredContext = Object.fromEntries(Object.entries(itemInput).filter(([key]) => variables.includes(key) && !placeholderNames.includes(key),),);- // Apply Handlebars ONLY if the content contains `{{variable}}` pattern+ // Apply template ONLY if the content contains `{{variable}}` patternif (content.includes("{{")) {- return compileHandlebarString(content, filteredContext);+ return compileTemplateString(content, filteredContext);}return content; // Return original content if no placeholders are found};diff --git a/worker/src/features/utils/utilities.ts b/worker/src/features/utils/utilities.tsindex da02d0fbba..fbe2d1b20a 100644--- a/worker/src/features/utils/utilities.ts+++ b/worker/src/features/utils/utilities.ts@@ -1,17 +1,22 @@import crypto from "node:crypto";import { logger } from "@langfuse/shared/src/server";-import Handlebars from "handlebars";-export function compileHandlebarString(- handlebarString: string,+export function compileTemplateString(+ template: string,context: Record<string, any>,): string {try {- const template = Handlebars.compile(handlebarString, { noEscape: true });- return template(context);+ return template.replace(/{{\s*([\w.]+)\s*}}/g, (match, key) => {+ if (key in context) {+ const value = context[key];+ return value === undefined || value === null ? "" : String(value);+ }+ return match; // missing key → return original variable including its braces+ });} catch (error) {- logger.info("Handlebars compilation error:", error);- return handlebarString; // Fallback to the original string if Handlebars fails+ logger.info("Template compilation error:", error);++ return template;}}