Open
Description
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