Skip to content

fix(langchain): escape empty json braces in prompts#1734

Open
sjsjsjjs534 wants to merge 1 commit into
langfuse:mainfrom
sjsjsjjs534:codex/escape-empty-langchain-braces
Open

fix(langchain): escape empty json braces in prompts#1734
sjsjsjjs534 wants to merge 1 commit into
langfuse:mainfrom
sjsjsjjs534:codex/escape-empty-langchain-braces

Conversation

@sjsjsjjs534

@sjsjsjjs534 sjsjsjjs534 commented Jul 2, 2026

Copy link
Copy Markdown

Summary

  • Treat bare empty JSON object braces as literal braces when converting Langfuse prompts for LangChain.
  • Add a regression test covering {}, { }, and multiline empty-object literals alongside normal LangChain variables.

Fixes langfuse/langfuse#14721

Verification

  • RED: UV_CACHE_DIR=.uv-cache uv run --frozen pytest tests/unit/test_prompt_compilation.py::TestLangchainPromptCompilation::test_langchain_prompt_escapes_bare_empty_json_objects -q (failed with LangChain formatting IndexError before the fix)
  • UV_CACHE_DIR=.uv-cache uv run --frozen pytest tests/unit/test_prompt_compilation.py::TestLangchainPromptCompilation::test_langchain_prompt_escapes_bare_empty_json_objects -q
  • UV_CACHE_DIR=.uv-cache uv run --frozen pytest tests/unit/test_prompt_compilation.py -q
  • UV_CACHE_DIR=.uv-cache uv run --frozen ruff check .
  • UV_CACHE_DIR=.uv-cache uv run --frozen ruff format --check langfuse/model.py tests/unit/test_prompt_compilation.py
  • UV_CACHE_DIR=.uv-cache uv run --frozen mypy langfuse --no-error-summary
  • git diff --check

Greptile Summary

This PR fixes a crash when Langfuse prompts containing bare empty JSON objects ({}, { }, or {\n}) are converted for use with LangChain. Previously, an empty {} would reach LangChain as an unescaped single-brace pair, causing an IndexError during .format().

  • Fix (langfuse/model.py): A one-character addition — "}" is added to the lookahead set that classifies an opening { as JSON-related, so empty objects are now doubled to {{}} before reaching LangChain's template formatter.
  • Regression test: Covers the three empty-object forms ({}, { }, {\n}) mixed with both Langfuse-style ({{test_var}}) and LangChain-style ({langchain_var}) variables in a single prompt.

Confidence Score: 5/5

Safe to merge — the change is minimal and targeted, and the added test directly reproduces the previously crashing scenario.

The logic change is a single-character addition to a lookahead character set inside a well-isolated static method. Adding } to the set that marks an opening brace as JSON-related correctly handles all three empty-object forms. Existing behaviour for non-empty JSON objects and LangChain variable placeholders is unaffected.

No files require special attention.

Reviews (1): Last reviewed commit: "fix(langchain): escape empty json braces..." | Re-trigger Greptile

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


zhangsixiao seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants