Skip to content

Conversation

christian-bromann
Copy link
Member

This PR fixes a bug in Mustache template parsing where variables inside conditional sections were not being extracted as input variables.

Problem:
When using Mustache templates with conditional sections like "Hey {{name}}, {{#isTrue}}{{myvar}}{{/isTrue}}", the variable myvar inside the conditional block was not being recognized as an input variable. The inputVariables array would only contain ["name", "isTrue"] instead of the expected ["name", "isTrue", "myvar"].

Root Cause:
The mustacheTemplateToNodes function in langchain-core/src/prompts/template.ts was not recursively processing nested content inside Mustache sections. It only extracted the section variable (isTrue) but ignored variables inside the section (myvar).

Solution:

  • Modified mustacheTemplateToNodes to recursively process nested content inside sections
  • Ensures all variables are properly extracted regardless of whether they appear inside conditional blocks
  • Updated test expectations to reflect the new comprehensive variable extraction behavior
  • Fixed test template spacing to match actual Mustache output

Impact:
This fix ensures that variables inside Mustache conditional sections are properly extracted as input variables when they are provided as top-level input values, improving the robustness of Mustache template parsing.

Files Changed:

  • langchain-core/src/prompts/template.ts - Core fix for recursive variable extraction
  • langchain-core/src/prompts/tests/chat.mustache.test.ts - Updated test expectations
  • langchain-core/src/prompts/tests/prompt.mustache.test.ts - Updated test expectations

Fixes #8456

- Modified mustacheTemplateToNodes to recursively process nested content inside sections
- Fixes issue where variables inside conditional blocks (e.g., {{#isTrue}}{{myvar}}{{/isTrue}}) were not being extracted as input variables
- Updated test expectations to reflect the new comprehensive variable extraction behavior
- Removed trailing space from test template to match expected output

This ensures that variables like 'myvar' inside Mustache conditional sections are properly extracted as input variables when they are provided as top-level input values.

fixes langchain-ai#8456
Copy link

vercel bot commented Jul 29, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
langchainjs-docs ✅ Ready (Inspect) Visit Preview Jul 29, 2025 0:36am
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchainjs-api-refs ⬜️ Ignored (Inspect) Jul 29, 2025 0:36am

@hntrl hntrl merged commit ff22c45 into langchain-ai:main Jul 31, 2025
36 checks passed
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.

Mustache variables in conditional blocks not extracted in inputVariables

2 participants