Bug description
When using SystemPromptTemplate builder, it's expected that an instance of SystemPromptTemplate is being built, not PromptTemplate. However, currently an instance of PromptTemplate is returned. As a result, when createMessage is called, it creates UserMessage instead of SystemMessage:
Environment Spring AI 1.0.0
Steps to reproduce Here's how the issue can be reproduced:
var promptTemplate = SystemPromptTemplate
.builder()
.template("template")
.build();
System.out.println(promptTemplate.getClass());
System.out.println(promptTemplate.createMessage().getClass());
Expected behavior
It is expected that a builder creates an instance of SystemPromptTemplate so that when createMessage is called, an instance of SystemMessage is created.
Comment From: sunyuhan1998
This is indeed an issue, and the key point is that SystemPromptTemplate inherits the Builder from PromptTemplate.
I can think of two ways to address this problem, but in my opinion, neither is perfect:
-
Add a
Builderinner class inSystemPromptTemplatethat extends theBuilderclass ofPromptTemplate, and override all of its methods. However, this approach would require updating all subclasses'Builderclasses whenever any new properties are added toPromptTemplatein the future. -
Make the
Builderclass ofPromptTemplategeneric and use the “Simulated Self-Type Idiom” so that each subclass can return its ownBuildertype. However, this would involve significant changes to the currentBuilderclass ofPromptTemplate, and all existing code that uses it would also need to support generics.
In any case, for now I've submitted a PR: https://github.com/spring-projects/spring-ai/pull/3529 using the first approach to try to fix the issue, as it seems relatively more acceptable.
Comment From: ilayaperumalg
@yargna Thanks for reporting the issue. @sunyuhan1998 Thanks for the quick follow up and the PR!
Comment From: yargna
@sunyuhan1998 @ilayaperumalg Thank you so much for reviewing and addressing this issue! I've upgraded to Spring AI 1.0.1 and I can confirm that SystemPromptTemplate works as expected now!