Skip to content

[AI extensions] Add jsonSchemaIsStrict option to OpenAI options mapping #6064

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

Merged
merged 2 commits into from
Mar 16, 2025
Merged
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
Expand Up @@ -377,7 +377,8 @@ private ChatCompletionsOptions ToAzureAIOptions(IEnumerable<ChatMessage> chatCon
["required"] = BinaryData.FromBytes(JsonSerializer.SerializeToUtf8Bytes(tool.Required, JsonContext.Default.ListString)),
["additionalProperties"] = _falseString,
},
json.SchemaDescription);
json.SchemaDescription,
jsonSchemaIsStrict: true);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,11 @@ private static (RunCreationOptions RunOptions, List<FunctionResultContent>? Tool
switch (tool)
{
case AIFunction aiFunction:
bool? strict =
aiFunction.AdditionalProperties.TryGetValue("Strict", out object? strictObj) &&
strictObj is bool strictValue ?
strictValue : null;
// Default strict to true, but allow to be overridden by an additional Strict property.
bool strict =
!aiFunction.AdditionalProperties.TryGetValue("Strict", out object? strictObj) ||
strictObj is not bool strictValue ||
strictValue;

var functionParameters = BinaryData.FromBytes(
JsonSerializer.SerializeToUtf8Bytes(
Expand Down Expand Up @@ -267,7 +268,8 @@ strictObj is bool strictValue ?
AssistantResponseFormat.CreateJsonSchemaFormat(
jsonFormat.SchemaName ?? "json_schema",
BinaryData.FromBytes(JsonSerializer.SerializeToUtf8Bytes(jsonSchema, OpenAIJsonContext.Default.JsonElement)),
jsonFormat.SchemaDescription) :
jsonFormat.SchemaDescription,
strictSchemaEnabled: true) :
AssistantResponseFormat.JsonObject;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ public static ChatCompletionOptions ToOpenAIOptions(ChatOptions? options)
jsonFormat.SchemaName ?? "json_schema",
BinaryData.FromBytes(
JsonSerializer.SerializeToUtf8Bytes(jsonSchema, OpenAIJsonContext.Default.JsonElement)),
jsonFormat.SchemaDescription) :
jsonFormat.SchemaDescription,
jsonSchemaIsStrict: true) :
OpenAI.Chat.ChatResponseFormat.CreateJsonObjectFormat();
}
}
Expand Down Expand Up @@ -443,10 +444,11 @@ private sealed class MetadataOnlyAIFunction(string name, string description, Jso
/// <summary>Converts an Extensions function to an OpenAI chat tool.</summary>
private static ChatTool ToOpenAIChatTool(AIFunction aiFunction)
{
bool? strict =
aiFunction.AdditionalProperties.TryGetValue("Strict", out object? strictObj) &&
strictObj is bool strictValue ?
strictValue : null;
// Default strict to true, but allow to be overridden by an additional Strict property.
bool strict =
!aiFunction.AdditionalProperties.TryGetValue("Strict", out object? strictObj) ||
strictObj is not bool strictValue ||
strictValue;

// Map to an intermediate model so that redundant properties are skipped.
var tool = JsonSerializer.Deserialize(aiFunction.JsonSchema, OpenAIJsonContext.Default.OpenAIChatToolJson)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ private static ResponseCreationOptions ToOpenAIResponseCreationOptions(ChatOptio
ResponseTextFormat.CreateJsonSchemaFormat(
jsonFormat.SchemaName ?? "json_schema",
BinaryData.FromBytes(JsonSerializer.SerializeToUtf8Bytes(jsonSchema, OpenAIJsonContext.Default.JsonElement)),
jsonFormat.SchemaDescription) :
jsonFormat.SchemaDescription,
jsonSchemaIsStrict: true) :
ResponseTextFormat.CreateJsonObjectFormat();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ public async Task ResponseFormat_JsonSchema_NonStreaming()
"required":["description"],
"additionalProperties":false
},
"description":"An object with a description"
"description":"An object with a description",
"strict":true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ public async Task FunctionCallContent_NonStreaming()
"function": {
"description": "Gets the age of the specified person.",
"name": "GetPersonAge",
"strict":true,
"parameters": {
"type": "object",
"required": [
Expand Down Expand Up @@ -811,6 +812,7 @@ public async Task FunctionCallContent_Streaming()
"function": {
"description": "Gets the age of the specified person.",
"name": "GetPersonAge",
"strict":true,
"parameters": {
"type": "object",
"required": [
Expand Down
Loading