Skip to content

.Net: Bug: Converting Kernel Plugins to AIFunctions fails name validation #10819

Description

@cdock1029

Describe the bug
Extracting an IChatClient from a Kernel with Plugins, transforming the Plugins to Tools / AIFunctions, then using the IChatClient fails

To Reproduce

 serviceCollection
     .AddKernel()
     .AddOpenAIChatCompletion(apiKey: chatConfig!.OpenAiApiKey!, modelId: "gpt-4o-mini")
     .Plugins
     .AddFromType<UtilitiesPlugin>()
     .AddFromType<TreasuryPlugin>();

 serviceCollection.AddChatClient(serviceProvider =>
 {
     Kernel kernel = serviceProvider.GetRequiredService<Kernel>();
     IChatCompletionService completionService = kernel.GetRequiredService<IChatCompletionService>();
     IChatClient client = completionService.AsChatClient();
     ChatClientBuilder builder = new ChatClientBuilder(client)
         .UseFunctionInvocation()
         .ConfigureOptions(options =>
         {
             IEnumerable<AIFunction> aiFunctions =
                 kernel.Plugins.SelectMany(kp => kp.AsAIFunctions());
             options.Tools = [..aiFunctions];
             options.ToolMode = ChatToolMode.Auto;
         });
     return builder.Build();
 });

public sealed class UtilitiesPlugin
{
    [KernelFunction("get_current_utc_time")]
    public string GetCurrentUtcTime()
    {
        return DateTime.UtcNow.ToString("R");
    }
}

//elsewhere
 var result = await Client.GetResponseAsync("Who are you?");

Error:

System.ArgumentException: A function name can contain only ASCII letters, digits, and underscores: 'UtilitiesPlugin-get_current_utc_time' is not a valid name. (Parameter 'value')
   at Microsoft.SemanticKernel.Verify.ThrowArgumentInvalidName(String kind, String name, String paramName)
   at Microsoft.SemanticKernel.Verify.ValidFunctionName(String functionName, String paramName)
   at Microsoft.SemanticKernel.KernelFunctionMetadata.set_Name(String value)

Expected behavior
Successfully sends messages, whether calling functions or not.

Platform

  • Language: C#
  • Source: Microsoft.SemanticKernel.Abstractions Versions 1.40.1 and 1.31.0
  • AI model: OpenAI and AzureAIInference
  • IDE: Visual Studio
  • OS: Windows

Additional context

In AsAIFunction KernelAIFunction , function names were changed from having an underscore to dash in this commit

ca5e285#diff-38953b1c66d724196bc0bea9aef0d9d3ddd15ad62457ff9cb906b6616a98318fL566

which conflicts with ValidFunctionName which disallows dashes.

Discussion #10815

Metadata

Metadata

Assignees

Labels

.NETIssue or Pull requests regarding .NET codebugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions