Skip to content

Commit d39d26d

Browse files
Copilotstephentoub
authored andcommitted
Document JSON schema derivation for return types in AIFunctionFactory (#7400)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent ff52ab9 commit d39d26d

3 files changed

Lines changed: 72 additions & 7 deletions

File tree

src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionDeclaration.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,29 @@ protected AIFunctionDeclaration()
4040
/// The metadata present in the schema document plays an important role in guiding AI function invocation.
4141
/// </para>
4242
/// <para>
43+
/// When an <see cref="AIFunction"/> is created via <see cref="AIFunctionFactory"/>, this schema is automatically derived from the
44+
/// method's parameters using the configured <see cref="JsonSerializerOptions"/> and <see cref="AIJsonSchemaCreateOptions"/>.
45+
/// </para>
46+
/// <para>
4347
/// When no schema is specified, consuming chat clients should assume the "{}" or "true" schema, indicating that any JSON input is admissible.
4448
/// </para>
4549
/// </remarks>
4650
public virtual JsonElement JsonSchema => AIJsonUtilities.DefaultJsonSchema;
4751

4852
/// <summary>Gets a JSON Schema describing the function's return value.</summary>
4953
/// <remarks>
50-
/// A <see langword="null"/> typically reflects a function that doesn't specify a return schema
51-
/// or a function that returns <see cref="void"/>, <see cref="Task"/>, or <see cref="ValueTask"/>.
54+
/// <para>
55+
/// When an <see cref="AIFunction"/> is created via <see cref="AIFunctionFactory"/>, this schema is automatically derived from the
56+
/// method's return type using the configured <see cref="JsonSerializerOptions"/> and <see cref="AIJsonSchemaCreateOptions"/>.
57+
/// For methods returning <see cref="Task{TResult}"/> or <see cref="ValueTask{TResult}"/>, the schema is based on the
58+
/// unwrapped result type. Return schema generation can be excluded by setting
59+
/// <see cref="AIFunctionFactoryOptions.ExcludeResultSchema"/> to <see langword="true"/>.
60+
/// </para>
61+
/// <para>
62+
/// A <see langword="null"/> value typically reflects a function that doesn't specify a return schema,
63+
/// a function that returns <see cref="void"/>, <see cref="Task"/>, or <see cref="ValueTask"/>,
64+
/// or a function for which <see cref="AIFunctionFactoryOptions.ExcludeResultSchema"/> was set to <see langword="true"/>.
65+
/// </para>
5266
/// </remarks>
5367
public virtual JsonElement? ReturnJsonSchema => null;
5468
}

src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactory.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
namespace Microsoft.Extensions.AI;
2828

