Support DefaultValueAttribute in AIFunctionFactory parameter handling#6947
Merged
stephentoub merged 7 commits intomainfrom Oct 22, 2025
Merged
Support DefaultValueAttribute in AIFunctionFactory parameter handling#6947stephentoub merged 7 commits intomainfrom
stephentoub merged 7 commits intomainfrom
Conversation
- Check for DefaultValueAttribute when determining parameter optionality - Use DefaultValueAttribute value as default when parameter not provided - Add helper methods HasEffectiveDefaultValue and GetEffectiveDefaultValue - Add comprehensive tests for DefaultValue attribute support Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Verify that when both DefaultValue attribute and C# default are present, the DefaultValue attribute takes precedence. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
- Add test verifying DefaultValue attribute takes precedence over C# default - Add .nuget directory to gitignore to exclude build artifacts Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix handling of DefaultValue attribute in optional parameters
Support DefaultValueAttribute in AIFunctionFactory parameter handling
Oct 22, 2025
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for the [DefaultValue] attribute in AIFunctionFactory, ensuring parameters decorated with this attribute are treated as optional in the same way as parameters with C# default value syntax.
Key Changes:
- Added helper methods to detect and retrieve default values from both
[DefaultValue]attribute and C# default syntax - Updated schema generation to correctly mark parameters with
[DefaultValue]as optional - Updated parameter marshaling to use default values from
[DefaultValue]attribute
Reviewed Changes
Copilot reviewed 3 out of 5 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| AIJsonUtilities.Schema.Create.cs | Added HasEffectiveDefaultValue() and GetEffectiveDefaultValue() helpers to check for both C# defaults and [DefaultValue] attribute; updated schema generation logic to use these helpers |
| AIFunctionFactory.cs | Added duplicate helper methods and updated parameter marshaling to respect [DefaultValue] attribute |
| AIFunctionFactoryTest.cs | Added comprehensive test coverage for [DefaultValue] attribute functionality including null values, non-null values, and precedence scenarios |
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactory.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs
Outdated
Show resolved
Hide resolved
stephentoub
reviewed
Oct 22, 2025
Member
stephentoub
left a comment
There was a problem hiding this comment.
@copilot, please fix the formatting diagnostics:
Build FAILED.
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(69,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(75,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(78,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(81,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(87,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(90,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(96,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(99,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(105,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(69,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(75,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(78,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(81,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(87,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(90,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(96,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(99,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs(105,1): error IDE0055: Fix formatting (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0055) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Tests/Microsoft.Extensions.AI.Tests.csproj::TargetFramework=net8.0]
0 Warning(s)
18 Error(s)
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs
Show resolved
Hide resolved
- Revert changes to .gitignore - Fix formatting issues in test file (remove extra blank lines) - Make HasEffectiveDefaultValue and GetEffectiveDefaultValue internal in AIJsonUtilities - Remove duplicate helper methods from AIFunctionFactory and use the ones from AIJsonUtilities Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Contributor
Author
stephentoub
approved these changes
Oct 22, 2025
test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs
Outdated
Show resolved
Hide resolved
Use a longer, more descriptive parameter name to avoid false positives when searching for the parameter name in string assertions. Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Replace HasEffectiveDefaultValue and GetEffectiveDefaultValue with a single TryGetEffectiveDefaultValue method to avoid resolving the DefaultValueAttribute multiple times, improving performance. Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
This was referenced Nov 19, 2025
Closed
Closed
Member
|
Looks like this PR added a nuget.exe to the repo :( |
Member
Ugh |
Member
|
I noticed copilot did this in a couple other PRs and I think we gave feedback around this. |
Member
|
I don't know why, but I'm not able to react to posts in this thread. So adding it here: 👀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[DefaultValue(null)]are treated as required instead of optionalParameterInfo.IsOptionalandParameterInfo.HasDefaultValueare false for[DefaultValue]attributesSummary
Successfully implemented support for the
[DefaultValue]attribute in AIFunctionFactory. Parameters decorated with[DefaultValue]are now treated as optional in the JSON schema and function invocation, matching the behavior of C# default values.Changes Made:
TryGetEffectiveDefaultValuehelper method to check for and retrieve default values from both C# defaults and DefaultValueAttribute in a single callSecurity Considerations:
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
Microsoft Reviewers: Open in CodeFlow