microsoft/autogen · error · ArgumentException
Unexpected message type: {message?.GetType()}
Error message
Unexpected message type: {message?.GetType()} What it means
AnthropicClientAgent.BuildMessages throws ArgumentException when an IMessage in the input is not IMessage<ChatMessage> — i.e. not a MessageEnvelope wrapping an Anthropic ChatMessage. The low-level AnthropicClientAgent speaks only its native ChatMessage envelope; AutoGen-native types like TextMessage, ImageMessage, or aggregate MessageEnvelope<string> fall into the default branch and are rejected.
Source
Thrown at dotnet/src/AutoGen.Anthropic/Agent/AnthropicClientAgent.cs:98
}
private List<ChatMessage> BuildMessages(IEnumerable<IMessage> messages)
{
List<ChatMessage> chatMessages = new();
foreach (IMessage? message in messages)
{
switch (message)
{
case IMessage<ChatMessage> chatMessage when chatMessage.Content.Role == "system":
throw new InvalidOperationException(
"system message has already been set and only one system message is supported. \"system\" role for input messages in the Message");
case IMessage<ChatMessage> chatMessage:
chatMessages.Add(chatMessage.Content);
break;
default:
throw new ArgumentException($"Unexpected message type: {message?.GetType()}");
}
}
// merge messages with the same role
// fixing #2884
var mergedMessages = chatMessages.Aggregate(new List<ChatMessage>(), (acc, message) =>
{
if (acc.Count > 0 && acc.Last().Role == message.Role)
{
acc.Last().Content.AddRange(message.Content);
}
else
{
acc.Add(message);
}
return acc;
});View on GitHub (pinned to 027ecf0a37)
Solutions
- Register the connector: new AnthropicClientAgent(...).RegisterMessageConnector() — the middleware converts AutoGen built-in message types to IMessage<ChatMessage> before they reach BuildMessages.
- Or send only MessageEnvelope<ChatMessage> payloads to the un-wrapped agent.
- Check you imported the right extension namespace (AutoGen.Anthropic.Extension) so RegisterMessageConnector resolves.
- If you use group chats, register the connector on the agent before adding it to the group.
Example fix
// before
var agent = new AnthropicClientAgent(client, name: "assistant", systemMessage: "...");
await agent.SendAsync(new TextMessage(Role.User, "hello", from: "user")); // ArgumentException: Unexpected message type
// after
var agent = new AnthropicClientAgent(client, name: "assistant", systemMessage: "...")
.RegisterMessageConnector();
await agent.SendAsync(new TextMessage(Role.User, "hello", from: "user")); // connector translates to ChatMessage Defensive patterns
Strategy: type-guard
Validate before calling
var unsupported = messages.Where(m => m is not IMessage<ChatMessage>).ToList();
if (unsupported.Count > 0)
{
throw new InvalidOperationException($"These messages need the connector (call RegisterMessageConnector()): {string.Join(", ", unsupported.Select(m => m.GetType().Name))}");
} Type guard
static bool AllAnthropicChatMessages(IEnumerable<IMessage> messages) =>
messages.All(m => m is IMessage<ChatMessage>); Try / catch
try { await agent.SendAsync(messages); } catch (ArgumentException ex) when (ex.Message.Contains("Unexpected message type")) { throw new InvalidOperationException($"Forgot RegisterMessageConnector() on the Anthropic agent? {ex.Message}", ex); } Prevention
- Always build Anthropic agents with .RegisterMessageConnector() before sending AutoGen-native messages (TextMessage, ToolCallMessage, ...).
- Wrap agent construction in one factory method so connector registration is never forgotten.
- Send MessageEnvelope<ChatMessage> only if you intentionally bypass the connector.
When it happens
Trigger: Calling AnthropicClientAgent.SendAsync with a TextMessage or MessageEnvelope<ToolCallMessage> without having called RegisterMessageConnector() on the agent; mixing middleware-processed and raw message types in one conversation; sending a Message[] array from a different provider integration.
Common situations: Using the raw agent instead of the wrapped one: forget .RegisterMessageConnector() which translates TextMessage/ToolCallMessage into IMessage<ChatMessage>; upgrading AutoGen where the connector extension moved namespaces; passing a DataMessage or WorkflowMessage to the client agent directly.
Related errors
- system message has already been set and only one system mess
- Failed to deserialize response
- Unknown content type
- Value was null.
- Unable to convert "{value}" to enum {typeToConvert}.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/a03e28002a68e9ee.
Report an issue: GitHub.