Skip to content

Feature (template): Supports multi character template separators #3801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ void whenCustomTemplateRendererIsProvidedThenItIsUsedForRendering() {
String systemText = "Instructions <name>";
Map<String, Object> systemParams = Map.of("name", "Spring AI");
TemplateRenderer customRenderer = StTemplateRenderer.builder()
.startDelimiterToken('<')
.endDelimiterToken('>')
.startDelimiterToken("<")
.endDelimiterToken(">")
.build();
ChatModel chatModel = mock(ChatModel.class);
DefaultChatClient.DefaultChatClientRequestSpec inputRequest = (DefaultChatClient.DefaultChatClientRequestSpec) ChatClient
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
/**
* Copyright 2023-2025 the original author or authors.
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -16,16 +16,20 @@

package org.springframework.ai.template.st;

import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.antlr.runtime.Token;
import org.antlr.runtime.TokenStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.compiler.Compiler;
import org.stringtemplate.v4.compiler.STException;
import org.stringtemplate.v4.compiler.STLexer;

import org.springframework.ai.template.TemplateRenderer;
Expand Down Expand Up @@ -57,106 +61,126 @@ public class StTemplateRenderer implements TemplateRenderer {

private static final String VALIDATION_MESSAGE = "Not all variables were replaced in the template. Missing variable names are: %s.";

private static final char DEFAULT_START_DELIMITER_TOKEN = '{';
private static final String DEFAULT_START_DELIMITER = "{";

private static final char DEFAULT_END_DELIMITER_TOKEN = '}';
private static final String DEFAULT_END_DELIMITER = "}";

private static final ValidationMode DEFAULT_VALIDATION_MODE = ValidationMode.THROW;

private static final boolean DEFAULT_VALIDATE_ST_FUNCTIONS = false;

private final char startDelimiterToken;
private final String startDelimiterToken;

private final char endDelimiterToken;
private final String endDelimiterToken;

private final ValidationMode validationMode;

private final boolean validateStFunctions;

/**
* Constructs a new {@code StTemplateRenderer} with the specified delimiter tokens,
* validation mode, and function validation flag.
* @param startDelimiterToken the character used to denote the start of a template
* variable (e.g., '{')
* @param endDelimiterToken the character used to denote the end of a template
* variable (e.g., '}')
* @param validationMode the mode to use for template variable validation; must not be
* null
* @param validateStFunctions whether to validate StringTemplate functions in the
* template
* Constructs a StTemplateRenderer with custom delimiters, validation mode, and
* function validation flag.
* @param startDelimiterToken Multi-character start delimiter (non-null/non-empty)
* @param endDelimiterToken Multi-character end delimiter (non-null/non-empty)
* @param validationMode Mode for handling missing variables (non-null)
* @param validateStFunctions Whether to treat ST built-in functions as variables
*/
public StTemplateRenderer(char startDelimiterToken, char endDelimiterToken, ValidationMode validationMode,
public StTemplateRenderer(String startDelimiterToken, String endDelimiterToken, ValidationMode validationMode,
boolean validateStFunctions) {
Assert.notNull(validationMode, "validationMode cannot be null");
Assert.notNull(validationMode, "validationMode must not be null");
Assert.hasText(startDelimiterToken, "startDelimiterToken must not be null or empty");
Assert.hasText(endDelimiterToken, "endDelimiterToken must not be null or empty");

this.startDelimiterToken = startDelimiterToken;
this.endDelimiterToken = endDelimiterToken;
this.validationMode = validationMode;
this.validateStFunctions = validateStFunctions;
}

/**
* Renders the template by first converting custom delimiters to ST's native format,
* then replacing variables.
* @param template Template string with variables (non-null/non-empty)
* @param variables Map of variable names to values (non-null, keys must not be null)
* @return Rendered string with variables replaced
*/
@Override
public String apply(String template, Map<String, Object> variables) {
Assert.hasText(template, "template cannot be null or empty");
Assert.notNull(variables, "variables cannot be null");
Assert.noNullElements(variables.keySet(), "variables keys cannot be null");
Assert.hasText(template, "template must not be null or empty");
Assert.notNull(variables, "variables must not be null");
Assert.noNullElements(variables.keySet(), "variables keys must not contain null");

try {
String processedTemplate = preprocessTemplate(template);
ST st = new ST(processedTemplate, '{', '}');
variables.forEach(st::add);

ST st = createST(template);
for (Map.Entry<String, Object> entry : variables.entrySet()) {
st.add(entry.getKey(), entry.getValue());
if (validationMode != ValidationMode.NONE) {
validate(st, variables);
}

return st.render();
}
if (this.validationMode != ValidationMode.NONE) {
validate(st, variables);
catch (STException e) {
throw new IllegalArgumentException("Failed to render template", e);
}
return st.render();
}

private ST createST(String template) {
try {
return new ST(template, this.startDelimiterToken, this.endDelimiterToken);
}
catch (Exception ex) {
throw new IllegalArgumentException("The template string is not valid.", ex);
/**
* Converts custom delimiter-wrapped variables (e.g., <name>) to ST's native format
* ({name}).
*/
private String preprocessTemplate(String template) {
if ("{".equals(startDelimiterToken) && "}".equals(endDelimiterToken)) {
return template;
}
String escapedStart = Pattern.quote(startDelimiterToken);
String escapedEnd = Pattern.quote(endDelimiterToken);
String variablePattern = escapedStart + "([a-zA-Z_][a-zA-Z0-9_]*)" + escapedEnd;
return template.replaceAll(variablePattern, "{$1}");
}

/**
* Validates that all required template variables are provided in the model. Returns
* the set of missing variables for further handling or logging.
* @param st the StringTemplate instance
* @param templateVariables the provided variables
* @return set of missing variable names, or empty set if none are missing
* Validates that all template variables have been provided in the variables map.
*/
private Set<String> validate(ST st, Map<String, Object> templateVariables) {
private void validate(ST st, Map<String, Object> templateVariables) {
Set<String> templateTokens = getInputVariables(st);
Set<String> modelKeys = templateVariables != null ? templateVariables.keySet() : new HashSet<>();
Set<String> modelKeys = templateVariables != null ? templateVariables.keySet() : Collections.emptySet();
Set<String> missingVariables = new HashSet<>(templateTokens);
missingVariables.removeAll(modelKeys);

if (!missingVariables.isEmpty()) {
if (this.validationMode == ValidationMode.WARN) {
logger.warn(VALIDATION_MESSAGE.formatted(missingVariables));
String message = VALIDATION_MESSAGE.formatted(missingVariables);
if (validationMode == ValidationMode.WARN) {
logger.warn(message);
}
else if (this.validationMode == ValidationMode.THROW) {
throw new IllegalStateException(VALIDATION_MESSAGE.formatted(missingVariables));
else if (validationMode == ValidationMode.THROW) {
throw new IllegalStateException(message);
}
}
return missingVariables;
}

/**
* Extracts variable names from the template using ST's token stream and regex
* validation.
*/
private Set<String> getInputVariables(ST st) {
TokenStream tokens = st.impl.tokens;
Set<String> inputVariables = new HashSet<>();
TokenStream tokens = st.impl.tokens;
boolean isInsideList = false;

// StringTemplate 控制结构关键字集合,校验时忽略它们
Set<String> stKeywords = Set.of("if", "elseif", "else", "endif", "for", "endfor", "while", "endwhile", "switch",
"endswitch", "case", "default");

for (int i = 0; i < tokens.size(); i++) {
Token token = tokens.get(i);

// Handle list variables with option (e.g., {items; separator=", "})
if (token.getType() == STLexer.LDELIM && i + 1 < tokens.size()
&& tokens.get(i + 1).getType() == STLexer.ID) {
if (i + 2 < tokens.size() && tokens.get(i + 2).getType() == STLexer.COLON) {
String text = tokens.get(i + 1).getText();
if (!Compiler.funcs.containsKey(text) || this.validateStFunctions) {
if ((!Compiler.funcs.containsKey(text) || validateStFunctions) && !stKeywords.contains(text)) {
inputVariables.add(text);
isInsideList = true;
}
Expand All @@ -165,34 +189,46 @@ private Set<String> getInputVariables(ST st) {
else if (token.getType() == STLexer.RDELIM) {
isInsideList = false;
}
// Only add IDs that are not function calls (i.e., not immediately followed by
else if (!isInsideList && token.getType() == STLexer.ID) {
boolean isFunctionCall = (i + 1 < tokens.size() && tokens.get(i + 1).getType() == STLexer.LPAREN);
boolean isDotProperty = (i > 0 && tokens.get(i - 1).getType() == STLexer.DOT);
// Only add as variable if:
// - Not a function call
// - Not a built-in function used as property (unless validateStFunctions)
if (!isFunctionCall && (!Compiler.funcs.containsKey(token.getText()) || this.validateStFunctions
|| !(isDotProperty && Compiler.funcs.containsKey(token.getText())))) {
inputVariables.add(token.getText());
String tokenText = token.getText();
if (!isFunctionCall && (!Compiler.funcs.containsKey(tokenText) || validateStFunctions
|| !(isDotProperty && Compiler.funcs.containsKey(tokenText)))) {
if (!stKeywords.contains(tokenText)) {
inputVariables.add(tokenText);
}
}
}
}

Pattern varPattern = Pattern.compile(Pattern.quote("{") + "([a-zA-Z_][a-zA-Z0-9_]*)" + Pattern.quote("}"));
Matcher matcher = varPattern.matcher(st.impl.template);
while (matcher.find()) {
String var = matcher.group(1);
if (!stKeywords.contains(var)) {
inputVariables.add(var);
}
}

return inputVariables;
}

/**
* Creates a builder for configuring StTemplateRenderer instances.
*/
public static Builder builder() {
return new Builder();
}

/**
* Builder for configuring and creating {@link StTemplateRenderer} instances.
* Builder for fluent configuration of StTemplateRenderer.
*/
public static final class Builder {

private char startDelimiterToken = DEFAULT_START_DELIMITER_TOKEN;
private String startDelimiterToken = DEFAULT_START_DELIMITER;

private char endDelimiterToken = DEFAULT_END_DELIMITER_TOKEN;
private String endDelimiterToken = DEFAULT_END_DELIMITER;

private ValidationMode validationMode = DEFAULT_VALIDATION_MODE;

Expand All @@ -202,65 +238,42 @@ private Builder() {
}

/**
* Sets the character used as the start delimiter for template expressions.
* Default is '{'.
* @param startDelimiterToken The start delimiter character.
* @return This builder instance for chaining.
* Sets the multi-character start delimiter (e.g., "{{" or "<").
*/
public Builder startDelimiterToken(char startDelimiterToken) {
public Builder startDelimiterToken(String startDelimiterToken) {
this.startDelimiterToken = startDelimiterToken;
return this;
}

/**
* Sets the character used as the end delimiter for template expressions. Default
* is '}'.
* @param endDelimiterToken The end delimiter character.
* @return This builder instance for chaining.
* Sets the multi-character end delimiter (e.g., "}}" or ">").
*/
public Builder endDelimiterToken(char endDelimiterToken) {
public Builder endDelimiterToken(String endDelimiterToken) {
this.endDelimiterToken = endDelimiterToken;
return this;
}

/**
* Sets the validation mode to control behavior when the provided variables do not
* match the variables required by the template. Default is
* {@link ValidationMode#THROW}.
* @param validationMode The desired validation mode.
* @return This builder instance for chaining.
* Sets the validation mode for missing variables.
*/
public Builder validationMode(ValidationMode validationMode) {
this.validationMode = validationMode;
return this;
}

/**
* Configures the renderer to support StringTemplate's built-in functions during
* validation.
* <p>
* When enabled (set to true), identifiers in the template that match known ST
* function names (e.g., "first", "rest", "length") will not be treated as
* required input variables during validation.
* <p>
* When disabled (default, false), these identifiers are treated like regular
* variables and must be provided in the input map if validation is enabled
* ({@link ValidationMode#WARN} or {@link ValidationMode#THROW}).
* @return This builder instance for chaining.
* Enables validation of ST built-in functions (treats them as variables).
*/
public Builder validateStFunctions() {
this.validateStFunctions = true;
return this;
}

/**
* Builds and returns a new {@link StTemplateRenderer} instance with the
* configured settings.
* @return A configured {@link StTemplateRenderer}.
* Builds the configured StTemplateRenderer instance.
*/
public StTemplateRenderer build() {
return new StTemplateRenderer(this.startDelimiterToken, this.endDelimiterToken, this.validationMode,
this.validateStFunctions);
return new StTemplateRenderer(startDelimiterToken, endDelimiterToken, validationMode, validateStFunctions);
}

}
Expand Down
Loading