Skip to content

Implement Support for JSON_SCHEMA in Azure OpenAI response_format Options #2754

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

Closed
Show file tree
Hide file tree
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
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -34,6 +34,8 @@
import com.azure.ai.openai.models.ChatCompletionsFunctionToolDefinition;
import com.azure.ai.openai.models.ChatCompletionsFunctionToolDefinitionFunction;
import com.azure.ai.openai.models.ChatCompletionsJsonResponseFormat;
import com.azure.ai.openai.models.ChatCompletionsJsonSchemaResponseFormat;
import com.azure.ai.openai.models.ChatCompletionsJsonSchemaResponseFormatJsonSchema;
import com.azure.ai.openai.models.ChatCompletionsOptions;
import com.azure.ai.openai.models.ChatCompletionsResponseFormat;
import com.azure.ai.openai.models.ChatCompletionsTextResponseFormat;
Expand Down Expand Up @@ -901,7 +903,14 @@ private ChatCompletionsOptions copy(ChatCompletionsOptions fromOptions) {
* @return Azure response format
*/
private ChatCompletionsResponseFormat toAzureResponseFormat(AzureOpenAiResponseFormat responseFormat) {
if (responseFormat == AzureOpenAiResponseFormat.JSON) {
if (responseFormat.getType() == AzureOpenAiResponseFormat.Type.JSON_SCHEMA) {
ChatCompletionsJsonSchemaResponseFormatJsonSchema jsonSchema = new ChatCompletionsJsonSchemaResponseFormatJsonSchema(
responseFormat.getJsonSchema().getName());
jsonSchema.setSchema(BinaryData.fromObject(responseFormat.getJsonSchema().getSchema()));
jsonSchema.setStrict(responseFormat.getJsonSchema().getStrict());
return new ChatCompletionsJsonSchemaResponseFormat(jsonSchema);
}
else if (responseFormat.getType() == AzureOpenAiResponseFormat.Type.JSON_OBJECT) {
return new ChatCompletionsJsonResponseFormat();
}
return new ChatCompletionsTextResponseFormat();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,27 +16,233 @@

package org.springframework.ai.azure.openai;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.ai.model.ModelOptionsUtils;

import java.util.Map;
import java.util.Objects;

/**
* Utility enumeration for representing the response format that may be requested from the
* Azure OpenAI model. Please check <a href=
* "https://platform.openai.com/docs/api-reference/chat/create#chat-create-response_format">OpenAI
* API documentation</a> for more details.
* Azure OpenAI model. Please check
* <a href= "https://learn.microsoft.com/en-us/azure/ai-services/openai/reference"> Azure
* OpenAI API documentation</a> for more details.
*
* @author Jonghoon Park
*/
public enum AzureOpenAiResponseFormat {

// default value used by OpenAI
TEXT,
/*
* From the OpenAI API documentation: Compatability: Compatible with GPT-4 Turbo and
* all GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106. Caveats: This enables JSON
* mode, which guarantees the message the model generates is valid JSON. Important:
* when using JSON mode, you must also instruct the model to produce JSON yourself via
* a system or user message. Without this, the model may generate an unending stream
* of whitespace until the generation reaches the token limit, resulting in a
* long-running and seemingly "stuck" request. Also note that the message content may
* be partially cut off if finish_reason="length", which indicates the generation
* exceeded max_tokens or the conversation exceeded the max context length.
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AzureOpenAiResponseFormat {

/**
* Type Must be one of 'text', 'json_object' or 'json_schema'.
*/
@JsonProperty("type")
private Type type;

public AzureOpenAiResponseFormat() {

}

/**
* JSON schema object that describes the format of the JSON object. Only applicable
* when type is 'json_schema'.
*/
@JsonProperty("json_schema")
private JsonSchema jsonSchema;

public Type getType() {
return this.type;
}

public JsonSchema getJsonSchema() {
return this.jsonSchema;
}

public static Builder builder() {
return new Builder();
}

private String schema;

private AzureOpenAiResponseFormat(Type type, JsonSchema jsonSchema) {
this.type = type;
this.jsonSchema = jsonSchema;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AzureOpenAiResponseFormat that = (AzureOpenAiResponseFormat) o;
return this.type == that.type && Objects.equals(this.jsonSchema, that.jsonSchema);
}

@Override
public int hashCode() {
return Objects.hash(this.type, this.jsonSchema);
}

@Override
public String toString() {
return "AzureOpenAiResponseFormat{" + "type=" + this.type + ", jsonSchema=" + this.jsonSchema + '}';
}

public static final class Builder {

private Type type;

private JsonSchema jsonSchema;

private Builder() {
}

public Builder type(Type type) {
this.type = type;
return this;
}

public Builder jsonSchema(JsonSchema jsonSchema) {
this.jsonSchema = jsonSchema;
return this;
}

public Builder jsonSchema(String jsonSchema) {
this.jsonSchema = JsonSchema.builder().schema(jsonSchema).build();
return this;
}

public AzureOpenAiResponseFormat build() {
return new AzureOpenAiResponseFormat(this.type, this.jsonSchema);
}

}

public enum Type {

/**
* Generates a text response. (default)
*/
@JsonProperty("text")
TEXT,

/**
* Enables JSON mode, which guarantees the message the model generates is valid
* JSON.
*/
@JsonProperty("json_object")
JSON_OBJECT,

/**
* Enables Structured Outputs which guarantees the model will match your supplied
* JSON schema.
*/
@JsonProperty("json_schema")
JSON_SCHEMA

}

/**
* JSON schema object that describes the format of the JSON object. Applicable for the
* 'json_schema' type only.
*/
JSON
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class JsonSchema {

@JsonProperty("name")
private String name;

@JsonProperty("schema")
private Map<String, Object> schema;

@JsonProperty("strict")
private Boolean strict;

public JsonSchema() {

}

public String getName() {
return this.name;
}

public Map<String, Object> getSchema() {
return this.schema;
}

public Boolean getStrict() {
return this.strict;
}

private JsonSchema(String name, Map<String, Object> schema, Boolean strict) {
this.name = name;
this.schema = schema;
this.strict = strict;
}

public static Builder builder() {
return new Builder();
}

@Override
public int hashCode() {
return Objects.hash(this.name, this.schema, this.strict);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
JsonSchema that = (JsonSchema) o;
return Objects.equals(this.name, that.name) && Objects.equals(this.schema, that.schema)
&& Objects.equals(this.strict, that.strict);
}

public static final class Builder {

private String name = "custom_schema";

private Map<String, Object> schema;

private Boolean strict = true;

private Builder() {
}

public Builder name(String name) {
this.name = name;
return this;
}

public Builder schema(Map<String, Object> schema) {
this.schema = schema;
return this;
}

public Builder schema(String schema) {
this.schema = ModelOptionsUtils.jsonToMap(schema);
return this;
}

public Builder strict(Boolean strict) {
this.strict = strict;
return this;
}

public JsonSchema build() {
return new JsonSchema(this.name, this.schema, this.strict);
}

}

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -68,7 +68,7 @@ public void createRequestWithChatOptions() {
.logprobs(true)
.topLogprobs(5)
.enhancements(mockAzureChatEnhancementConfiguration)
.responseFormat(AzureOpenAiResponseFormat.TEXT)
.responseFormat(AzureOpenAiResponseFormat.builder().type(AzureOpenAiResponseFormat.Type.TEXT).build())
.build();

var client = AzureOpenAiChatModel.builder()
Expand Down Expand Up @@ -114,7 +114,8 @@ public void createRequestWithChatOptions() {
.logprobs(true)
.topLogprobs(4)
.enhancements(anotherMockAzureChatEnhancementConfiguration)
.responseFormat(AzureOpenAiResponseFormat.JSON)
.responseFormat(
AzureOpenAiResponseFormat.builder().type(AzureOpenAiResponseFormat.Type.JSON_OBJECT).build())
.build();

requestOptions = client.toAzureChatCompletionsOptions(new Prompt("Test message content", runtimeOptions));
Expand Down
Loading