microsoft/autogen · error · InvalidOperationException
Invalid mode
Error message
Invalid mode
What it means
Unreachable-in-practice defensive throw at the end of HumanInputMiddleware.InvokeAsync. The method handles HumanInputMode.NEVER, ALWAYS, and AUTO explicitly; the InvalidOperationException only fires if 'mode' holds an enum value outside those three (e.g. (HumanInputMode)99 cast from an int or from a bad config parse).
Source
Thrown at dotnet/src/AutoGen/Middleware/HumanInputMiddleware.cs:84
{
if (await isTermination(context.Messages, cancellationToken) is false)
{
return await agent.GenerateReplyAsync(context.Messages, context.Options, cancellationToken);
}
this.writeLine(prompt);
var input = getInput();
if (input == exitKeyword)
{
return new TextMessage(Role.Assistant, GroupChatExtension.TERMINATE, agent.Name);
}
input ??= string.Empty;
return new TextMessage(Role.Assistant, input, agent.Name);
}
throw new InvalidOperationException("Invalid mode");
}
private async Task<bool> DefaultIsTermination(IEnumerable<IMessage> messages, CancellationToken _)
{
return messages?.Last().IsGroupChatTerminateMessage() is true;
}
private string? GetInput()
{
return Console.ReadLine();
}
private void WriteLine(string message)
{
Console.WriteLine(message);
}
}
View on GitHub (pinned to 027ecf0a37)
Solutions
- Use only the defined HumanInputMode values: NEVER, ALWAYS, AUTO (AUTO is the default)
- Validate external input with Enum.IsDefined<HumanInputMode>(value) before constructing the middleware
- If a new enum member was added upstream, upgrade AutoGen so the middleware handles it
Example fix
// before var mode = (HumanInputMode)int.Parse(args[0]); // 7 -> invalid var mw = new HumanInputMiddleware(mode: mode); // after var raw = int.Parse(args[0]); var mode = Enum.IsDefined<HumanInputMode>(raw) ? (HumanInputMode)raw : HumanInputMode.AUTO; var mw = new HumanInputMiddleware(mode: mode);
Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined<HumanInputMode>(mode)) throw new ArgumentOutOfRangeException(nameof(mode)); var middleware = new HumanInputMiddleware(mode: mode);
Type guard
static bool IsValidMode(HumanInputMode mode) => mode is HumanInputMode.NEVER or HumanInputMode.ALWAYS or HumanInputMode.AUTO;
Prevention
- Never construct enum values via unchecked casts from external input; validate with Enum.IsDefined
- Parse mode strings with Enum.TryParse<HumanInputMode>(s, ignoreCase, out _) plus IsDefined
- Default to AUTO when input is unparsable instead of propagating a bad value
When it happens
Trigger: Constructing HumanInputMiddleware with a mode produced by an unchecked cast: (HumanInputMode)42, or parsing a numeric/string config value into the enum without Enum.IsDefined validation.
Common situations: Driving HumanInputMode from appsettings or CLI args where an out-of-range integer is accepted by the compiler/parser; interop code that passes raw ints across a boundary; refactoring that adds a new enum member not yet handled here.
Related errors
- ImageMessage must have Url or DataUri
- Unsupported content type {item.GetType()}
- Unsupported config type {llmConfig.GetType()}
- Agent name '{name}' is not a valid identifier.
- Handoff name '{name}' is not a valid identifier.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/a90d695670825893.
Report an issue: GitHub.