Description
When using SystemPromptTemplate
in Spring AI, any usage of double curly braces like {{ this is my line }}
gets removed entirely in the final output, instead of escaping to { this is my line }
or remaining as-is.
Sample code
String template = "{{ this is my line }}";
SystemPromptTemplate promptTemplate = new SystemPromptTemplate(template);
String result = promptTemplate.create(Map.of()).getContents();
System.out.println(result);
Expected Output
{ this is my line }
Actual Output
this is my line
Comment From: sunyuhan1998
Hi @SaurabhDesaiCeloxis , the SystemPromptTemplate
uses StringTemplate4
as the rendering engine by default. The default syntax in StringTemplate4
uses <
and >
as delimiters for expressions, and if {}
is used inside <...>
, it represents an anonymous sub-template. However, in SystemPromptTemplate
, the default delimiters for expressions have been declared as {
and }
(instead of the default <
and >
).
Now let's look at your question: Why does the template {{ this is my line }}
render to this is my line
? This is because during rendering, the outer {}
is treated as the expression delimiter, and the inner {}
is interpreted as an anonymous sub-template, which results in outputting this is my line
.
We can achieve the same behavior using native StringTemplate4
by using the default delimiters for expressions:
ST st = new ST("<{this is my line}>");
String rendered = st.render();
System.out.println(rendered); // this is my line
In Spring AI, to solve your issue, you simply need to escape with a backslash \
:
String template = "\\{ this is my line \\}";
SystemPromptTemplate promptTemplate = new SystemPromptTemplate(template);
String result = promptTemplate.create(Map.of()).getContents();
System.out.println(result); // { this is my line }