Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 21, 2025

  • Understand the issue requirements (support DisplayNameAttribute for AIFunction name)
  • Explore the repository structure and locate relevant files
  • Generate filtered solution for AI projects
  • Modify AIFunctionFactory.cs to support DisplayNameAttribute
  • Add focused tests for DisplayNameAttribute support
  • Build and test changes - all tests pass (352 AI tests, 1042 AI.Abstractions tests)
  • Manual verification with test programs - confirmed working correctly
  • Review feedback: Add DisplayNameAttribute support to other locations (ChatResponseFormat, AIJsonUtilities)
  • Revert nuget.exe and .gitignore changes per review feedback
  • Fix analyzer warnings and update documentation
  • All verification complete

Summary of Changes

This PR implements support for the DisplayNameAttribute to define names throughout the AI libraries, as requested in the issue.

Changes Made:

  1. AIFunctionFactory.cs: Modified name resolution logic to check for DisplayNameAttribute before falling back to method name. Updated documentation for name parameters to mention DisplayNameAttribute.
  2. ChatResponseFormat.cs: Added DisplayNameAttribute support for schema names when creating response formats from types
  3. AIJsonUtilities.Schema.Create.cs: Added DisplayNameAttribute support for method titles in JSON schema creation
  4. AIFunctionFactoryOptions.cs: Updated documentation for the Name property to mention DisplayNameAttribute
  5. AIFunctionFactoryTest.cs: Added comprehensive tests for DisplayNameAttribute on functions
  6. ChatResponseFormatTests.cs: Added tests for DisplayNameAttribute on schema types
  7. AIJsonUtilitiesTests.cs: Added tests for DisplayNameAttribute on methods (with comments to fix analyzer warnings)
  8. TestJsonSerializerContext.cs: Added TypeWithDisplayName to the serialization context

Priority Chain:

The resolution priority for names is now consistently (across all locations):

  1. Explicit parameter (e.g., name, schemaName, title)
  2. DisplayNameAttribute (NEW)
  3. Member name (method/type name as fallback)

Tests:

  • All existing tests pass (352 AI tests, 1,048 AI.Abstractions tests)
  • New tests verify DisplayNameAttribute works correctly in all three locations:
    • AIFunctionFactory for function names
    • ChatResponseFormat for schema names
    • AIJsonUtilities for method titles in JSON schemas
  • No analyzer warnings

Example Usage:

// For functions
[DisplayName("get_user_id")]
[Description("Gets the user ID")]
void GetUserId() { }
var f = AIFunctionFactory.Create(GetUserId);
// f.Name is "get_user_id"

// For response format schemas
[DisplayName("user_data")]
[Description("User information")]
public class UserData { public int Id { get; set; } }
var format = ChatResponseFormat.ForJsonSchema<UserData>();
// format.SchemaName is "user_data"

Security Summary:

The changes are minimal and safe:

  • Only added attribute lookups using existing .NET reflection APIs
  • No new security vulnerabilities introduced
  • All existing tests pass
  • Manual testing confirms correct behavior
Original prompt

This section details on the original issue you should resolve

<issue_title>Support DisplayNameAttribute as AIFunction name</issue_title>
<issue_description>```csharp
var f = AIFunctionFactory.Create(GetUserId);

[Description("This does this")]
void GetUserId() { }


This code generates an AIFunction with a description resolved from the [DescriptionAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.descriptionattribute) and a name resolved from the C# function name. That latter is usually never great. At best you'll get a pascal cased `GetUserId` which I understood is not ideal because LLMs are trained on snake cased code. At worst, you'll get `_Main_g_GetUserId_0_0`.

To have a better name you can simply specify a second parameter
```csharp
var f = AIFunctionFactory.Create(GetUserId, "get_user_id");

This works fine but it forces defining the name at a different place than the method which is inconvenient. For example, I would like to create several classes that define some methods, with no dependencies on anything AI, then in the Startup of my agent, register these services and create tools from their methods without having to give them new names.

Would it make sense to support DisplayNameAttribute to define the name of the tool?

[DisplayName("get_user_id")]
[Description("This does this")]
void GetUserId() { }

The resolution priority of the name would become "AIFunctionFactory parameter" > DisplayNameAttribute > C# method name.</issue_description>

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

@stephentoub @eiriktsarpalis, any opinion? @eiriktsarpalis Sounds reasonable.

Fixes #6941


✨ 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

@eiriktsarpalis eiriktsarpalis marked this pull request as ready for review October 21, 2025 17:05
@eiriktsarpalis eiriktsarpalis requested a review from a team as a code owner October 21, 2025 17:05
Copilot AI review requested due to automatic review settings October 21, 2025 17:05
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

