Skip to content

Commit d0aa11b

Browse files
eiriktsarpalisjeffhandley
authored andcommitted
Disable default required property schema generation and OpenAI strict mode. (#6285)
* Disable default required property schema generation and OpenAI strict mode. * Default strictness to OpenAI client defaults. * Fix a few failing tests. * Undo a number of additional changes made by #6064 * Revert change to structured output defaults. * Incorporate more test fixes. * Address feedback. * Tweak strict mode signature, fix failing tests. * Roll back schemaIsStrict flag from ChatResponseFormat.
1 parent 24188c7 commit d0aa11b

File tree

8 files changed

+23
-31
lines changed

8 files changed

+23
-31
lines changed

src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonSchemaCreateOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ public sealed record class AIJsonSchemaCreateOptions
5555
/// <summary>
5656
/// Gets a value indicating whether to mark all properties as required in the schema.
5757
/// </summary>
58-
public bool RequireAllProperties { get; init; } = true;
58+
public bool RequireAllProperties { get; init; }
5959
}

src/Libraries/Microsoft.Extensions.AI.AzureAIInference/AzureAIInferenceChatClient.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,7 @@ private ChatCompletionsOptions ToAzureAIOptions(IEnumerable<ChatMessage> chatCon
370370
["required"] = BinaryData.FromBytes(JsonSerializer.SerializeToUtf8Bytes(tool.Required, JsonContext.Default.ListString)),
371371
["additionalProperties"] = _falseString,
372372
},
373-
json.SchemaDescription,
374-
jsonSchemaIsStrict: true);
373+
json.SchemaDescription);
375374
}
376375
else
377376
{

src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIChatClient.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -611,8 +611,7 @@ private static ChatCompletionOptions ToOpenAIOptions(ChatOptions? options)
611611
jsonFormat.SchemaName ?? "json_schema",
612612
BinaryData.FromBytes(
613613
JsonSerializer.SerializeToUtf8Bytes(jsonSchema, ChatClientJsonContext.Default.JsonElement)),
614-
jsonFormat.SchemaDescription,
615-
jsonSchemaIsStrict: true) :
614+
jsonFormat.SchemaDescription) :
616615
OpenAI.Chat.ChatResponseFormat.CreateJsonObjectFormat();
617616
}
618617
}
@@ -623,11 +622,10 @@ private static ChatCompletionOptions ToOpenAIOptions(ChatOptions? options)
623622
/// <summary>Converts an Extensions function to an OpenAI chat tool.</summary>
624623
private static ChatTool ToOpenAIChatTool(AIFunction aiFunction)
625624
{
626-
// Default strict to true, but allow to be overridden by an additional Strict property.
627-
bool strict =
628-
!aiFunction.AdditionalProperties.TryGetValue("Strict", out object? strictObj) ||
629-
strictObj is not bool strictValue ||
630-
strictValue;
625+
bool? strict =
626+
aiFunction.AdditionalProperties.TryGetValue("strictJsonSchema", out object? strictObj) &&
627+
strictObj is bool strictValue ?
628+
strictValue : null;
631629

632630
// Map to an intermediate model so that redundant properties are skipped.
633631
var tool = JsonSerializer.Deserialize(aiFunction.JsonSchema, ChatClientJsonContext.Default.ChatToolJson)!;

src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponseChatClient.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,7 @@ private static ResponseCreationOptions ToOpenAIResponseCreationOptions(ChatOptio
418418
ResponseTextFormat.CreateJsonSchemaFormat(
419419
jsonFormat.SchemaName ?? "json_schema",
420420
BinaryData.FromBytes(JsonSerializer.SerializeToUtf8Bytes(jsonSchema, ResponseClientJsonContext.Default.JsonElement)),
421-
jsonFormat.SchemaDescription,
422-
jsonSchemaIsStrict: true) :
421+
jsonFormat.SchemaDescription) :
423422
ResponseTextFormat.CreateJsonObjectFormat(),
424423
};
425424
}

src/Libraries/Microsoft.Extensions.AI/ChatCompletion/ChatClientStructuredOutputExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public static class ChatClientStructuredOutputExtensions
2424
{
2525
IncludeSchemaKeyword = true,
2626
DisallowAdditionalProperties = true,
27-
IncludeTypeInEnumSchemas = true
27+
IncludeTypeInEnumSchemas = true,
28+
RequireAllProperties = true,
2829
};
2930

3031
/// <summary>Sends chat messages, requesting a response matching the type <typeparamref name="T"/>.</summary>

test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static void AIJsonSchemaCreateOptions_DefaultInstance_ReturnsExpectedValu
7474
Assert.True(options.IncludeTypeInEnumSchemas);
7575
Assert.True(options.DisallowAdditionalProperties);
7676
Assert.False(options.IncludeSchemaKeyword);
77-
Assert.True(options.RequireAllProperties);
77+
Assert.False(options.RequireAllProperties);
7878
Assert.Null(options.TransformSchemaNode);
7979
}
8080
@@ -148,11 +148,11 @@ public static void CreateJsonSchema_DefaultParameters_GeneratesExpectedJsonSchem
148148
"enum": ["A", "B"]
149149
},
150150
"Value": {
151-
"description": "Default value: \"defaultValue\"",
152-
"type": ["string", "null"]
151+
"type": ["string", "null"],
152+
"default": "defaultValue"
153153
}
154154
},
155-
"required": ["Key", "EnumValue", "Value"],
155+
"required": ["Key", "EnumValue"],
156156
"additionalProperties": false
157157
}
158158
""").RootElement;
@@ -168,7 +168,7 @@ public static void CreateJsonSchema_OverriddenParameters_GeneratesExpectedJsonSc
168168
JsonElement expected = JsonDocument.Parse("""
169169
{
170170
"$schema": "https://json-schema.org/draft/2020-12/schema",
171-
"description": "alternative description",
171+
"description": "alternative description (Default value: null)",
172172
"type": "object",
173173
"properties": {
174174
"Key": {
@@ -179,12 +179,11 @@ public static void CreateJsonSchema_OverriddenParameters_GeneratesExpectedJsonSc
179179
"enum": ["A", "B"]
180180
},
181181
"Value": {
182-
"type": ["string", "null"],
183-
"default": "defaultValue"
182+
"description": "Default value: \"defaultValue\"",
183+
"type": ["string", "null"]
184184
}
185185
},
186-
"required": ["Key", "EnumValue"],
187-
"default": null
186+
"required": ["Key", "EnumValue", "Value"]
188187
}
189188
""").RootElement;
190189

@@ -193,7 +192,7 @@ public static void CreateJsonSchema_OverriddenParameters_GeneratesExpectedJsonSc
193192
IncludeTypeInEnumSchemas = false,
194193
DisallowAdditionalProperties = false,
195194
IncludeSchemaKeyword = true,
196-
RequireAllProperties = false,
195+
RequireAllProperties = true,
197196
};
198197

199198
JsonElement actual = AIJsonUtilities.CreateJsonSchema(
@@ -224,11 +223,11 @@ public static void CreateJsonSchema_UserDefinedTransformer()
224223
"enum": ["A", "B"]
225224
},
226225
"Value": {
227-
"description": "Default value: \"defaultValue\"",
228-
"type": ["string", "null"]
226+
"type": ["string", "null"],
227+
"default": "defaultValue"
229228
}
230229
},
231-
"required": ["Key", "EnumValue", "Value"],
230+
"required": ["Key", "EnumValue"],
232231
"additionalProperties": false
233232
}
234233
""").RootElement;
@@ -270,7 +269,6 @@ public static void CreateJsonSchema_FiltersDisallowedKeywords()
270269
"type": "string"
271270
}
272271
},
273-
"required": ["Date","TimeSpan","Char"],
274272
"additionalProperties": false
275273
}
276274
""").RootElement;

test/Libraries/Microsoft.Extensions.AI.AzureAIInference.Tests/AzureAIInferenceChatClientTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ public async Task ResponseFormat_JsonSchema_NonStreaming()
385385
"required":["description"],
386386
"additionalProperties":false
387387
},
388-
"description":"An object with a description",
389-
"strict":true
388+
"description":"An object with a description"
390389
}
391390
}
392391
}

test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIChatClientTests.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,6 @@ public async Task FunctionCallContent_NonStreaming()
662662
"function": {
663663
"description": "Gets the age of the specified person.",
664664
"name": "GetPersonAge",
665-
"strict":true,
666665
"parameters": {
667666
"type": "object",
668667
"required": [
@@ -868,7 +867,6 @@ public async Task FunctionCallContent_Streaming()
868867
"function": {
869868
"description": "Gets the age of the specified person.",
870869
"name": "GetPersonAge",
871-
"strict":true,
872870
"parameters": {
873871
"type": "object",
874872
"required": [

0 commit comments

Comments
 (0)