Skip to content

Commit e5ce0d7

Browse files
stephentoubeiriktsarpalis
authored andcommitted
Rename useJsonSchema parameter (#6394)
* Rename useJsonSchema parameter The `GetResponseAsync<T>` methods accept a `bool?` parameter currently called `useJsonSchema`. This is confusing, because the whole point of the method is to create and use a JSON schema from the `T`. The parameter actually controls _how_ that schema is used, whether it's included as part of the messages (false), as part of a ChatResponseFormat in the ChatOptions (true), or up to the system to decide. I've clarified it by renaming it from `useJsonSchema` to `useJsonSchemaResponseFormat`. It's wordier, but it disambiguates the intent. * Update src/Libraries/Microsoft.Extensions.AI/ChatCompletion/ChatClientStructuredOutputExtensions.cs Co-authored-by: Eirik Tsarpalis <[email protected]> --------- Co-authored-by: Eirik Tsarpalis <[email protected]>
1 parent 5f8fc54 commit e5ce0d7

File tree

3 files changed

+21
-23
lines changed

3 files changed

+21
-23
lines changed

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

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static class ChatClientStructuredOutputExtensions
3333
/// <param name="chatClient">The <see cref="IChatClient"/>.</param>
3434
/// <param name="messages">The chat content to send.</param>
3535
/// <param name="options">The chat options to configure the request.</param>
36-
/// <param name="useJsonSchema">
36+
/// <param name="useJsonSchemaResponseFormat">
3737
/// <see langword="true" /> to set a JSON schema on the <see cref="ChatResponseFormat"/>; otherwise, <see langword="false" />. The default is <see langword="true" />.
3838
/// Using a JSON schema improves reliability if the underlying model supports native structured output with a schema, but might cause an error if the model does not support it.
3939
/// </param>
@@ -44,19 +44,17 @@ public static Task<ChatResponse<T>> GetResponseAsync<T>(
4444
this IChatClient chatClient,
4545
IEnumerable<ChatMessage> messages,
4646
ChatOptions? options = null,
47-
bool? useJsonSchema = null,
47+
bool? useJsonSchemaResponseFormat = null,
4848
CancellationToken cancellationToken = default) =>
49-
GetResponseAsync<T>(chatClient, messages, AIJsonUtilities.DefaultOptions, options, useJsonSchema, cancellationToken);
49+
GetResponseAsync<T>(chatClient, messages, AIJsonUtilities.DefaultOptions, options, useJsonSchemaResponseFormat, cancellationToken);
5050

5151
/// <summary>Sends a user chat text message, requesting a response matching the type <typeparamref name="T"/>.</summary>
5252
/// <param name="chatClient">The <see cref="IChatClient"/>.</param>
5353
/// <param name="chatMessage">The text content for the chat message to send.</param>
5454
/// <param name="options">The chat options to configure the request.</param>
55-
/// <param name="useJsonSchema">
55+
/// <param name="useJsonSchemaResponseFormat">
5656
/// <see langword="true" /> to set a JSON schema on the <see cref="ChatResponseFormat"/>; otherwise, <see langword="false" />.
5757
/// Using a JSON schema improves reliability if the underlying model supports native structured output with a schema, but might cause an error if the model does not support it.
58-
/// If not specified, the default value is determined by the implementation.
59-
/// If a specific value is required, it must be specified by the caller.
6058
/// </param>
6159
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
6260
/// <returns>The response messages generated by the client.</returns>
@@ -66,15 +64,15 @@ public static Task<ChatResponse<T>> GetResponseAsync<T>(
6664
this IChatClient chatClient,
6765
string chatMessage,
6866
ChatOptions? options = null,
69-
bool? useJsonSchema = null,
67+
bool? useJsonSchemaResponseFormat = null,
7068
CancellationToken cancellationToken = default) =>
71-
GetResponseAsync<T>(chatClient, new ChatMessage(ChatRole.User, chatMessage), options, useJsonSchema, cancellationToken);
69+
GetResponseAsync<T>(chatClient, new ChatMessage(ChatRole.User, chatMessage), options, useJsonSchemaResponseFormat, cancellationToken);
7270

7371
/// <summary>Sends a chat message, requesting a response matching the type <typeparamref name="T"/>.</summary>
7472
/// <param name="chatClient">The <see cref="IChatClient"/>.</param>
7573
/// <param name="chatMessage">The chat message to send.</param>
7674
/// <param name="options">The chat options to configure the request.</param>
77-
/// <param name="useJsonSchema">
75+
/// <param name="useJsonSchemaResponseFormat">
7876
/// <see langword="true" /> to set a JSON schema on the <see cref="ChatResponseFormat"/>; otherwise, <see langword="false" />. The default is <see langword="true" />.
7977
/// Using a JSON schema improves reliability if the underlying model supports native structured output with a schema, but might cause an error if the model does not support it.
8078
/// </param>
@@ -85,16 +83,16 @@ public static Task<ChatResponse<T>> GetResponseAsync<T>(
8583
this IChatClient chatClient,
8684
ChatMessage chatMessage,
8785
ChatOptions? options = null,
88-
bool? useJsonSchema = null,
86+
bool? useJsonSchemaResponseFormat = null,
8987
CancellationToken cancellationToken = default) =>
90-
GetResponseAsync<T>(chatClient, [chatMessage], options, useJsonSchema, cancellationToken);
88+
GetResponseAsync<T>(chatClient, [chatMessage], options, useJsonSchemaResponseFormat, cancellationToken);
9189

9290
/// <summary>Sends a user chat text message, requesting a response matching the type <typeparamref name="T"/>.</summary>
9391
/// <param name="chatClient">The <see cref="IChatClient"/>.</param>
9492
/// <param name="chatMessage">The text content for the chat message to send.</param>
9593
/// <param name="serializerOptions">The JSON serialization options to use.</param>
9694
/// <param name="options">The chat options to configure the request.</param>
97-
/// <param name="useJsonSchema">
95+
/// <param name="useJsonSchemaResponseFormat">
9896
/// <see langword="true" /> to set a JSON schema on the <see cref="ChatResponseFormat"/>; otherwise, <see langword="false" />. The default is <see langword="true" />.
9997
/// Using a JSON schema improves reliability if the underlying model supports native structured output with a schema, but might cause an error if the model does not support it.
10098
/// </param>
@@ -106,16 +104,16 @@ public static Task<ChatResponse<T>> GetResponseAsync<T>(
106104
string chatMessage,
107105
JsonSerializerOptions serializerOptions,
108106
ChatOptions? options = null,
109-
bool? useJsonSchema = null,
107+
bool? useJsonSchemaResponseFormat = null,
110108
CancellationToken cancellationToken = default) =>
111-
GetResponseAsync<T>(chatClient, new ChatMessage(ChatRole.User, chatMessage), serializerOptions, options, useJsonSchema, cancellationToken);
109+
GetResponseAsync<T>(chatClient, new ChatMessage(ChatRole.User, chatMessage), serializerOptions, options, useJsonSchemaResponseFormat, cancellationToken);
112110

113111
/// <summary>Sends a chat message, requesting a response matching the type <typeparamref name="T"/>.</summary>
114112
/// <param name="chatClient">The <see cref="IChatClient"/>.</param>
115113
/// <param name="chatMessage">The chat message to send.</param>
116114
/// <param name="serializerOptions">The JSON serialization options to use.</param>
117115
/// <param name="options">The chat options to configure the request.</param>
118-
/// <param name="useJsonSchema">
116+
/// <param name="useJsonSchemaResponseFormat">
119117
/// <see langword="true" /> to set a JSON schema on the <see cref="ChatResponseFormat"/>; otherwise, <see langword="false" />. The default is <see langword="true" />.
120118
/// Using a JSON schema improves reliability if the underlying model supports native structured output with a schema, but might cause an error if the model does not support it.
121119
/// </param>
@@ -127,16 +125,16 @@ public static Task<ChatResponse<T>> GetResponseAsync<T>(
127125
ChatMessage chatMessage,
128126
JsonSerializerOptions serializerOptions,
129127
ChatOptions? options = null,
130-
bool? useJsonSchema = null,
128+
bool? useJsonSchemaResponseFormat = null,
131129
CancellationToken cancellationToken = default) =>
132-
GetResponseAsync<T>(chatClient, [chatMessage], serializerOptions, options, useJsonSchema, cancellationToken);
130+
GetResponseAsync<T>(chatClient, [chatMessage], serializerOptions, options, useJsonSchemaResponseFormat, cancellationToken);
133131

134132
/// <summary>Sends chat messages, requesting a response matching the type <typeparamref name="T"/>.</summary>
135133
/// <param name="chatClient">The <see cref="IChatClient"/>.</param>
136134
/// <param name="messages">The chat content to send.</param>
137135
/// <param name="serializerOptions">The JSON serialization options to use.</param>
138136
/// <param name="options">The chat options to configure the request.</param>
139-
/// <param name="useJsonSchema">
137+
/// <param name="useJsonSchemaResponseFormat">
140138
/// <see langword="true" /> to set a JSON schema on the <see cref="ChatResponseFormat"/>; otherwise, <see langword="false" />. The default is <see langword="true" />.
141139
/// Using a JSON schema improves reliability if the underlying model supports native structured output with a schema, but might cause an error if the model does not support it.
142140
/// </param>
@@ -149,7 +147,7 @@ public static async Task<ChatResponse<T>> GetResponseAsync<T>(
149147
IEnumerable<ChatMessage> messages,
150148
JsonSerializerOptions serializerOptions,
151149
ChatOptions? options = null,
152-
bool? useJsonSchema = null,
150+
bool? useJsonSchemaResponseFormat = null,
153151
CancellationToken cancellationToken = default)
154152
{
155153
_ = Throw.IfNull(chatClient);
@@ -192,8 +190,8 @@ public static async Task<ChatResponse<T>> GetResponseAsync<T>(
192190

193191
// We default to assuming that models support JSON schema because developers will normally use
194192
// GetResponseAsync<T> only with models that do. If the model doesn't support JSON schema, it may
195-
// throw or it may ignore the schema. In these cases developers should pass useJsonSchema: false.
196-
if (useJsonSchema.GetValueOrDefault(true))
193+
// throw or it may ignore the schema. In these cases developers should pass useJsonSchemaResponseFormat: false.
194+
if (useJsonSchemaResponseFormat ?? true)
197195
{
198196
// When using native structured output, we don't add any additional prompt, because
199197
// the LLM backend is meant to do whatever's needed to explain the schema to the LLM.

test/Libraries/Microsoft.Extensions.AI.Integration.Tests/ChatClientIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ public virtual async Task GetResponseAsync_StructuredOutput_NonNative()
942942

943943
var response = await captureOutputChatClient.GetResponseAsync<Person>("""
944944
Supply an object to represent Jimbo Smith from Cardiff.
945-
""", useJsonSchema: false);
945+
""", useJsonSchemaResponseFormat: false);
946946

947947
Assert.Equal("Jimbo Smith", response.Result.FullName);
948948
Assert.Contains("Cardiff", response.Result.HomeTown);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public async Task SuccessUsage_NoJsonSchema()
139139
};
140140

141141
var chatHistory = new List<ChatMessage> { new(ChatRole.User, "Hello") };
142-
var response = await client.GetResponseAsync<Animal>(chatHistory, useJsonSchema: false, serializerOptions: JsonContext2.Default.Options);
142+
var response = await client.GetResponseAsync<Animal>(chatHistory, useJsonSchemaResponseFormat: false, serializerOptions: JsonContext2.Default.Options);
143143

144144
// The response contains the deserialized result and other response properties
145145
Assert.Equal(1, response.Result.Id);

0 commit comments

Comments
 (0)