Honor [Required] attribute in AI function parameter JSON schema generation#7272
Honor [Required] attribute in AI function parameter JSON schema generation#7272
Conversation
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs
Show resolved
Hide resolved
Head branch was pushed to by a user without write access
There was a problem hiding this comment.
Pull request overview
Updates AI function parameter JSON schema generation in Microsoft.Extensions.AI.Abstractions to honor System.ComponentModel.DataAnnotations.RequiredAttribute when determining the schema’s "required" parameters, even when parameters have defaults / are otherwise optional.
Changes:
- Update
AIJsonUtilities.CreateFunctionJsonSchemarequired-parameter inference to treat[Required]as required (under#if NET || NETFRAMEWORK). - Add a regression test covering combinations of
[Required],[DefaultValue], C# optional defaults, nullability, and value/reference types.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs | Extends required-property detection to include [Required] (when available) while keeping existing baseline behavior. |
| test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs | Adds coverage ensuring [Required] forces inclusion in the schema’s "required" list across a matrix of parameter shapes. |
eiriktsarpalis
left a comment
There was a problem hiding this comment.
@stephentoub do we want to apply this recursively to nested properties with attribute too?
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs
Show resolved
Hide resolved
|
@eiriktsarpalis Unfortunately I hit an unexpected error while processing your comment. I've automatically reported this to GitHub. You can ask me to try again later by mentioning me in a new comment. If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: Sorry for the inconvenience! |
|
@copilot please try again |
Wouldn't doing that correctly necessitate changes in STJ itself? |
Head branch was pushed to by a user without write access
Done in c6b210d. Added |
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs
Outdated
Show resolved
Hide resolved
When a parameter is annotated with [Required] (System.ComponentModel.DataAnnotations), it is now included in the JSON schema's "required" array even if it has a default value or is otherwise optional. This applies on TFMs where RequiredAttribute is available (NET and NETFRAMEWORK). Existing behavior for parameters without [Required] is unchanged. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
…rmer callback Extends schema generation so that properties of types used in function parameters are also checked for [Required] attribute. When a property on a nested type has [Required], it's added to that type's schema "required" array. This works for both CreateFunctionJsonSchema and CreateJsonSchema. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Instead of a separate ApplyRequiredMembers method, the [Required] property check is now called from within ApplyDataAnnotations as ApplyRequiredAttributes, alongside the other data annotation checks. The implementation is still in a class-level private static method (forwarded via static local) to keep the CreateJsonSchemaCore maintainability index within bounds. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
d7790a5 to
4ec5fd2
Compare
|
@eiriktsarpalis please take another look |
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs
Show resolved
Hide resolved
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Head branch was pushed to by a user without write access
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
CreateFunctionJsonSchemainAIJsonUtilities.Schema.Create.csto check for[Required]attribute on parameters (under#if NET || NETFRAMEWORK)[Required], with/without[DefaultValue], with/without C# optional value, nullable/non-nullable, reference/value types[Required]check to all attributed members on types used in the parameters (nested type properties), within theApplyDataAnnotationsmethod alongside other data annotations[Required]attribute support (bothCreateFunctionJsonSchemaandCreateJsonSchema)// [AttributeName]comments in ApplyDataAnnotationsOriginal prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
Microsoft Reviewers: Open in CodeFlow