{"record":{"id":"d4b89bf7c247ac34","repo":"microsoft/semantic-kernel","slug":"auto-invocation-with-anyfunction-is-not-supported","errorCode":null,"errorMessage":"Auto-invocation with AnyFunction is not supported when no kernel is provided.","messagePattern":"Auto-invocation with AnyFunction is not supported when no kernel is provided\\.","errorType":"exception","errorClass":"KernelException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.MistralAI/MistralAIToolCallBehavior.cs","lineNumber":191,"sourceCode":"\n        public override string ToString() => $\"{nameof(AnyFunction)}(autoInvoke:{this.MaximumAutoInvokeAttempts != 0}): {string.Join(\", \", this._kernelFunctionMetadata!.Select(f => f.Name))}\";\n\n        internal override void ConfigureRequest(Kernel? kernel, ChatCompletionRequest request)\n        {\n            if (this._kernelFunctionMetadata is null)\n            {\n                return;\n            }\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            bool autoInvoke = base.MaximumAutoInvokeAttempts > 0;\n            if (autoInvoke && kernel is null)\n            {\n                throw new KernelException($\"Auto-invocation with {nameof(AnyFunction)} is not supported when no kernel is provided.\");\n            }\n\n            foreach (var metadata in this._kernelFunctionMetadata)\n            {\n                // Make sure that if auto-invocation is specified, every enabled function can be found in the kernel.\n                if (autoInvoke)\n                {\n                    Debug.Assert(kernel is not null);\n                    if (!kernel!.Plugins.TryGetFunction(metadata.PluginName, metadata.Name, out _))\n                    {\n                        throw new KernelException($\"The specified {nameof(RequiredFunctions)} function {metadata.PluginName}-{metadata.Name} is not available in the kernel.\");\n                    }\n                }\n            }\n\n            request.ToolChoice = \"any\";\n\n            foreach (var functionMetadata in this._kernelFunctionMetadata)","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.MistralAI/MistralAIToolCallBehavior.cs#L173-L209","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass a non-null Kernel when using AnyFunction with auto-invocation.","Disable auto-invocation (MaximumAutoInvokeAttempts = 0) if you only want the model to suggest tool calls without executing them.","Use RequiredFunctions with explicit function names if the full kernel is not available."],"exampleFix":"// before\nvar settings = new MistralAIPromptExecutionSettings\n{\n    ToolCallBehavior = MistralAIToolCallBehavior.AnyFunction\n};\nawait service.GetChatMessageContentAsync(history, settings, kernel: null);\n\n// after\nawait service.GetChatMessageContentAsync(history, settings, kernel: myKernel);","handlingStrategy":"validation","validationCode":"if (behavior is { MaximumAutoInvokeAttempts: > 0 } && kernel is null)\n{\n    throw new InvalidOperationException(\"AnyFunction auto-invocation requires a kernel.\");\n}","typeGuard":"static bool SupportsAnyFunctionAutoInvoke(Kernel? kernel, int maxAttempts) =>\n    maxAttempts == 0 || kernel is not null;","tryCatchPattern":null,"preventionTips":["Ensure the kernel is available when using AnyFunction behavior.","Disable auto-invoke if only tool-call suggestions are needed.","Use RequiredFunctions with explicit names as a fallback when a full kernel is unavailable."],"tags":["mistralai","tool-calling","auto-invocation","any-function","kernel-exception"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}