bug(sdk-js): getLangchainPrompt drops the closing brace of nested JSON
Describe the bug
getLangchainPrompt() drops the final closing brace when a prompt contains nested JSON. The model then receives malformed JSON.
Prompt text: Reply with JSON like {"user": {"name": "{{name}}"}}
getLangchainPrompt() returns Reply with JSON like {{"user": {{"name": "{name}"}} (two escaped opening braces, one escaped closing pair), and PromptTemplate.fromTemplate(...).format({ name: "Ann" }) renders Reply with JSON like {"user": {"name": "Ann"}. Deeper nesting loses one brace per }} run. Chat prompts are affected the same way.
Cause: escapeJsonForLangchain in packages/client/src/prompt/promptClients.ts treats every }} as already escaped and passes it through, even when the first } closes a JSON brace it doubled earlier.
Steps to reproduce
- Create a text prompt with the content above.
const prompt = await langfuse.prompt.get(...);PromptTemplate.fromTemplate(prompt.getLangchainPrompt()).format({ name: "Ann" })- The output is missing the last
}.
Langfuse Cloud or self-hosted?
Langfuse Cloud
SDK and integration versions
@langfuse/client 5.11.1, @langchain/core
Are you interested in contributing a fix for this bug?
Source: langfuse/langfuse