I have a Spring Boot (version 3.4.2) application with Spring AI and I want to get only JSON responses from OpenAI, so I configured it in my application.yml file, see Built-in JSON mode:
Some AI Models provide dedicated configuration options to generate structured (usually JSON) output.
- OpenAI Structured Outputs can ensure your model generates responses conforming strictly to your provided JSON Schema. You can choose between the
JSON_OBJECTthat guarantees the message the model generates is valid JSON orJSON_SCHEMAwith a supplied schema that guarantees the model will generate a response that matches your supplied schema (spring.ai.openai.chat.options.responseFormatoption).
Unfortunately, it is not working.
Configuration
spring:
ai:
openai:
chat:
options:
model: gpt-4o-mini
temperature: 0.0
responseFormat: JSON_SCHEMA
Logs
Failed to bind properties under 'spring.ai.openai.chat.options.response-format' to org.springframework.ai.openai.api.ResponseFormat:
Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonProperty org.springframework.ai.openai.api.ResponseFormat]
Research
I looked into ResponseFormat and ResponseFormat.Type and changed JSON_SCHEMA to json_schema but it didn't help.
I read Using the Chat Options Builder and tried to add the response format programmatically. But then I can no longer use BeanOutputConverter and have to write the JSON schema manually instead of getting it dynamically from the Java class.
Question
What did I wrong? How can I configure the response format in my application.yml?
The name of the property is spring.ai.openai.chat.options.response-format.type, see Configuring via Application Properties:
Configuring via Application Properties
Alternatively, when using the OpenAI auto-configuration, you can configure the desired response format through the following chat application properties:
spring.ai.openai.api-key=YOUR_API_KEY spring.ai.openai.chat.options.model=gpt-4o-mini spring.ai.openai.chat.options.response-format.type=JSON_SCHEMA spring.ai.openai.chat.options.response-format.name=MySchemaName spring.ai.openai.chat.options.response-format.schema={"type":"object","properties":{"steps":{"type":"array","items":{"type":"object","properties":{"explanation":{"type":"string"},"output":{"type":"string"}},"required":["explanation","output"],"additionalProperties":false}},"final_answer":{"type":"string"}},"required":["steps","final_answer"],"additionalProperties":false} spring.ai.openai.chat.options.response-format.strict=true
The modified application.yml:
spring:
ai:
openai:
chat:
options:
model: gpt-4o-mini
temperature: 0.0
response-format:
type: json-schema