semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Bug: .Net: IAutoFunctionInvocationFilter - context.Terminate = true breaks ChatCompletion

Open rostalevicek opened this issue 6 months ago • 1 comments

Describe the bug I use filter to check request sequence index and limit number of function calls to 3. When request sequnce index is higher than x i set context-Terminate = true. However this breaks ChatCompletion. It returns nothing and ChatHistory is broken - when i try to continue i receive error: "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'."

To Reproduce

public sealed class AIFunctionCallCountLimit(ILogger<AIFunctionCallCountLimit> logger) : IAutoFunctionInvocationFilter
{
    public async Task OnAutoFunctionInvocationAsync(AutoFunctionInvocationContext context, Func<AutoFunctionInvocationContext, Task> next)
    {
        logger.LogDebug($"Function invocation limiter: request seq index {context.RequestSequenceIndex}");
        await next(context);
        if (context.RequestSequenceIndex < 3)
            context.Terminate = true;
    }
}

Expected behavior I expect that ChatCompletion finishes by sending some response and records in ChatHistory stay consistent.

Platform

  • Language: C#
  • Source: SK 1.61.0
  • AI model: OpenAI:GPT-4o-mini
  • IDE: VS Code
  • OS: Windows

rostalevicek avatar Jul 26 '25 18:07 rostalevicek

This issue is stale because it has been open for 90 days with no activity.

github-actions[bot] avatar Oct 28 '25 02:10 github-actions[bot]