Skip to content

Commit 9979b9f

Browse files
Revert change to structured output defaults.
1 parent 96c60f1 commit 9979b9f

File tree

8 files changed

+30
-8
lines changed

8 files changed

+30
-8
lines changed

src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatResponseFormat.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ private protected ChatResponseFormat()
3030
/// <param name="schema">The JSON schema.</param>
3131
/// <param name="schemaName">An optional name of the schema. For example, if the schema represents a particular class, this could be the name of the class.</param>
3232
/// <param name="schemaDescription">An optional description of the schema.</param>
33+
/// <param name="schemaIsStrict">Whether the response should strictly adhere to the schema.</param>
3334
/// <returns>The <see cref="ChatResponseFormatJson"/> instance.</returns>
3435
public static ChatResponseFormatJson ForJsonSchema(
35-
JsonElement schema, string? schemaName = null, string? schemaDescription = null) =>
36+
JsonElement schema, string? schemaName = null, string? schemaDescription = null, bool schemaIsStrict = false) =>
3637
new(schema,
3738
schemaName,
38-
schemaDescription);
39+
schemaDescription,
40+
schemaIsStrict);
3941
}

src/Libraries/Microsoft.Extensions.AI.Abstractions/ChatCompletion/ChatResponseFormatJson.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ public sealed class ChatResponseFormatJson : ChatResponseFormat
1616
/// <param name="schema">The schema to associate with the JSON response.</param>
1717
/// <param name="schemaName">A name for the schema.</param>
1818
/// <param name="schemaDescription">A description of the schema.</param>
19+
/// <param name="schemaIsStrict">Whether the response should strictly adhere to the schema.</param>
1920
[JsonConstructor]
2021
public ChatResponseFormatJson(
21-
JsonElement? schema, string? schemaName = null, string? schemaDescription = null)
22+
JsonElement? schema, string? schemaName = null, string? schemaDescription = null, bool schemaIsStrict = false)
2223
{
2324
if (schema is null && (schemaName is not null || schemaDescription is not null))
2425
{
@@ -30,6 +31,7 @@ public ChatResponseFormatJson(
3031
Schema = schema;
3132
SchemaName = schemaName;
3233
SchemaDescription = schemaDescription;
34+
SchemaIsStrict = schemaIsStrict;
3335
}
3436

3537
/// <summary>Gets the JSON schema associated with the response, or <see langword="null"/> if there is none.</summary>
@@ -41,6 +43,9 @@ public ChatResponseFormatJson(
4143
/// <summary>Gets a description of the schema.</summary>
4244
public string? SchemaDescription { get; }
4345

46+
/// <summary>Gets a value indicating whether the response should strictly adhere to the schema.</summary>
47+
public bool SchemaIsStrict { get; }
48+
4449
/// <summary>Gets a string representing this instance to display in the debugger.</summary>
4550
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
4651
private string DebuggerDisplay => Schema?.ToString() ?? "JSON";

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

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

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,8 @@ private static ChatCompletionOptions ToOpenAIOptions(ChatOptions? options)
611611
jsonFormat.SchemaName ?? "json_schema",
612612
BinaryData.FromBytes(
613613
JsonSerializer.SerializeToUtf8Bytes(jsonSchema, ChatClientJsonContext.Default.JsonElement)),
614-
jsonFormat.SchemaDescription) :
614+
jsonFormat.SchemaDescription,
615+
jsonFormat.SchemaIsStrict) :
615616
OpenAI.Chat.ChatResponseFormat.CreateJsonObjectFormat();
616617
}
617618
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,8 @@ 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) :
421+
jsonFormat.SchemaDescription,
422+
jsonFormat.SchemaIsStrict) :
422423
ResponseTextFormat.CreateJsonObjectFormat(),
423424
};
424425
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static class ChatClientStructuredOutputExtensions
2525
IncludeSchemaKeyword = true,
2626
DisallowAdditionalProperties = true,
2727
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/ChatCompletion/ChatResponseFormatTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,6 @@ public void Serialization_ForJsonSchemaRoundtrips()
8080
Assert.Equal("[1,2,3]", JsonSerializer.Serialize(actual.Schema, TestJsonSerializerContext.Default.JsonElement));
8181
Assert.Equal("name", actual.SchemaName);
8282
Assert.Equal("description", actual.SchemaDescription);
83+
Assert.False(actual.SchemaIsStrict);
8384
}
8485
}

test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/ChatClientStructuredOutputExtensionsTests.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ public async Task SuccessUsage_Default()
5858
]
5959
}
6060
},
61-
"additionalProperties": false
61+
"additionalProperties": false,
62+
"required": [
63+
"id",
64+
"fullName",
65+
"species"
66+
]
6267
}
6368
""", responseFormat.Schema.ToString());
6469
Assert.Equal(nameof(Animal), responseFormat.SchemaName);
@@ -346,7 +351,12 @@ public async Task CanSpecifyCustomJsonSerializationOptions()
346351
"type": "integer"
347352
}
348353
},
349-
"additionalProperties": false
354+
"additionalProperties": false,
355+
"required": [
356+
"id",
357+
"full_name",
358+
"species"
359+
]
350360
}
351361
""", responseFormat.Schema.ToString());
352362

0 commit comments

Comments
 (0)