{"record":{"id":"919c6a76b2a48803","repo":"microsoft/semantic-kernel","slug":"auto-invocation-with-nameof-requiredfunction-is","errorCode":null,"errorMessage":"Auto-invocation with {nameof(RequiredFunction)} 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.OpenAI/ToolCallBehavior.cs","lineNumber":260,"sourceCode":"            this._function = function;\n            this._tool = function.ToFunctionDefinition(false);\n            this._choice = ChatToolChoice.CreateFunctionChoice(this._tool.FunctionName);\n        }\n\n        public override string ToString() => $\"{nameof(RequiredFunction)}(autoInvoke:{this.MaximumAutoInvokeAttempts != 0}): {this._tool.FunctionName}\";\n\n        internal override (IList<ChatTool>? Tools, ChatToolChoice? Choice) ConfigureOptions(Kernel? kernel)\n        {\n            bool autoInvoke = base.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(RequiredFunction)} is not supported when no kernel is provided.\");\n            }\n\n            // Make sure that if auto-invocation is specified, the required function can be found in the kernel.\n            if (autoInvoke && !kernel!.Plugins.TryGetFunction(this._function.PluginName, this._function.FunctionName, out _))\n            {\n                throw new KernelException($\"The specified {nameof(RequiredFunction)} function {this._function.FullyQualifiedName} is not available in the kernel.\");\n            }\n\n            return ([this._tool], this._choice);\n        }\n\n        /// <summary>Gets how many requests are part of a single interaction should include this tool in the request.</summary>\n        /// <remarks>\n        /// Unlike <see cref=\"EnabledFunctions\"/> and <see cref=\"KernelFunctions\"/>, this must use 1 as the maximum\n        /// use attempts. Otherwise, every call back to the model _requires_ it to invoke the function (as opposed\n        /// to allows it), which means we end up doing the same work over and over and over until the maximum is reached.\n        /// Thus for \"requires\", we must send the tool information only once.\n        /// </remarks>","sourceCodeStart":242,"sourceCodeEnd":278,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.OpenAI/ToolCallBehavior.cs#L242-L278","documentation":"RequiredFunction tool-call behavior forces the model to call a specific function. When auto-invocation is enabled (MaximumAutoInvokeAttempts > 0) but the kernel is null, the connector throws before sending the request. Unlike EnabledFunctions (which 'allows' calls), RequiredFunction 'forces' a call, so a missing kernel is always fatal — the model will call the function and there is nothing to execute it.","triggerScenarios":"Creating ToolCallBehavior.RequireFunction(function, autoInvoke: true) and calling chat completion without a kernel, or through a path where kernel is null.","commonSituations":"Same as [411]: custom orchestration omitting the kernel, direct SDK calls, or testing harnesses that invoke the model without a kernel.","solutions":["Pass a non-null Kernel that contains the required function.","Use autoInvoke: false if you intend to handle the required function call yourself.","Avoid RequireFunction unless you specifically need the model to always call one function — prefer EnableFunctions or KernelFunctions."],"exampleFix":"// before\nvar behavior = ToolCallBehavior.RequireFunction(myFunc, autoInvoke: true);\nawait chatService.GetChatMessageContentAsync(history, settings); // no kernel\n\n// after\nawait chatService.GetChatMessageContentAsync(history, settings, kernel: myKernel);","handlingStrategy":"validation","validationCode":"// RequireFunction with autoInvoke requires a kernel\nif (behavior is { MaximumAutoInvokeAttempts: > 0 } && kernel is null)\n{\n    throw new InvalidOperationException(\n        \"RequireFunction with auto-invocation needs a non-null Kernel.\");\n}","typeGuard":null,"tryCatchPattern":"try { await chatService.GetChatMessageContentAsync(history, settings, kernel); }\ncatch (KernelException ex) when (ex.Message.Contains(\"RequiredFunction\") && ex.Message.Contains(\"no kernel\"))\n{\n    throw new InvalidOperationException(\"Pass a Kernel when using RequireFunction with autoInvoke.\", ex);\n}","preventionTips":["Avoid RequireFunction unless you truly need forced function calls.","Ensure the kernel is always passed in chat calls when autoInvoke is on."],"tags":["tool-calling","auto-invocation","kernel","required-function"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}