fix(core): extract variables from inside Mustache sections #8580
+38
−8
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 variablemyvar
inside the conditional block was not being recognized as an input variable. TheinputVariables
array would only contain["name", "isTrue"]
instead of the expected["name", "isTrue", "myvar"]
.Root Cause:
The
mustacheTemplateToNodes
function inlangchain-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:
mustacheTemplateToNodes
to recursively process nested content inside sectionsImpact:
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 extractionlangchain-core/src/prompts/tests/chat.mustache.test.ts
- Updated test expectationslangchain-core/src/prompts/tests/prompt.mustache.test.ts
- Updated test expectationsFixes #8456