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
Builder
inner class inSystemPromptTemplate
that extends theBuilder
class ofPromptTemplate
, and override all of its methods. However, this approach would require updating all subclasses'Builder
classes whenever any new properties are added toPromptTemplate
in the future. -
Make the
Builder
class ofPromptTemplate
generic and use the “Simulated Self-Type Idiom” so that each subclass can return its ownBuilder
type. However, this would involve significant changes to the currentBuilder
class 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.