This PR adds support for using DisplayNameAttribute to specify the name of AI functions created via AIFunctionFactory, addressing issue #6941. This allows developers to define function names alongside their method definitions using attributes, rather than having to specify names separately during factory creation.

Key changes:

  • Modified AIFunctionFactory.cs to check for DisplayNameAttribute when resolving function names
  • Added comprehensive test coverage for the new functionality including precedence rules

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/Functions/AIFunctionFactory.cs Updated name resolution logic to check for DisplayNameAttribute between explicit name parameter and fallback to method name
test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs Added test method with multiple test cases covering DisplayNameAttribute functionality and precedence rules

Copilot AI and others added 2 commits October 21, 2025 17:13
Copilot AI changed the title [WIP] Add support for DisplayNameAttribute in AIFunction name Support DisplayNameAttribute for AIFunction name resolution Oct 21, 2025
Copilot finished work on behalf of eiriktsarpalis October 21, 2025 17:22
Copilot AI requested a review from eiriktsarpalis October 21, 2025 17:22
@stephentoub
Copy link
Member

@copilot, please search the repo for Description Attribute. Anywhere it's being referenced, there's likely a nearby place that should be updated for Display Name as well. For example in AIFunctionFactoryOptions, ChatResponse Format, etc

Copy link
Contributor Author

Copilot AI commented Oct 21, 2025

@copilot, please search the repo for Description Attribute. Anywhere it's being referenced, there's likely a nearby place that should be updated for Display Name as well. For example in AIFunctionFact...

I've added DisplayNameAttribute support to the following locations where DescriptionAttribute is used:

  1. ChatResponseFormat.cs (line 92) - Added DisplayNameAttribute support for schemaName
  2. AIJsonUtilities.Schema.Create.cs (line 83) - Added DisplayNameAttribute support for method title in JSON schema

The changes maintain the same priority order across all locations:

  • Explicit parameter > DisplayNameAttribute > member name

Added comprehensive tests for all three locations and all tests pass (352 AI tests, 1,048 AI.Abstractions tests).

Copilot AI changed the title Support DisplayNameAttribute for AIFunction name resolution Support DisplayNameAttribute for name resolution in AI libraries Oct 21, 2025
Copilot AI requested a review from stephentoub October 21, 2025 17:58
Copilot finished work on behalf of stephentoub October 21, 2025 17:58
Copilot finished work on behalf of stephentoub October 21, 2025 18:04
Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

@copilot, please fix the analyzer warnings:

Build FAILED.

/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs(431,21): error S1186: Add a nested comment explaining why this method is empty, throw a 'NotSupportedException' or complete the implementation. (https://rules.sonarsource.com/csharp/RSPEC-1186) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Microsoft.Extensions.AI.Abstractions.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs(432,9): error S108: Either remove or fill this block of code. (https://rules.sonarsource.com/csharp/RSPEC-108) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Microsoft.Extensions.AI.Abstractions.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs(449,21): error S1186: Add a nested comment explaining why this method is empty, throw a 'NotSupportedException' or complete the implementation. (https://rules.sonarsource.com/csharp/RSPEC-1186) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Microsoft.Extensions.AI.Abstractions.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs(450,9): error S108: Either remove or fill this block of code. (https://rules.sonarsource.com/csharp/RSPEC-108) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Microsoft.Extensions.AI.Abstractions.Tests.csproj::TargetFramework=net9.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs(431,21): error S1186: Add a nested comment explaining why this method is empty, throw a 'NotSupportedException' or complete the implementation. (https://rules.sonarsource.com/csharp/RSPEC-1186) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Microsoft.Extensions.AI.Abstractions.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs(432,9): error S108: Either remove or fill this block of code. (https://rules.sonarsource.com/csharp/RSPEC-108) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Microsoft.Extensions.AI.Abstractions.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs(449,21): error S1186: Add a nested comment explaining why this method is empty, throw a 'NotSupportedException' or complete the implementation. (https://rules.sonarsource.com/csharp/RSPEC-1186) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Microsoft.Extensions.AI.Abstractions.Tests.csproj::TargetFramework=net8.0]
/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs(450,9): error S108: Either remove or fill this block of code. (https://rules.sonarsource.com/csharp/RSPEC-108) [/mnt/vss/_work/1/s/test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Microsoft.Extensions.AI.Abstractions.Tests.csproj::TargetFramework=net8.0]
    0 Warning(s)
    8 Error(s)

This was referenced Nov 13, 2025
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.

Support DisplayNameAttribute as AIFunction name

3 participants