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 <mark.pollack@broadcom.com>
This commit is contained in:
Mark Pollack
2025-04-17 15:47:12 -04:00
parent 6e0c09838f
commit 5bca99546b

View File

@@ -61,18 +61,18 @@ import static org.springframework.ai.util.LoggingMarkers.SENSITIVE_DATA_MARKER;
*/
public class BeanOutputConverter<T> implements StructuredOutputConverter<T> {
private final Logger logger = LoggerFactory.getLogger(BeanOutputConverter.class);
protected final Logger logger = LoggerFactory.getLogger(BeanOutputConverter.class);
/**
* The target class type reference to which the output will be converted.
*/
private final Type type;
protected final Type type;
/** The object mapper used for deserialization and other JSON operations. */
private final ObjectMapper objectMapper;
/** Holds the generated JSON schema for the target type. */
private String jsonSchema;
protected String jsonSchema;
/**
* Constructor to initialize with the target type's class.
@@ -128,7 +128,7 @@ public class BeanOutputConverter<T> implements StructuredOutputConverter<T> {
/**
* Generates the JSON schema for the target type.
*/
private void generateSchema() {
protected void generateSchema() {
JacksonModule jacksonModule = new JacksonModule(JacksonOption.RESPECT_JSONPROPERTY_REQUIRED,
JacksonOption.RESPECT_JSONPROPERTY_ORDER);
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(
@@ -206,7 +206,16 @@ public class BeanOutputConverter<T> implements StructuredOutputConverter<T> {
*/
@Override
public String getFormat() {
String template = """
return String.format(getFormatTemplate(), this.jsonSchema);
}
/**
* Provides the template for the format instruction. Subclasses can override this
* method to customize the instruction format.
* @return The format template string.
*/
protected String getFormatTemplate() {
return """
Your response should be in JSON format.
Do not include any explanations, only provide a RFC8259 compliant JSON response following this format without deviation.
Do not include markdown code blocks in your response.
@@ -214,7 +223,6 @@ public class BeanOutputConverter<T> implements StructuredOutputConverter<T> {
Here is the JSON Schema instance your output must adhere to:
```%s```
""";
return String.format(template, this.jsonSchema);
}
/**