2929
/// <summary>Provides factory methods for creating commonly-used implementations of <see cref="AIFunction"/>.</summary>
30+
/// <remarks>
31+
/// <para>
32+
/// The <see cref="AIFunctionFactory"/> class creates <see cref="AIFunction"/> instances that wrap .NET methods
33+
/// (specified as <see cref="Delegate"/> or <see cref="MethodInfo"/>). As part of this process, JSON schemas are
34+
/// automatically derived for both the function's input parameters (exposed via <see cref="AIFunctionDeclaration.JsonSchema"/>)
35+
/// and, by default, the function's return type (exposed via <see cref="AIFunctionDeclaration.ReturnJsonSchema"/>).
36+
/// These schemas are produced using the <see cref="AIFunctionFactoryOptions.SerializerOptions"/> and
37+
/// <see cref="AIFunctionFactoryOptions.JsonSchemaCreateOptions"/>, and enable AI services to understand and
38+
/// interact with the function. Return value serialization and schema derivation behavior can be customized
39+
/// via <see cref="AIFunctionFactoryOptions.MarshalResult"/> and <see cref="AIFunctionFactoryOptions.ExcludeResultSchema"/>,
40+
/// respectively.
41+
/// </para>
42+
/// </remarks>
3043
/// <related type="Article" href="https://learn.microsoft.com/dotnet/ai/quickstarts/use-function-calling">Invoke .NET functions using an AI model.</related>
3144
public static partial class AIFunctionFactory
3245
{
@@ -98,7 +111,14 @@ public static partial class AIFunctionFactory
98111
/// special-cased and are not serialized: the created function returns the original instance(s) directly to enable
99112
/// callers (such as an <c>IChatClient</c>) to perform type tests and implement specialized handling. If
100113
/// <see cref="AIFunctionFactoryOptions.MarshalResult"/> is supplied, that delegate governs the behavior instead.
101-
/// Handling of return values can be overridden via <see cref="AIFunctionFactoryOptions.MarshalResult"/>.
114+
/// </para>
115+
/// <para>
116+
/// In addition to the parameter schema, a JSON schema is also derived from the method's return type and exposed via the
117+
/// returned <see cref="AIFunction"/>'s <see cref="AIFunctionDeclaration.ReturnJsonSchema"/>. For methods returning
118+
/// <see cref="void"/>, <see cref="Task"/>, or <see cref="ValueTask"/>, no return schema is produced (the property is <see langword="null"/>).
119+
/// For methods returning <see cref="Task{TResult}"/> or <see cref="ValueTask{TResult}"/>, the schema is derived from the
120+
/// unwrapped result type. Return schema generation can be excluded via <see cref="AIFunctionFactoryOptions.ExcludeResultSchema"/>,
121+
/// and its generation is governed by <paramref name="options"/>'s <see cref="AIFunctionFactoryOptions.JsonSchemaCreateOptions"/>.
102122
/// </para>
103123
/// </remarks>
104124
/// <exception cref="ArgumentNullException"><paramref name="method"/> is <see langword="null"/>.</exception>
@@ -169,6 +189,11 @@ public static AIFunction Create(Delegate method, AIFunctionFactoryOptions? optio
169189
/// derived type of <see cref="AIContent"/>, or any type assignable from <see cref="IEnumerable{AIContent}"/> are not serialized;
170190
/// they are returned as-is to facilitate specialized handling.
171191
/// </para>
192+
/// <para>
193+
/// A JSON schema is also derived from the method's return type and exposed via <see cref="AIFunctionDeclaration.ReturnJsonSchema"/>.
194+
/// For methods returning <see cref="void"/>, <see cref="Task"/>, or <see cref="ValueTask"/>, no return schema is produced.
195+
/// For methods returning <see cref="Task{TResult}"/> or <see cref="ValueTask{TResult}"/>, the schema is derived from the unwrapped result type.
196+
/// </para>
172197
/// </remarks>
173198
/// <exception cref="ArgumentNullException"><paramref name="method"/> is <see langword="null"/>.</exception>
174199
/// <exception cref="JsonException">A parameter to <paramref name="method"/> is not serializable.</exception>
@@ -255,6 +280,14 @@ public static AIFunction Create(Delegate method, string? name = null, string? de
255280
/// any type assignable from <see cref="IEnumerable{AIContent}"/> are not serialized and are instead returned directly.
256281
/// Handling of return values can be overridden via <see cref="AIFunctionFactoryOptions.MarshalResult"/>.
257282
/// </para>
283+
/// <para>
284+
/// In addition to the parameter schema, a JSON schema is also derived from the method's return type and exposed via the
285+
/// returned <see cref="AIFunction"/>'s <see cref="AIFunctionDeclaration.ReturnJsonSchema"/>. For methods returning
286+
/// <see cref="void"/>, <see cref="Task"/>, or <see cref="ValueTask"/>, no return schema is produced (the property is <see langword="null"/>).
287+
/// For methods returning <see cref="Task{TResult}"/> or <see cref="ValueTask{TResult}"/>, the schema is derived from the
288+
/// unwrapped result type. Return schema generation can be excluded via <see cref="AIFunctionFactoryOptions.ExcludeResultSchema"/>,
289+
/// and its generation is governed by <paramref name="options"/>'s <see cref="AIFunctionFactoryOptions.JsonSchemaCreateOptions"/>.
290+
/// </para>
258291
/// </remarks>
259292
/// <exception cref="ArgumentNullException"><paramref name="method"/> is <see langword="null"/>.</exception>
260293
/// <exception cref="ArgumentNullException"><paramref name="method"/> represents an instance method but <paramref name="target"/> is null.</exception>
@@ -334,6 +367,11 @@ public static AIFunction Create(MethodInfo method, object? target, AIFunctionFac
334367
/// derived type of <see cref="AIContent"/>, or any type assignable from <see cref="IEnumerable{AIContent}"/> are returned
335368
/// without serialization to enable specialized handling.
336369
/// </para>
370+
/// <para>
371+
/// A JSON schema is also derived from the method's return type and exposed via <see cref="AIFunctionDeclaration.ReturnJsonSchema"/>.
372+
/// For methods returning <see cref="void"/>, <see cref="Task"/>, or <see cref="ValueTask"/>, no return schema is produced.
373+
/// For methods returning <see cref="Task{TResult}"/> or <see cref="ValueTask{TResult}"/>, the schema is derived from the unwrapped result type.
374+
/// </para>
337375
/// </remarks>
338376
/// <exception cref="ArgumentNullException"><paramref name="method"/> is <see langword="null"/>.</exception>
339377
/// <exception cref="ArgumentNullException"><paramref name="method"/> represents an instance method but <paramref name="target"/> is null.</exception>
@@ -433,6 +471,14 @@ public static AIFunction Create(MethodInfo method, object? target, string? name
433471
/// assignable from <see cref="IEnumerable{AIContent}"/> are returned directly without serialization.
434472
/// Handling of return values can be overridden via <see cref="AIFunctionFactoryOptions.MarshalResult"/>.
435473
/// </para>
474+
/// <para>
475+
/// In addition to the parameter schema, a JSON schema is also derived from the method's return type and exposed via the
476+
/// returned <see cref="AIFunction"/>'s <see cref="AIFunctionDeclaration.ReturnJsonSchema"/>. For methods returning
477+
/// <see cref="void"/>, <see cref="Task"/>, or <see cref="ValueTask"/>, no return schema is produced (the property is <see langword="null"/>).
478+
/// For methods returning <see cref="Task{TResult}"/> or <see cref="ValueTask{TResult}"/>, the schema is derived from the
479+
/// unwrapped result type. Return schema generation can be excluded via <see cref="AIFunctionFactoryOptions.ExcludeResultSchema"/>,
480+
/// and its generation is governed by <paramref name="options"/>'s <see cref="AIFunctionFactoryOptions.JsonSchemaCreateOptions"/>.
481+
/// </para>
436482
/// </remarks>
437483
/// <exception cref="ArgumentNullException"><paramref name="method"/> is <see langword="null"/>.</exception>
438484
/// <exception cref="ArgumentNullException"><paramref name="createInstanceFunc"/> is <see langword="null"/>.</exception>

src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactoryOptions.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ public AIFunctionFactoryOptions()
3030
public JsonSerializerOptions? SerializerOptions { get; set; }
3131

3232
/// <summary>
33-
/// Gets or sets the <see cref="AIJsonSchemaCreateOptions"/> governing the generation of JSON schemas for the function.
33+
/// Gets or sets the <see cref="AIJsonSchemaCreateOptions"/> governing the generation of JSON schemas for
34+
/// the function's input parameters and return type.
3435
/// </summary>
3536
/// <remarks>
3637
/// If no value has been specified, the <see cref="AIJsonSchemaCreateOptions.Default"/> instance will be used.
38+
/// This setting affects both the <see cref="AIFunctionDeclaration.JsonSchema"/> (input parameters) and
39+
/// the <see cref="AIFunctionDeclaration.ReturnJsonSchema"/> (return type).
3740
/// </remarks>
3841
public AIJsonSchemaCreateOptions? JsonSchemaCreateOptions { get; set; }
3942

@@ -107,14 +110,16 @@ public AIFunctionFactoryOptions()
107110
public Func<object?, Type?, CancellationToken, ValueTask<object?>>? MarshalResult { get; set; }
108111

109112
/// <summary>
110-
/// Gets or sets a value indicating whether a schema should be created for the function's result type, if possible, and included as <see cref="AIFunctionDeclaration.ReturnJsonSchema" />.
113+
/// Gets or sets a value indicating whether to exclude generation of a JSON schema for the function's return type.
111114
/// </summary>
112115
/// <remarks>
113116
/// <para>
114-
/// The default value is <see langword="false"/>.
117+
/// The default value is <see langword="false"/>, meaning a return type schema will be generated and exposed
118+
/// via <see cref="AIFunctionDeclaration.ReturnJsonSchema"/> when the method has a return type other than
119+
/// <see cref="void"/>, <see cref="Task"/>, or <see cref="ValueTask"/>.
115120
/// </para>
116121
/// <para>
117-
/// When set to <see langword="true"/>, results in the produced <see cref="AIFunctionDeclaration.ReturnJsonSchema"/> to always be <see langword="null"/>.
122+
/// When set to <see langword="true"/>, the produced <see cref="AIFunctionDeclaration.ReturnJsonSchema"/> will always be <see langword="null"/>.
118123
/// </para>
119124
/// </remarks>
120125
public bool ExcludeResultSchema { get; set; }

0 commit comments

Comments
 (0)