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:

  1. Add a Builder inner class in SystemPromptTemplate that extends the Builder class of PromptTemplate, and override all of its methods. However, this approach would require updating all subclasses' Builder classes whenever any new properties are added to PromptTemplate in the future.

  2. Make the Builder class of PromptTemplate generic and use the “Simulated Self-Type Idiom” so that each subclass can return its own Builder type. However, this would involve significant changes to the current Builder class of PromptTemplate, 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.