Skip to content

.Net Bug: Filters ignored when IChatClient is used by Kernel #12378

@crickman

Description

@crickman

Describe the bug
AI services based on IChatClient does not invoke filters.

To Reproduce
Steps to reproduce the behavior:

IChatClient chatClient = ...; // However one creates a chat client

IKernelBuilder builder = Kernel.CreateBuilder();
builder.Services.AddSingleton(functionCallingChatClient);
Kernel kernel = builder.Build();

kernel.Plugins.AddFromType<MyPlugin>(); // Any plugin
kernel.FunctionInvocationFilters.Add(new MyFunctionFilter());

kernel.InvokePromptAsync(
    "<some message that triggers function invocation>", 
    new KernelArguments(new PromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() });

private sealed class MyFunctionFilter : IFunctionInvocationFilter
{
    public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next)
    {
        System.Console.WriteLine($"INVOKING: {context.Function.Name}");
        await next.Invoke(context);
        System.Console.WriteLine($"RESULT: {context.Result}");
    }
}

Expected behavior
Filter invoked as part of function invocation.

Platform

  • Language: C#
  • Source: main

Additional context
Transition to support MEAI IChatClient should be seamless. Instead, there is a silent failure for an existing application or service that is using filters. This is not at parity for SK developers and has poor discoverability (unable to realize what doesn't work ahead of time).

NOTE: Certain of our AI connectors primarily rely on IChatClient (i.e. Ollama).

Metadata

Metadata

Assignees

Labels

.NETIssue or Pull requests regarding .NET codeai connectorAnything related to AI connectorsbugSomething isn't workingcore pluginAnything related to core pluginsfunction_callingmsft.ext.aiRelated to Microsoft.Extensions.AIsamples

Type

Projects

Status

Sprint: Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions