🪢 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(prompts): cap resolved prompt size during dependency resolution (#19631)
Hassieb Pakzad7d83af70e106
diff --git a/packages/shared/src/server/services/PromptService/index.ts b/packages/shared/src/server/services/PromptService/index.tsindex bda50ca828..1743c06853 100644--- a/packages/shared/src/server/services/PromptService/index.ts+++ b/packages/shared/src/server/services/PromptService/index.ts@@ -17,6 +17,9 @@ import {import { ParsedPromptDependencyTag } from "../../../features/prompts/parsePromptDependencyTags";export const MAX_PROMPT_NESTING_DEPTH = 5;+// Measured on the JSON-serialized prompt. A dependency tag repeated N times+// expands to N copies of the child, so this bounds the resolved output.+export const MAX_RESOLVED_PROMPT_LENGTH = 5_000_000;type PrismaClientOrTransaction = PrismaClient | Prisma.TransactionClient;export class PromptService {@@ -423,6 +426,16 @@ export class PromptService {.slice(1, -1) // this is necessary to avoid parsing errors as resolved value is unstringified.replace(/\$/g, "$$$$"); // Escape dollar signs in replacement string. $ has special meaning in replace(), so we need to escape it with $$. Since we're in a string that will be used as a replacement, we need to double escape it (hence $$$$)+ const occurrences = resolvedPrompt.match(regex)?.length ?? 0;+ if (+ resolvedPrompt.length + occurrences * replaceValue.length >+ MAX_RESOLVED_PROMPT_LENGTH+ ) {+ throw new LangfuseConflictError(+ `Resolved prompt exceeds maximum size (${MAX_RESOLVED_PROMPT_LENGTH} characters)`,+ );+ }+resolvedPrompt = resolvedPrompt.replace(regex, replaceValue);}diff --git a/web/src/__tests__/server/prompts.v2.servertest.ts b/web/src/__tests__/server/prompts.v2.servertest.tsindex 1d7fdbdef2..2cc28a1d28 100644--- a/web/src/__tests__/server/prompts.v2.servertest.ts+++ b/web/src/__tests__/server/prompts.v2.servertest.ts@@ -21,6 +21,7 @@ import {createOrgProjectAndApiKey,getObservationById,MAX_PROMPT_NESTING_DEPTH,+ MAX_RESOLVED_PROMPT_LENGTH,ChatMessageType,PromptService,redis,@@ -3321,6 +3322,46 @@ describe("PATCH api/public/v2/prompts/[promptName]/versions/[version]", () => {"Maximum nesting depth exceeded",);}, 10_000);++ it("should reject a prompt whose resolved content exceeds MAX_RESOLVED_PROMPT_LENGTH", async () => {+ const { auth: newAuth } = await createOrgProjectAndApiKey();++ const childLength = 100_000;+ const childResponse = await makeAPICall(+ "POST",+ baseURI,+ { name: "large-child", prompt: "x".repeat(childLength), type: "text" },+ newAuth,+ );+ expect(childResponse.status).toBe(201);++ const tag = "@@@langfusePrompt:name=large-child|version=1@@@";+ const repetitions =+ Math.floor(MAX_RESOLVED_PROMPT_LENGTH / childLength) + 1;+ const createResponse = await makeAPICall(+ "POST",+ baseURI,+ { name: "amplifier", prompt: tag.repeat(repetitions), type: "text" },+ newAuth,+ );++ expect(createResponse.status).toBe(400);+ expect(JSON.stringify(createResponse.body)).toContain(+ "Resolved prompt exceeds maximum size",+ );++ const withinLimitResponse = await makeAPICall(+ "POST",+ baseURI,+ {+ name: "within-limit",+ prompt: tag.repeat(repetitions - 2),+ type: "text",+ },+ newAuth,+ );+ expect(withinLimitResponse.status).toBe(201);+ });});describe("DELETE /api/public/v2/prompts/:promptName", () => {