Skip to content

Conversation

@sunyuhan1998
Copy link
Contributor

@sunyuhan1998 sunyuhan1998 commented Jun 13, 2025

As mentioned in the issue, currently when using the Builder in SystemPromptTemplate, it returns a Builder of PromptTemplate. This causes subsequent operations like createMessage based on the Builder class to actually return a UserMessage instead of a SystemMessage.

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.

Currently, I have adopted the first approach because it has a smaller impact scope compared to the second one.

This PR includes the following changes:

  1. Adjusted the visibility of some fields and methods in the Builder of PromptTemplate to support inheritance in the Builder of SystemPromptTemplate.
  2. Added a Builder for SystemPromptTemplate.
  3. Added relevant unit tests.

Fixes #3526

…uilder()` in `SystemPromptTemplate` returned a Builder for `PromptTemplate` instead.

Signed-off-by: Sun Yuhan <[email protected]>
@ilayaperumalg
Copy link
Member

Since this PR fixes the underlying issue with the Builder in SystemPromptTemplate, we can back port the changes into 1.0.x as well.

@ilayaperumalg ilayaperumalg added bug Something isn't working for: backport-to-1.0.x labels Jul 4, 2025
@ilayaperumalg
Copy link
Member

@sunyuhan1998 Thanks for the PR!
Rebased and merged as dbaa608. Also, back-ported into 1.0.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working for: backport-to-1.0.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Invalid message type created when using SystemPromptTemplate builder

2 participants