Skip to content

Make BeanOutputConverter more extensible #2788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@
*/
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.
Expand Down Expand Up @@ -128,7 +128,7 @@ private BeanOutputConverter(Type type, ObjectMapper objectMapper) {
/**
* 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(
Expand Down Expand Up @@ -206,15 +206,23 @@ protected ObjectMapper getObjectMapper() {
*/
@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.
Remove the ```json markdown from the output.
Here is the JSON Schema instance your output must adhere to:
```%s```
""";
return String.format(template, this.jsonSchema);
}

/**
Expand Down