microsoft/semantic-kernel · error · KernelException

Auto-invocation with AnyFunction is not supported when no ke

Error message

Auto-invocation with AnyFunction is not supported when no kernel is provided.

What it means

Thrown as a KernelException when the AnyFunction tool-call behavior has auto-invocation enabled but no kernel is provided. AnyFunction advertises all kernel functions to the model, and auto-invocation must be able to execute any of them, so a null kernel is a fatal configuration error detected before the request is sent.

Source

Thrown at dotnet/src/Connectors/Connectors.MistralAI/MistralAIToolCallBehavior.cs:191

        public override string ToString() => $"{nameof(AnyFunction)}(autoInvoke:{this.MaximumAutoInvokeAttempts != 0}): {string.Join(", ", this._kernelFunctionMetadata!.Select(f => f.Name))}";

        internal override void ConfigureRequest(Kernel? kernel, ChatCompletionRequest request)
        {
            if (this._kernelFunctionMetadata is null)
            {
                return;
            }

            // If auto-invocation is specified, we need a kernel to be able to invoke the functions.
            // Lack of a kernel is fatal: we don't want to tell the model we can handle the functions
            // and then fail to do so, so we fail before we get to that point. This is an error
            // on the consumers behalf: if they specify auto-invocation with any functions, they must
            // specify the kernel and the kernel must contain those functions.
            bool autoInvoke = base.MaximumAutoInvokeAttempts > 0;
            if (autoInvoke && kernel is null)
            {
                throw new KernelException($"Auto-invocation with {nameof(AnyFunction)} is not supported when no kernel is provided.");
            }

            foreach (var metadata in this._kernelFunctionMetadata)
            {
                // Make sure that if auto-invocation is specified, every enabled function can be found in the kernel.
                if (autoInvoke)
                {
                    Debug.Assert(kernel is not null);
                    if (!kernel!.Plugins.TryGetFunction(metadata.PluginName, metadata.Name, out _))
                    {
                        throw new KernelException($"The specified {nameof(RequiredFunctions)} function {metadata.PluginName}-{metadata.Name} is not available in the kernel.");
                    }
                }
            }

            request.ToolChoice = "any";

            foreach (var functionMetadata in this._kernelFunctionMetadata)

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Pass a non-null Kernel when using AnyFunction with auto-invocation.
  2. Disable auto-invocation (MaximumAutoInvokeAttempts = 0) if you only want the model to suggest tool calls without executing them.
  3. Use RequiredFunctions with explicit function names if the full kernel is not available.

Example fix

// before
var settings = new MistralAIPromptExecutionSettings
{
    ToolCallBehavior = MistralAIToolCallBehavior.AnyFunction
};
await service.GetChatMessageContentAsync(history, settings, kernel: null);

// after
await service.GetChatMessageContentAsync(history, settings, kernel: myKernel);
Defensive patterns

Strategy: validation

Validate before calling

if (behavior is { MaximumAutoInvokeAttempts: > 0 } && kernel is null)
{
    throw new InvalidOperationException("AnyFunction auto-invocation requires a kernel.");
}

Type guard

static bool SupportsAnyFunctionAutoInvoke(Kernel? kernel, int maxAttempts) =>
    maxAttempts == 0 || kernel is not null;

Prevention

When it happens

Trigger: Using MistralAIToolCallBehavior.AnyFunction (or enabling auto-invoke with it) and calling the completion API without a kernel; setting MaximumAutoInvokeAttempts > 0 on the behavior while the kernel is null.

Common situations: Switching from RequiredFunctions to AnyFunction without adding the kernel parameter; service-level code that conditionally builds the kernel but skips it in a test or early-return path.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/d4b89bf7c247ac34. Report an issue: GitHub.