{"record":{"id":"353fb0782343e4fd","repo":"microsoft/semantic-kernel","slug":"invalid-kernel-selection-selectedkernelname-is","errorCode":null,"errorMessage":"Invalid kernel selection. {selectedKernelName} is not a valid kernel.","messagePattern":"Invalid kernel selection\\. (.+?) is not a valid kernel\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/samples/Demos/CopilotAgentPlugins/CopilotAgentPluginsDemoSample/DemoCommand.cs","lineNumber":55,"sourceCode":"        var availableCopilotPlugins = Directory.GetDirectories($\"../../../Concepts/Resources/Plugins/{CopilotAgentPluginsDirectory}\");\n\n        var selectedKernelName = AnsiConsole.Prompt(\n            new SelectionPrompt<string>()\n                .Title(\"[green]SELECT KERNEL TO USE:[/]\")\n                .AddChoices([\n                    \"azureopenai\",\n                    \"openai\",\n                    \"ollama\"\n                ]));\n\n        var enableLogging = settings.EnableLogging == true;\n\n        var (kernel, promptSettings) = selectedKernelName switch\n        {\n            \"azureopenai\" => InitializeAzureOpenAiKernel(configuration, enableLogging: enableLogging),\n            \"openai\" => InitializeOpenAiKernel(configuration, enableLogging: enableLogging),\n            \"ollama\" => InitializeKernelForOllama(configuration, enableLogging: enableLogging),\n            _ => throw new InvalidOperationException($\"Invalid kernel selection. {selectedKernelName} is not a valid kernel.\")\n        };\n        kernel.AutoFunctionInvocationFilters.Add(new ExpectedSchemaFunctionFilter());\n\n        while (true)\n        {\n            const string LOAD_COPILOT_AGENT_PLUGIN = \"Load Copilot Agent plugin(s)\";\n            const string LOAD_ALL_COPILOT_AGENT_PLUGINS = \"Load all available Copilot Agent plugins\";\n            const string UNLOAD_ALL_PLUGINS = \"Unload all plugins\";\n            const string SHOW_COPILOT_AGENT_MANIFEST = \"Show Copilot Agent manifest\";\n            const string EXECUTE_GOAL = \"Execute a goal\";\n            const string LIST_LOADED_PLUGINS = \"List loaded plugins\";\n            const string LIST_LOADED_PLUGINS_WITH_FUNCTIONS = \"List loaded plugins with functions\";\n            const string LIST_LOADED_PLUGINS_WITH_FUNCTIONS_AND_PARAMETERS = \"List loaded plugins with functions and parameters\";\n            const string EXIT = \"Exit\";\n            AnsiConsole.WriteLine();\n            var selection = AnsiConsole.Prompt(\n                new SelectionPrompt<string>()\n                    .Title(\"SELECT AN OPTION:\")","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/samples/Demos/CopilotAgentPlugins/CopilotAgentPluginsDemoSample/DemoCommand.cs#L37-L73","documentation":"DemoCommand validates selectedKernelName against azureopenai/openai/ollama via a switch; the default arm throws InvalidOperationException. The input is constrained upstream by an AllowedValues setting, so reaching the default usually means the constraint was bypassed or the setting list desynced from the switch cases.","triggerScenarios":"selectedKernelName is a string other than 'azureopenai', 'openai', or 'ollama' (e.g. typo, different casing like 'AzureOpenAI', or a new provider added to settings but not to the switch).","commonSituations":"Case-sensitivity mismatch, a contributor adding a kernel provider to AllowedValues but not a switch arm, or passing the value programmatically with an unsupported string.","solutions":["Pass one of the exact supported values: 'azureopenai', 'openai', or 'ollama' (lowercase).","If adding a new provider, add both the AllowedValues entry AND a matching switch arm.","Normalize the input (Trim().ToLowerInvariant()) before the switch to avoid casing issues.","Use StringComparer.OrdinalIgnoreCase on the switch to make selection case-insensitive."],"exampleFix":"// before\n_ => throw new InvalidOperationException($\"Invalid kernel selection. {selectedKernelName} is not a valid kernel.\")\n\n// after\nselectedKernelName = (selectedKernelName ?? \"\").Trim().ToLowerInvariant();\nvar (kernel, promptSettings) = selectedKernelName switch\n{\n    \"azureopenai\" => ...,\n    \"openai\" => ...,\n    \"ollama\" => ...,\n    _ => throw new ArgumentOutOfRangeException(nameof(selectedKernelName),\n        $\"'{selectedKernelName}' is not supported. Use: azureopenai, openai, ollama.\")\n};","handlingStrategy":"validation","validationCode":"selectedKernelName = (selectedKernelName ?? \"\").Trim().ToLowerInvariant();\nif (selectedKernelName is not (\"azureopenai\" or \"openai\" or \"ollama\"))\n    throw new ArgumentOutOfRangeException(nameof(selectedKernelName),\n        \"Use one of: azureopenai, openai, ollama.\");","typeGuard":"static readonly HashSet<string> s_kernels = new(StringComparer.OrdinalIgnoreCase)\n    { \"azureopenai\", \"openai\", \"ollama\" };\nstatic bool IsValidKernel(string? name) => name is not null && s_kernels.Contains(name);","tryCatchPattern":null,"preventionTips":["Normalize input casing before the switch.","Keep the AllowedValues setting and switch arms in sync (single source of truth).","Use a HashSet for the valid set and validate before dispatching."],"tags":["validation","kernel","configuration","samples"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}