Skip to content

Add AIFunctionNameAttribute and AIParameterNameAttribute#7610

Merged
jozkee merged 9 commits into
mainfrom
copilot/ship-two-attributes
Jul 9, 2026
Merged

Add AIFunctionNameAttribute and AIParameterNameAttribute#7610
jozkee merged 9 commits into
mainfrom
copilot/ship-two-attributes

Conversation

Copilot AI commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Fixes #7477

This change addresses the naming override gap from #7477 while aligning with updated direction: ship two targeted attributes instead of a single general-purpose one. It enables explicit AI-facing names for methods and parameters without introducing class-level naming semantics.

  • API surface

    • Adds AIFunctionNameAttribute (AttributeTargets.Method) for tool/function identifier overrides.
    • Adds AIParameterNameAttribute (AttributeTargets.Parameter) for schema/binding key overrides.
    • Marks both as experimental (MEAI001) and updates public API baselines.
  • Function metadata resolution

    • Updates AIFunctionFactory name precedence to:
      1. explicit options name,
      2. [AIFunctionName],
      3. [DisplayName],
      4. derived method name.
    • Keeps existing DisplayName behavior as fallback.
  • Parameter schema + invocation binding

    • Uses [AIParameterName] for generated function JSON schema property names and required lists.
    • Uses the same effective parameter name for runtime argument binding and missing-argument errors.
    • Adds collision detection when multiple parameters resolve to the same effective name.
  • Schema correctness for special names

    • Escapes overridden parameter names when used in $ref JSON Pointer segments (RFC 6901), so names containing / or ~ remain valid.
  • Coverage updates

    • Adds focused tests for attribute precedence, inherited behavior, runtime binding, duplicate-name rejection, and pointer escaping.
using Microsoft.Extensions.AI;

[AIFunctionName("query_entities")]
static string Query(
    [AIParameterName("$select")] string select,
    [AIParameterName("$expand")] string expand) => "...";

AIFunction f = AIFunctionFactory.Create((Func<string, string, string>)Query);

// f.Name == "query_entities"
// f.JsonSchema.properties contains "$select" and "$expand"
// invocation expects arguments keyed by "$select" / "$expand"
Microsoft Reviewers: Open in CodeFlow

Copilot AI changed the title Add targeted AI function and parameter naming attributes Adopt targeted AI naming attributes for function and parameter identifiers Jul 7, 2026
Copilot AI requested a review from jozkee July 7, 2026 17:48
Copilot finished work on behalf of jozkee July 7, 2026 17:48
@jozkee jozkee requested a review from Copilot July 7, 2026 19:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR adds two targeted experimental attributes to the Microsoft.Extensions.AI abstractions to let developers override AI-facing function and parameter identifiers, and updates both JSON schema generation and runtime argument binding to honor those overrides (including correct JSON Pointer escaping).

Changes:

  • Introduces [AIFunctionName] (method-level) and [AIParameterName] (parameter-level) experimental attributes (MEAI001) and updates API baselines.
  • Updates AIFunctionFactory name precedence and switches argument binding to use the effective (possibly overridden) parameter name.
  • Updates AIJsonUtilities.CreateFunctionJsonSchema to emit overridden parameter property names, detect duplicates, and escape JSON Pointer segments; adds focused tests.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs Adds tests for function-name precedence and runtime binding via [AIParameterName].
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs Adds schema tests for [AIParameterName], duplicate detection, and JSON Pointer escaping.
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Functions/AINameAttributesTests.cs Adds constructor argument validation tests for the new attributes.
src/Shared/DiagnosticIds/DiagnosticIds.cs Adds experiment IDs for the new attribute APIs.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Schema.Create.cs Implements effective parameter naming, duplicate checks in schema, and JSON Pointer escaping.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Microsoft.Extensions.AI.Abstractions.json Updates public API baseline entries for the new attribute types.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIParameterNameAttribute.cs Adds the new parameter naming attribute (experimental).
src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionNameAttribute.cs Adds the new function naming attribute (experimental).
src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactoryOptions.cs Updates docs to mention [AIFunctionName] as a naming source.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactory.cs Applies new name precedence and binds arguments using effective parameter names.

Comment thread src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactory.cs Outdated

@jozkee jozkee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot address feedback

Comment thread src/Shared/DiagnosticIds/DiagnosticIds.cs Outdated

Copilot AI commented Jul 8, 2026

Copy link
Copy Markdown
Contributor Author

@copilot address feedback

All feedback addressed in commit 69d2fef:

  • ExpectedArgumentNames now throws ArgumentException when Add() returns false (mirrors the schema generation collision check)
  • GetReturnParameterDescription catches IndexOutOfRangeException for .NET Framework compatibility
  • DiagnosticIds: merged AIFunctionName + AIParameterName into AIFunctionAndParameterName
  • Tests updated per inline suggestions and new tests added to AIFunctionFactoryTest.cs and AIJsonUtilitiesTests.cs

Adds test to verify that parameter name collision detection occurs
even when one of the colliding parameters is excluded from schema
generation via ExcludeFromSchema = true. This addresses reviewer
feedback about potential silent de-duplication in this scenario.
Includes AIFunctionFactory_InheritedDescriptionAttributes_OnOverride test
that validates inherited Description attributes on overridden methods,
including parameter descriptions and return value descriptions.
Covers both modern .NET and .NET Framework behavior differences.
Copilot finished work on behalf of jozkee July 8, 2026 20:02
@dotnet-comment-bot

Copy link
Copy Markdown
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.Diagnostics.Testing Line 99 98.65 🔻
Microsoft.Extensions.Telemetry Line 93 92.64 🔻
Microsoft.Extensions.AI Line 89 88.5 🔻
Microsoft.Extensions.AI Branch 89 88.57 🔻
Microsoft.Extensions.AI.OpenAI Line 75 62.75 🔻
Microsoft.Extensions.AI.OpenAI Branch 75 50.26 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Line 75 4.46 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Branch 75 0 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Line 99 96.03 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Branch 99 92.76 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes Line 99 97.73 🔻
Microsoft.Extensions.ServiceDiscovery.Dns Line 75 69.93 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Line 75 42.11 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Branch 75 42.86 🔻
Microsoft.Extensions.ServiceDiscovery Line 75 67.66 🔻
Microsoft.Extensions.ServiceDiscovery Branch 75 71.43 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Line 75 73.85 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Branch 75 70 🔻
Microsoft.Extensions.VectorData.Abstractions Line 75 37.39 🔻
Microsoft.Extensions.VectorData.Abstractions Branch 75 22.73 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Gen.BuildMetadata 97 100
Microsoft.Gen.MetadataExtractor 57 73
Microsoft.Gen.MetricsReports 67 69
Microsoft.Extensions.AI.Abstractions 82 86
Microsoft.Extensions.AI.Evaluation.NLP 0 78
Microsoft.Extensions.Caching.Hybrid 82 89
Microsoft.Extensions.DataIngestion 75 89
Microsoft.Extensions.DataIngestion.Markdig 75 90
Microsoft.Extensions.Http.Resilience 97 100

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1500144&view=codecoverage-tab

@jozkee jozkee requested a review from Copilot July 8, 2026 21:37
@jozkee jozkee marked this pull request as ready for review July 8, 2026 21:37
@jozkee jozkee requested review from a team as code owners July 8, 2026 21:37
@jozkee jozkee changed the title Adopt targeted AI naming attributes for function and parameter identifiers Add AIFunctionNameAttribute and AIParameterNameAttribute Jul 8, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Comment thread src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactory.cs Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated no new comments.

@dotnet-comment-bot

Copy link
Copy Markdown
Collaborator

‼️ Found issues ‼️

Project Coverage Type Expected Actual
Microsoft.Extensions.Diagnostics.Testing Line 99 98.65 🔻
Microsoft.Extensions.Telemetry Line 93 92.64 🔻
Microsoft.Extensions.AI Line 89 88.55 🔻
Microsoft.Extensions.AI Branch 89 88.57 🔻
Microsoft.Extensions.AI.OpenAI Line 75 62.75 🔻
Microsoft.Extensions.AI.OpenAI Branch 75 50.26 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Line 75 4.46 🔻
Microsoft.Extensions.DataIngestion.MarkItDown Branch 75 0 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Line 99 96.03 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring Branch 99 92.76 🔻
Microsoft.Extensions.Diagnostics.ResourceMonitoring.Kubernetes Line 99 97.73 🔻
Microsoft.Extensions.ServiceDiscovery.Dns Line 75 71.61 🔻
Microsoft.Extensions.ServiceDiscovery Line 75 67.21 🔻
Microsoft.Extensions.ServiceDiscovery Branch 75 71.43 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Line 75 42.11 🔻
Microsoft.Extensions.ServiceDiscovery.Abstractions Branch 75 42.86 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Line 75 73.85 🔻
Microsoft.Extensions.ServiceDiscovery.Yarp Branch 75 70 🔻
Microsoft.Extensions.VectorData.Abstractions Line 75 37.39 🔻
Microsoft.Extensions.VectorData.Abstractions Branch 75 22.73 🔻

🎉 Good job! The coverage increased 🎉
Update MinCodeCoverage in the project files.

Project Expected Actual
Microsoft.Gen.BuildMetadata 97 100
Microsoft.Gen.MetadataExtractor 57 73
Microsoft.Gen.MetricsReports 67 69
Microsoft.Extensions.AI.Abstractions 82 86
Microsoft.Extensions.AI.Evaluation.NLP 0 78
Microsoft.Extensions.Caching.Hybrid 82 85
Microsoft.Extensions.DataIngestion 75 89
Microsoft.Extensions.DataIngestion.Markdig 75 90
Microsoft.Extensions.Http.Resilience 97 100

Full code coverage report: https://dev.azure.com/dnceng-public/public/_build/results?buildId=1500672&view=codecoverage-tab

@eiriktsarpalis eiriktsarpalis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@jozkee jozkee merged commit 06759bd into main Jul 9, 2026
5 checks passed
@jozkee jozkee deleted the copilot/ship-two-attributes branch July 9, 2026 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ability to change name of arguments inside AIFunction parameters in MEAI

6 participants