openai-apispring-ai

No converter for org.springframework.ai.openai.api.ResponseFormat


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_OBJECT that guarantees the message the model generates is valid JSON or JSON_SCHEMA with a supplied schema that guarantees the model will generate a response that matches your supplied schema (spring.ai.openai.chat.options.responseFormat option).

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

Question

What did I wrong? How can I configure the response format in my application.yml?


Solution

  • 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