microsoft/semantic-kernel · error · InvalidOperationException
Invalid choice
Error message
Invalid choice
What it means
Thrown by the default arm of a console-menu switch in the Amazon Bedrock Models sample. Cases 1-4 dispatch to chat/text/stream demos; any other value falls through to a hard InvalidOperationException. It is a programmer/user-input guard, not a library error, so the message 'Invalid choice' refers to the menu digit entered at runtime.
Source
Thrown at dotnet/samples/Demos/AmazonBedrockModels/Program.cs:34
// Get user choice
int choice = GetUserChoice();
switch (choice)
{
case 1:
await PerformChatCompletion().ConfigureAwait(false);
break;
case 2:
await PerformTextGeneration().ConfigureAwait(false);
break;
case 3:
await PerformStreamChatCompletion().ConfigureAwait(false);
break;
case 4:
await PerformStreamTextGeneration().ConfigureAwait(false);
break;
default:
throw new InvalidOperationException("Invalid choice");
}
async Task PerformChatCompletion()
{
string userInput;
ChatHistory chatHistory = [];
// Get available chat completion models
var availableChatModels = bedrockModels.Values
.Where(m => m.Modalities.Contains(ModelDefinition.SupportedModality.ChatCompletion))
.ToDictionary(m => bedrockModels.Single(kvp => kvp.Value.Name == m.Name).Key, m => m.Name);
// Show user what models are available and let them choose
int chosenModel = GetModelNumber(availableChatModels, "chat completion");
var kernel = Kernel.CreateBuilder().AddBedrockChatCompletionService(availableChatModels[chosenModel]).Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
View on GitHub (pinned to c028a0c7dc)
Solutions
- Re-run the sample and enter a digit strictly between 1 and 4.
- If scripting the demo, clamp/validate the parsed integer before the switch and re-prompt on invalid input.
- If you extended the menu, add the new case branch before the default so valid new options do not throw.
Example fix
// before
default:
throw new InvalidOperationException("Invalid choice");
// after
int choice;
while (!int.TryParse(Console.ReadLine(), out choice) || choice < 1 || choice > 4)
{
Console.Write("Enter a number from 1 to 4: ");
}
switch (choice) { /* cases 1-4, no default throw */ } Defensive patterns
Strategy: validation
Validate before calling
int choice;
while (!int.TryParse(Console.ReadLine(), out choice) || choice < 1 || choice > 4)
{
Console.Write("Enter a number from 1 to 4: ");
}
// choice is now guaranteed valid; the default throw is unreachable Type guard
static bool IsValidMenuChoice(string input, out int value) =>
int.TryParse(input, out value) && value is >= 1 and <= 4; Prevention
- Bound-check parsed menu input before the switch.
- Re-prompt on invalid input instead of throwing.
- Add new switch arms whenever you extend AllowedValues.
When it happens
Trigger: The user types a number outside 1-4 (e.g. 0, 5, 9) or a non-integer string that was parsed to a value outside the supported range and reached the `default` branch of the switch at Program.cs:34.
Common situations: Running the interactive demo and mistyping the menu option, an automated script feeding an out-of-range index, or the menu being extended with new cases without updating valid input bounds.
Related errors
- Invalid kernel selection. {selectedKernelName} is not a vali
- Unsupported image format.
- Configuration is not setup correctly.
- Please provide valid MSGraph configuration in appsettings.De
- Please provide valid Ollama configuration in appsettings.Dev
AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13).
Data as JSON: /api/errors/17e746a705451fa9.
Report an issue: GitHub.