microsoft/semantic-kernel · error · KernelException

Auto-invocation with {nameof(EnabledFunctions)} is not suppo

Error message

Auto-invocation with {nameof(EnabledFunctions)} is not supported when no kernel is provided.

What it means

Thrown by EnabledFunctions.ConfigureGeminiRequest when auto-invocation is requested (MaximumAutoInvokeAttempts > 0) but the kernel passed to the request is null. Auto-invocation requires a kernel to actually execute the function the model calls back, so the connector fails fast rather than advertising functions it cannot fulfill.

Source

Thrown at dotnet/src/Connectors/Connectors.Google/GeminiToolCallBehavior.cs:176

            $"{string.Join(", ", this._functions.Select(f => f.FunctionName))}";

        internal override void ConfigureGeminiRequest(Kernel? kernel, GeminiRequest request)
        {
            if (this._functions.Length == 0)
            {
                return;
            }

            bool autoInvoke = this.MaximumAutoInvokeAttempts > 0;

            // 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.
            if (autoInvoke && kernel is null)
            {
                throw new KernelException($"Auto-invocation with {nameof(EnabledFunctions)} is not supported when no kernel is provided.");
            }

            foreach (var func in this._functions)
            {
                // Make sure that if auto-invocation is specified, every enabled function can be found in the kernel.
                if (autoInvoke)
                {
                    if (!kernel!.Plugins.TryGetFunction(func.PluginName, func.FunctionName, out _))
                    {
                        throw new KernelException(
                            $"The specified {nameof(EnabledFunctions)} function {func.FullyQualifiedName} is not available in the kernel.");
                    }
                }

                // Add the function.
                request.AddFunction(func);
            }
        }

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Invoke through the kernel (kernel.InvokePromptAsync / kernel.InvokeAsync) so the kernel is supplied to the request configuration.
  2. If you must call the service directly, pass a non-null kernel, or disable auto-invocation (use EnabledFunctions with autoInvoke: false).
  3. Register the Gemini chat completion service in a Kernel and use that kernel for tool-calling requests.

Example fix

// before (no kernel -> throws)
var behavior = GeminiToolCallBehavior.EnabledFunctions(autoInvoke: true, functions);
var svc = new GeminiChatCompletionService(...);
await svc.GetChatMessageContentAsync(history, settings with { ToolCallBehavior = behavior });

// after (invoke through kernel)
var kernel = builder.Build();
kernel.Plugins.AddFromFunctions("p", functions);
var settings = new GeminiPromptExecutionSettings { ToolCallBehavior = GeminiToolCallBehavior.EnabledFunctions(autoInvoke: true, functions) };
await kernel.InvokePromptAsync(prompt, new(settings));
Defensive patterns

Strategy: validation

Validate before calling

static GeminiToolCallBehavior SafeBehavior(bool autoInvoke, Kernel? kernel, IEnumerable<GeminiFunction> fns)
{
    if (autoInvoke && kernel is null)
        throw new InvalidOperationException("Auto-invocation requires a non-null kernel.");
    return GeminiToolCallBehavior.EnabledFunctions(autoInvoke, fns);
}

Type guard

static bool CanAutoInvoke(Kernel? kernel, bool autoInvoke) => !autoInvoke || kernel is not null;

Try / catch

try { await kernel.InvokePromptAsync(prompt, new(settings)); }
catch (KernelException ex) when (ex.Message.Contains("Auto-invocation") && ex.Message.Contains("no kernel"))
{ logger.LogError(ex, "Pass a kernel or disable autoInvoke."); throw; }

Prevention

When it happens

Trigger: Using GeminiToolCallBehavior.EnabledFunctions with autoInvoke: true in a path that calls GetChatMessageContentsAsync / GetStreamingChatMessageContentsAsync without passing a Kernel (e.g. calling the chat completion service directly via its interface with kernel: null).

Common situations: Invoking the IChatCompletionService directly instead of through KernelExtensions; building a GeminiChatCompletionService without registering it in a Kernel and then calling it standalone with a tool-call behavior that has autoInvoke on.

Related errors


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