microsoft/autogen · error · InvalidOperationException
Invalid message type: {m.GetType().Name}
Error message
Invalid message type: {m.GetType().Name} What it means
Thrown by ProcessIncomingMessages when strictMode is true and an incoming IMessage is not one of the supported types (TextMessage, ImageMessage, MultiModalMessage, ToolCallMessage, ToolCallResultMessage, AggregateMessage<ToolCallMessage,ToolCallResultMessage>, legacy Message, or ChatRequestMessage envelopes). In strict mode the connector must translate every message into Azure OpenAI ChatRequestMessage form; anything unrecognized aborts the batch.
Source
Thrown at dotnet/src/AutoGen.OpenAI.V1/Middleware/OpenAIChatRequestMessageConnector.cs:192
if (m is IMessage<ChatRequestMessage> crm)
{
return [crm];
}
else
{
var chatRequestMessages = m switch
{
TextMessage textMessage => ProcessTextMessage(agent, textMessage),
ImageMessage imageMessage when (imageMessage.From is null || imageMessage.From != agent.Name) => ProcessImageMessage(agent, imageMessage),
MultiModalMessage multiModalMessage when (multiModalMessage.From is null || multiModalMessage.From != agent.Name) => ProcessMultiModalMessage(agent, multiModalMessage),
ToolCallMessage toolCallMessage when (toolCallMessage.From is null || toolCallMessage.From == agent.Name) => ProcessToolCallMessage(agent, toolCallMessage),
ToolCallResultMessage toolCallResultMessage => ProcessToolCallResultMessage(toolCallResultMessage),
AggregateMessage<ToolCallMessage, ToolCallResultMessage> aggregateMessage => ProcessFunctionCallMiddlewareMessage(agent, aggregateMessage),
#pragma warning disable CS0618 // deprecated
Message msg => ProcessMessage(agent, msg),
#pragma warning restore CS0618 // deprecated
_ when strictMode is false => [],
_ => throw new InvalidOperationException($"Invalid message type: {m.GetType().Name}"),
};
if (chatRequestMessages.Any())
{
return chatRequestMessages.Select(cm => MessageEnvelope.Create(cm, m.From));
}
else
{
return [m];
}
}
});
}
[Obsolete("This method is deprecated, please use ProcessIncomingMessages(IAgent agent, IEnumerable<IMessage> messages) instead.")]
private IEnumerable<ChatRequestMessage> ProcessIncomingMessagesForSelf(Message message)
{
if (message.Role == Role.System)View on GitHub (pinned to 027ecf0a37)
Solutions
- Construct the connector with strictMode: false so unsupported messages are forwarded unchanged
- Convert foreign messages to TextMessage/MessageEnvelope<ChatRequestMessage> before they reach this agent
- If the message is from another agent, ensure it is an aggregate or tool-call/result type the connector understands
- Log m.GetType().Name at the call site to identify the offending producer
Example fix
// before var connector = new OpenAIChatRequestMessageConnector(strictMode: true); await agent.SendAsync(new MessageEnvelope<MyPayload>(new MyPayload())); // throws // after var connector = new OpenAIChatRequestMessageConnector(); // lenient passthrough // or wrap the payload: new TextMessage(Role.User, Serialize(myPayload))
Defensive patterns
Strategy: type-guard
Validate before calling
var supported = msg is TextMessage or ImageMessage or MultiModalMessage or ToolCallMessage or ToolCallResultMessage or AggregateMessage<ToolCallMessage, ToolCallResultMessage> or IMessage<ChatRequestMessage>;
if (!supported && strictMode) { /* convert to TextMessage first */ } Type guard
static bool IsSupportedByV1Connector(IMessage m) => m is TextMessage or ImageMessage or MultiModalMessage or ToolCallMessage or ToolCallResultMessage or AggregateMessage<ToolCallMessage, ToolCallResultMessage> or IMessage<ChatRequestMessage>;
Try / catch
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Invalid message type")) { /* convert or drop the offending message */ } Prevention
- Standardize on AutoGen core message types across agents
- Keep strictMode off for heterogeneous group chats
- Unit-test message routing with every message type your app produces
When it happens
Trigger: Sending a message type like MessageEnvelope<someCustomPayload>, Microsoft.AutoGen messages, or another provider's message class into an agent pipeline containing this connector built with strictMode: true.
Common situations: Heterogeneous group chats where agents from different AutoGen packages share one conversation; custom user-defined IMessage implementations; strict-mode enabled to catch message-flow bugs but foreign messages legitimately flow through.
Related errors
- Invalid return message type {message.GetType().Name}
- Invalid return message type {message.GetType().Name}
- Invalid ChatResponseMessage
- Invalid streaming message type {reply.GetType().Name}
- No user message found.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/5c2c02a9c8f7fc07.
Report an issue: GitHub.