microsoft/semantic-kernel · error · InvalidOperationException
Invalid kernel selection. {selectedKernelName} is not a vali
Error message
Invalid kernel selection. {selectedKernelName} is not a valid kernel. What it means
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.
Source
Thrown at dotnet/samples/Demos/CopilotAgentPlugins/CopilotAgentPluginsDemoSample/DemoCommand.cs:55
var availableCopilotPlugins = Directory.GetDirectories($"../../../Concepts/Resources/Plugins/{CopilotAgentPluginsDirectory}");
var selectedKernelName = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("[green]SELECT KERNEL TO USE:[/]")
.AddChoices([
"azureopenai",
"openai",
"ollama"
]));
var enableLogging = settings.EnableLogging == true;
var (kernel, promptSettings) = selectedKernelName switch
{
"azureopenai" => InitializeAzureOpenAiKernel(configuration, enableLogging: enableLogging),
"openai" => InitializeOpenAiKernel(configuration, enableLogging: enableLogging),
"ollama" => InitializeKernelForOllama(configuration, enableLogging: enableLogging),
_ => throw new InvalidOperationException($"Invalid kernel selection. {selectedKernelName} is not a valid kernel.")
};
kernel.AutoFunctionInvocationFilters.Add(new ExpectedSchemaFunctionFilter());
while (true)
{
const string LOAD_COPILOT_AGENT_PLUGIN = "Load Copilot Agent plugin(s)";
const string LOAD_ALL_COPILOT_AGENT_PLUGINS = "Load all available Copilot Agent plugins";
const string UNLOAD_ALL_PLUGINS = "Unload all plugins";
const string SHOW_COPILOT_AGENT_MANIFEST = "Show Copilot Agent manifest";
const string EXECUTE_GOAL = "Execute a goal";
const string LIST_LOADED_PLUGINS = "List loaded plugins";
const string LIST_LOADED_PLUGINS_WITH_FUNCTIONS = "List loaded plugins with functions";
const string LIST_LOADED_PLUGINS_WITH_FUNCTIONS_AND_PARAMETERS = "List loaded plugins with functions and parameters";
const string EXIT = "Exit";
AnsiConsole.WriteLine();
var selection = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("SELECT AN OPTION:")View on GitHub (pinned to c028a0c7dc)
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.
Example fix
// before
_ => throw new InvalidOperationException($"Invalid kernel selection. {selectedKernelName} is not a valid kernel.")
// after
selectedKernelName = (selectedKernelName ?? "").Trim().ToLowerInvariant();
var (kernel, promptSettings) = selectedKernelName switch
{
"azureopenai" => ...,
"openai" => ...,
"ollama" => ...,
_ => throw new ArgumentOutOfRangeException(nameof(selectedKernelName),
$"'{selectedKernelName}' is not supported. Use: azureopenai, openai, ollama.")
}; Defensive patterns
Strategy: validation
Validate before calling
selectedKernelName = (selectedKernelName ?? "").Trim().ToLowerInvariant();
if (selectedKernelName is not ("azureopenai" or "openai" or "ollama"))
throw new ArgumentOutOfRangeException(nameof(selectedKernelName),
"Use one of: azureopenai, openai, ollama."); Type guard
static readonly HashSet<string> s_kernels = new(StringComparer.OrdinalIgnoreCase)
{ "azureopenai", "openai", "ollama" };
static bool IsValidKernel(string? name) => name is not null && s_kernels.Contains(name); Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Please provide valid Ollama configuration in appsettings.Dev
- Please provide valid AzureOpenAI configuration in appsetting
- Please provide valid OpenAI configuration in appsettings.Dev
- Invalid choice
- Configuration is not setup correctly.
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/353fb0782343e4fd.
Report an issue: GitHub.