Skip to content

Commit 5bca995

Browse files
committed
Make BeanOutputConverter more extensible
This change improves the extensibility of BeanOutputConverter by: - Making generateSchema method and jsonSchema field protected - Making logger and type fields protected for subclass access - Extracting format template into a separate protected method These changes allow subclasses to access and override key parts of the converter's behavior without having to reimplement large portions of the class. Fixes #1985 Signed-off-by: Mark Pollack <[email protected]>
1 parent 6e0c098 commit 5bca995

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

spring-ai-client-chat/src/main/java/org/springframework/ai/converter/BeanOutputConverter.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@
6161
*/
6262
public class BeanOutputConverter<T> implements StructuredOutputConverter<T> {
6363

64-
private final Logger logger = LoggerFactory.getLogger(BeanOutputConverter.class);
64+
protected final Logger logger = LoggerFactory.getLogger(BeanOutputConverter.class);
6565

6666
/**
6767
* The target class type reference to which the output will be converted.
6868
*/
69-
private final Type type;
69+
protected final Type type;
7070

7171
/** The object mapper used for deserialization and other JSON operations. */
7272
private final ObjectMapper objectMapper;
7373

7474
/** Holds the generated JSON schema for the target type. */
75-
private String jsonSchema;
75+
protected String jsonSchema;
7676

7777
/**
7878
* Constructor to initialize with the target type's class.
@@ -128,7 +128,7 @@ private BeanOutputConverter(Type type, ObjectMapper objectMapper) {
128128
/**
129129
* Generates the JSON schema for the target type.
130130
*/
131-
private void generateSchema() {
131+
protected void generateSchema() {
132132
JacksonModule jacksonModule = new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_REQUIRED,
133133
JacksonOption.RESPECT_JSONPROPERTY_ORDER);
134134
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(
@@ -206,15 +206,23 @@ protected ObjectMapper getObjectMapper() {
206206
*/
207207
@Override
208208
public String getFormat() {
209-
String template = """
209+
return String.format(getFormatTemplate(), this.jsonSchema);
210+
}
211+
212+
/**
213+
* Provides the template for the format instruction. Subclasses can override this
214+
* method to customize the instruction format.
215+
* @return The format template string.
216+
*/
217+
protected String getFormatTemplate() {
218+
return """
210219
Your response should be in JSON format.
211220
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
212221
Do not include markdown code blocks in your response.
213222
Remove the ```json markdown from the output.
214223
Here is the JSON Schema instance your output must adhere to:
215224
```%s```
216225
""";
217-
return String.format(template, this.jsonSchema);
218226
}
219227

220228
/**

0 commit comments

Comments
 (0)