feat: add keepMissingVariables option to StTemplateRenderer #5099
+174
−2
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.
PR Description
This PR introduces an option to preserve missing template variables as their original placeholders (e.g.
{var}) instead of letting StringTemplate render them as empty strings.This behavior is only available when
validationModeis set toNONE. The feature is disabled by default and does not affect existing usage; it is only enabled whenkeepMissingVariables(true)is explicitly configured on the renderer.Why Do We Need It?
This is useful for:
In the context of building agents, especially multi-agent systems and Agentic RAG systems, a common pattern is to feed the output of one agent into the prompt of another. The original output or example prompts often contain JSON blocks or other structures with braces, for example:
{ "sections": [ { "index": 1, "title": "Intro" } ] }The existing
StTemplateRendererandNoOpTemplateRenderercannot fully address this scenario when parts of the prompt are meant to remain unchanged across stages. Since this is a high-frequency usage pattern, it is helpful to enhanceStTemplateRendererwith an option that preserves unresolved placeholders instead of silently stripping them.Behavior
When
keepMissingVariables(true)is set (withvalidationMode = NONE), any variables that are referenced in the template but not provided in the variables map are rendered as their original placeholder text (e.g.{var}) instead of being rendered as empty strings.For example:
If only
input_datais provided in the variables map, the rendered result will keep the JSON block unchanged while replacing{input_data}with its value.