Skip to content

Honor [Required] attribute in AI function parameter JSON schema generation#7272

Open
Copilot wants to merge 8 commits intomainfrom
copilot/fix-required-attribute-json-schema
Open

Honor [Required] attribute in AI function parameter JSON schema generation#7272
Copilot wants to merge 8 commits intomainfrom
copilot/fix-required-attribute-json-schema

Conversation

Copy link
Contributor

Copilot AI commented Feb 7, 2026

  • Modify CreateFunctionJsonSchema in AIJsonUtilities.Schema.Create.cs to check for [Required] attribute on parameters (under #if NET || NETFRAMEWORK)
  • Add comprehensive test covering all combinations: with/without [Required], with/without [DefaultValue], with/without C# optional value, nullable/non-nullable, reference/value types
  • Build and run targeted tests to validate changes
  • Run code review and CodeQL checks (both clean)
  • Address PR feedback: remove baseline/change annotations from test comments
  • Apply [Required] check to all attributed members on types used in the parameters (nested type properties), within the ApplyDataAnnotations method alongside other data annotations
  • Add tests for nested type [Required] attribute support (both CreateFunctionJsonSchema and CreateJsonSchema)
  • Build and validate (18/18 tests pass)
  • Restore // [AttributeName] comments in ApplyDataAnnotations
Original prompt

This section details on the original issue you should resolve

<issue_title>[Required] attribute is ignored when generating JSON schema for AI function parameters</issue_title>
<issue_description>### Description
Currently, parameters annotated with [Required] are treated the same as optional parameters if they had a default value or were otherwise non-optional, which can be surprising and is undocumented. This came up quite recently while investigating what looked to be an [inconsistency with tool parameter schema generation]. There, it wasn't clear why [Required] had no effect on the generated schema.

Motivation

It is extremely natural to expect that the [Required] attribute would mark a tool parameter as required. We already respect parameter annotations such as [Description], so it’s reasonable to expect [Required] to be honored as well. This inconsistency can lead to confusion, especially considering how other annotations are handled. Furthermore, SemanticKernel, which uses its own pipeline, honors the [Required] attribute when generating function schemas, meaning that this change could make developer expectations more consistent throughout libraries. This is especially true for SK users migrating to Agent Framework, which uses the same underlying schema generation logic from Microsoft.Extensions.AI.Abstractions.

Current Behavior

string MyTool([Required] int count = 1) { ... }

// produces
{
  "properties": {
    "count": { "type": "integer", "default": 1 }
  }
}

Expected Behavior

string MyTool([Required] int count = 1) { ... }

// SHOULD produce
{
  "required": ["count"],
  "properties": {
    "count": { "type": "integer", "default": 1 }
  }
}

Root Cause

The code determining whether a parameter is required can be seen below: (link):

// ...
if (!parameter.IsOptional && !hasDefaultValue)
{
    (requiredProperties ??= []).Add((JsonNode)parameter.Name);
}

A potential change would be to detect the presence of [Required], treating the parameter as required, regardless of whether its using optional syntax or has a default value. Of course, we would keep the existing logic if [Required] is not present, keeping existing client code unaffected. This change would only affect schema generation, keeping other steps untouched (invocation, reflection, or runtime execution behavior).
</issue_description>

<agent_instructions>Factor the RequiredAttribute (on TFMs where it's available) into the method schema generation in AIJsonUtilities. Ensure the existing tests are augmented to validate all combinations of with or without the required attribute, with or without a Default Value attribute, with or without a C# optional value, nullable or not, reference and value type; that testing should be done with as few method signatures in as few tests as possible. </agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 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

Copilot AI changed the title [WIP] Fix handling of Required attribute in JSON schema generation Honor [Required] attribute in AI function parameter JSON schema generation Feb 7, 2026
Copilot AI requested a review from stephentoub February 7, 2026 13:08
@stephentoub stephentoub marked this pull request as ready for review February 7, 2026 13:32
@stephentoub stephentoub requested a review from a team as a code owner February 7, 2026 13:32
@stephentoub stephentoub enabled auto-merge (squash) February 7, 2026 13:33
auto-merge was automatically disabled February 7, 2026 13:33

Head branch was pushed to by a user without write access

Copilot AI requested a review from stephentoub February 7, 2026 13:33
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.CreateFunctionJsonSchema required-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.

@stephentoub stephentoub enabled auto-merge (squash) February 7, 2026 15:05
@stephentoub stephentoub added the area-ai Microsoft.Extensions.AI libraries label Feb 8, 2026
Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephentoub do we want to apply this recursively to nested properties with attribute too?

Copy link
Contributor Author

Copilot AI commented Feb 9, 2026

@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: ad0427b2-1327-44cf-a042-c08dc5ad7b01

Sorry for the inconvenience!

@eiriktsarpalis
Copy link
Member

@copilot please try again

@stephentoub
Copy link
Member

@stephentoub do we want to apply this recursively to nested properties with attribute too?

Wouldn't doing that correctly necessitate changes in STJ itself?

auto-merge was automatically disabled February 9, 2026 22:00

Head branch was pushed to by a user without write access

Copy link
Contributor Author

Copilot AI commented Feb 9, 2026

@copilot update the transformer callback to apply the check for required to all attributed members on types used in the parameters

Done in c6b210d. Added ApplyRequiredMembers to the transformer callback, which checks properties on object types for [Required] and adds them to the schema's required array. This applies recursively to all types used in parameters. Also added tests for both CreateFunctionJsonSchema (nested type parameter) and CreateJsonSchema (direct type).

Copilot AI and others added 6 commits February 12, 2026 10:15
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>
@stephentoub stephentoub force-pushed the copilot/fix-required-attribute-json-schema branch from d7790a5 to 4ec5fd2 Compare February 12, 2026 15:39
@stephentoub
Copy link
Member

@eiriktsarpalis please take another look

@stephentoub stephentoub enabled auto-merge (squash) February 12, 2026 15:39
Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
auto-merge was automatically disabled February 12, 2026 16:13

Head branch was pushed to by a user without write access

Copilot AI requested a review from eiriktsarpalis February 12, 2026 16:14
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
@stephentoub stephentoub enabled auto-merge (squash) February 12, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Required] attribute is ignored when generating JSON schema for AI function parameters

3 participants