Skip to content

bug: get_langchain_prompt(): empty braces {} are not escaped and break LangChain templates #14721

Description

@lionjashari

Describe the bug

The heuristic in BasePromptClient._escape_json_for_langchain only treats a { as JSON when the next non-whitespace character is a quote, so empty braces {} (and { }) are left unescaped and still break LangChain templates.

Input to ChatPromptTemplate is missing variables {''}. Expected: ['', 'changes_description', ...]

Empty braces are common in prompts that document JSON option fields must be a dict (can be empty {})

Steps to reproduce

from string import Formatter
from langfuse.model import BasePromptClient

for text in ['can be empty {}', 'spaced { } too', '{"col": "x"}']:
out = BasePromptClient._get_langchain_prompt_string(text)
vars = [f for _, f, _, _ in Formatter().parse(out) if f is not None]
print(f"{text!r} -> {out!r} -> template vars {vars}")

'# 'can be empty {}' -> 'can be empty {}' -> [''] ← breaks ChatPromptTemplate
'# 'spaced { } too' -> 'spaced { } too' -> [' '] ← breaks ChatPromptTemplate
'# '{"col": "x"}' -> '{{"col": "x"}}' -> [] ← correctly escaped

Langfuse Cloud or self-hosted?

Self-hosted

If self-hosted, what version are you running?

4.9.0

SDK and integration versions

No response

Additional information

No response

Are you interested in contributing a fix for this bug?

Yes

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions