extensions icon indicating copy to clipboard operation
extensions copied to clipboard

JSON schema response format does not set 'strict' parameter for OpenAI models

Open NiceWaffel opened this issue 1 year ago • 1 comments

Description

When using the OpenAIChatClient provided by the Microsoft.Extensions.AI.OpenAI package, the strict parameter of the chat response format is not set.

if (options.ResponseFormat is ChatResponseFormatText)
{
  result.ResponseFormat = OpenAI.Chat.ChatResponseFormat.CreateTextFormat();
}
else if (options.ResponseFormat is ChatResponseFormatJson jsonFormat)
{
  result.ResponseFormat = jsonFormat.Schema is { } jsonSchema
    ? OpenAI.Chat.ChatResponseFormat.CreateJsonSchemaFormat(
      jsonFormat.SchemaName ?? "json_schema",
      BinaryData.FromString(jsonSchema),
      jsonFormat.SchemaDescription)
    : OpenAI.Chat.ChatResponseFormat.CreateJsonObjectFormat();
}

Reproduction Steps

Try with the following setup. Here I am using a model on Azure.

var openAiClient = new AzureOpenAIClient(new Uri(ApiUrl), new ApiKeyCredential(ApiKey));

const string prompt = "Generate JSON according to the schema.";
const string schema = """
                      {
                        "$schema": "http://json-schema.org/draft-04/schema#",
                        "type": "object",
                        "additionalProperties": false,
                        "required": ["A"],
                        "properties": {
                          "A": {
                            "type": "integer",
                            "description": "Set this field to null."
                          }
                        }
                      }
                      """;

var chatClient = new OpenAIChatClient(openAiClient, "gpt-4o-structured-outputs");
var schemaResponseFormat = ChatResponseFormat.ForJsonSchema(JsonSerializer.Deserialize<JsonElement>(schema), "Schema");

var result = await chatClient.CompleteAsync(
  [new ChatMessage(ChatRole.User, prompt)],
  new ChatOptions { ResponseFormat = schemaResponseFormat });

Console.WriteLine(result.Choices[0].Contents[0].ToString());

Expected behavior

I'd expect there to either be a parameter for strict behavior, or strict behavior to be the default.

Actual behavior

The ChatResponseFormatJson does not have a property for strict, and the ForJsonSchema method does not have a parameter for it. Thus it just silently defaults to null.

Regression?

This did not work in any previous release.

Known Workarounds

You could implement your own version of the OpenAIChatClient. In there the strict parameter could be set to a different default value.

Configuration

Using version 9.1.0-preview.1.25064.3 of the package with .NET 9.

Other information

No response

NiceWaffel avatar Jan 20 '25 15:01 NiceWaffel

This is was resolved: https://github.com/dotnet/extensions/blob/c43462a31323873b8e4329f6b8f86cc46da6c8ee/src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs#L610-L616

SteveSandersonMS avatar Apr 01 '25 10:04 SteveSandersonMS