Bug description
While attempting to pass complex objects as parameters to a PromptTemplate
I discovered that the in built validation does not properly handle field references and likely other things that StTemplate natively supports.
Environment Java Version: OpenJdk 17.0.16 Spring AI Version: 1.0.2
Steps to reproduce With a complex test object like this:
class TestClass {
private final String name;
public TestClass(final String name) {
this.name = name;
}
public String getName() {
return name;
}
}
And a unit like:
@Test
public void testPromptTemplate() {
final PromptTemplate prompt = PromptTemplate.builder()
.template("{test.name}")
.build();
final String result = prompt.render(Map.ofEntries(Map.entry("test", new TestClass("test"))));
assertEquals("test", result);
}
You get a failing test with a result like:
Not all variables were replaced in the template. Missing variable names are: [name].
java.lang.IllegalStateException: Not all variables were replaced in the template. Missing variable names are: [name].
at org.springframework.ai.template.st.StTemplateRenderer.validate(StTemplateRenderer.java:140)
at org.springframework.ai.template.st.StTemplateRenderer.apply(StTemplateRenderer.java:108)
at org.springframework.ai.chat.prompt.PromptTemplate.render(PromptTemplate.java:137)
This seems to happen with any of the more advanced features of StTemplate.
Expected behavior The test passes.
If you change the definition of the PromptTemplate call to:
final PromptTemplate prompt = PromptTemplate.builder()
.template("{test.name}")
.renderer(StTemplateRenderer.builder()
.validationMode(ValidationMode.NONE)
.build())
.build();
The test passes as expected.
Minimal Complete Reproducible example See above.
Comment From: sunyuhan1998
@arlaneenalra Thank you for your report. I've submitted a PR to address this issue—you can follow it here: #4499