{"record":{"id":"923d2c8d32deed6a","repo":"microsoft/semantic-kernel","slug":"auto-invocation-with-nameof-enabledfunctions-is","errorCode":null,"errorMessage":"Auto-invocation with {nameof(EnabledFunctions)} is not supported when no kernel is provided.","messagePattern":"Auto-invocation with (.+?) is not supported when no kernel is provided\\.","errorType":"exception","errorClass":"KernelException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.Google/GeminiToolCallBehavior.cs","lineNumber":176,"sourceCode":"            $\"{string.Join(\", \", this._functions.Select(f => f.FunctionName))}\";\n\n        internal override void ConfigureGeminiRequest(Kernel? kernel, GeminiRequest request)\n        {\n            if (this._functions.Length == 0)\n            {\n                return;\n            }\n\n            bool autoInvoke = this.MaximumAutoInvokeAttempts > 0;\n\n            // If auto-invocation is specified, we need a kernel to be able to invoke the functions.\n            // Lack of a kernel is fatal: we don't want to tell the model we can handle the functions\n            // and then fail to do so, so we fail before we get to that point. This is an error\n            // on the consumers behalf: if they specify auto-invocation with any functions, they must\n            // specify the kernel and the kernel must contain those functions.\n            if (autoInvoke && kernel is null)\n            {\n                throw new KernelException($\"Auto-invocation with {nameof(EnabledFunctions)} is not supported when no kernel is provided.\");\n            }\n\n            foreach (var func in this._functions)\n            {\n                // Make sure that if auto-invocation is specified, every enabled function can be found in the kernel.\n                if (autoInvoke)\n                {\n                    if (!kernel!.Plugins.TryGetFunction(func.PluginName, func.FunctionName, out _))\n                    {\n                        throw new KernelException(\n                            $\"The specified {nameof(EnabledFunctions)} function {func.FullyQualifiedName} is not available in the kernel.\");\n                    }\n                }\n\n                // Add the function.\n                request.AddFunction(func);\n            }\n        }","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.Google/GeminiToolCallBehavior.cs#L158-L194","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Invoke through the kernel (kernel.InvokePromptAsync / kernel.InvokeAsync) so the kernel is supplied to the request configuration.","If you must call the service directly, pass a non-null kernel, or disable auto-invocation (use EnabledFunctions with autoInvoke: false).","Register the Gemini chat completion service in a Kernel and use that kernel for tool-calling requests."],"exampleFix":"// before (no kernel -> throws)\nvar behavior = GeminiToolCallBehavior.EnabledFunctions(autoInvoke: true, functions);\nvar svc = new GeminiChatCompletionService(...);\nawait svc.GetChatMessageContentAsync(history, settings with { ToolCallBehavior = behavior });\n\n// after (invoke through kernel)\nvar kernel = builder.Build();\nkernel.Plugins.AddFromFunctions(\"p\", functions);\nvar settings = new GeminiPromptExecutionSettings { ToolCallBehavior = GeminiToolCallBehavior.EnabledFunctions(autoInvoke: true, functions) };\nawait kernel.InvokePromptAsync(prompt, new(settings));","handlingStrategy":"validation","validationCode":"static GeminiToolCallBehavior SafeBehavior(bool autoInvoke, Kernel? kernel, IEnumerable<GeminiFunction> fns)\n{\n    if (autoInvoke && kernel is null)\n        throw new InvalidOperationException(\"Auto-invocation requires a non-null kernel.\");\n    return GeminiToolCallBehavior.EnabledFunctions(autoInvoke, fns);\n}","typeGuard":"static bool CanAutoInvoke(Kernel? kernel, bool autoInvoke) => !autoInvoke || kernel is not null;","tryCatchPattern":"try { await kernel.InvokePromptAsync(prompt, new(settings)); }\ncatch (KernelException ex) when (ex.Message.Contains(\"Auto-invocation\") && ex.Message.Contains(\"no kernel\"))\n{ logger.LogError(ex, \"Pass a kernel or disable autoInvoke.\"); throw; }","preventionTips":["Always invoke tool-calling requests through the kernel.","If calling a service standalone, set autoInvoke: false.","Register the chat service in a Kernel and use it for any auto-invocation request."],"tags":["gemini","tool-calling","auto-invocation","kernel"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}