🪢 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(shared): interpolate unicode variables in compileTemplateString (#17662)
João Vítor Sberseed9ca367b85f
diff --git a/packages/shared/src/utils/prompts.test.ts b/packages/shared/src/utils/prompts.test.tsindex cf8ad13d9f..b18f8d62e8 100644--- a/packages/shared/src/utils/prompts.test.ts+++ b/packages/shared/src/utils/prompts.test.ts@@ -1,5 +1,6 @@import { describe, expect, it } from "vitest";import { compileEvalPrompt, compileTemplateString } from "./prompts";+import { extractVariables } from "./stringChecks";describe("compileTemplateString", () => {it("interpolates supported placeholders and preserves missing ones", () => {@@ -21,6 +22,24 @@ describe("compileTemplateString", () => {});});+describe("compileTemplateString unicode variables", () => {+ it.each(["réponse", "名前", "вопрос", "a1_é"])(+ "interpolates every variable that extractVariables accepts: %s",+ (name) => {+ const template = `Judge {{${name}}}`;++ expect(extractVariables(template)).toEqual([name]);+ expect(compileTemplateString(template, { [name]: "ok" })).toBe(+ "Judge ok",+ );+ },+ );++ it("preserves unicode placeholders missing from the context", () => {+ expect(compileTemplateString("{{réponse}}", {})).toBe("{{réponse}}");+ });+});+describe("compileEvalPrompt", () => {it("serializes structured evaluator variables before interpolation", () => {expect(diff --git a/packages/shared/src/utils/prompts.ts b/packages/shared/src/utils/prompts.tsindex 7f34dda925..f9efa87e4d 100644--- a/packages/shared/src/utils/prompts.ts+++ b/packages/shared/src/utils/prompts.ts@@ -16,12 +16,15 @@ export function compileTemplateString(context: Record<string, unknown>,) {try {- return template.replace(/{{\s*([\w.]+)\s*}}/g, (match, key: string) => {- if (!(key in context)) return match;+ return template.replace(+ /\{\{\s*([\p{L}\p{N}_.]+)\s*\}\}/gu,+ (match, key: string) => {+ if (!(key in context)) return match;- const value = context[key];- return value === undefined || value === null ? "" : String(value);- });+ const value = context[key];+ return value === undefined || value === null ? "" : String(value);+ },+ );} catch {return template;}