{"record":{"id":"a03e28002a68e9ee","repo":"microsoft/autogen","slug":"unexpected-message-type-message-gettype","errorCode":null,"errorMessage":"Unexpected message type: {message?.GetType()}","messagePattern":"Unexpected message type: (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.Anthropic/Agent/AnthropicClientAgent.cs","lineNumber":98,"sourceCode":"    }\n\n    private List<ChatMessage> BuildMessages(IEnumerable<IMessage> messages)\n    {\n        List<ChatMessage> chatMessages = new();\n        foreach (IMessage? message in messages)\n        {\n            switch (message)\n            {\n                case IMessage<ChatMessage> chatMessage when chatMessage.Content.Role == \"system\":\n                    throw new InvalidOperationException(\n                        \"system message has already been set and only one system message is supported. \\\"system\\\" role for input messages in the Message\");\n\n                case IMessage<ChatMessage> chatMessage:\n                    chatMessages.Add(chatMessage.Content);\n                    break;\n\n                default:\n                    throw new ArgumentException($\"Unexpected message type: {message?.GetType()}\");\n            }\n        }\n\n        // merge messages with the same role\n        // fixing #2884\n        var mergedMessages = chatMessages.Aggregate(new List<ChatMessage>(), (acc, message) =>\n        {\n            if (acc.Count > 0 && acc.Last().Role == message.Role)\n            {\n                acc.Last().Content.AddRange(message.Content);\n            }\n            else\n            {\n                acc.Add(message);\n            }\n\n            return acc;\n        });","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.Anthropic/Agent/AnthropicClientAgent.cs#L80-L116","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar agent = new AnthropicClientAgent(client, name: \"assistant\", systemMessage: \"...\");\nawait agent.SendAsync(new TextMessage(Role.User, \"hello\", from: \"user\")); // ArgumentException: Unexpected message type\n\n// after\nvar agent = new AnthropicClientAgent(client, name: \"assistant\", systemMessage: \"...\")\n    .RegisterMessageConnector();\nawait agent.SendAsync(new TextMessage(Role.User, \"hello\", from: \"user\")); // connector translates to ChatMessage","handlingStrategy":"type-guard","validationCode":"var unsupported = messages.Where(m => m is not IMessage<ChatMessage>).ToList();\nif (unsupported.Count > 0)\n{\n    throw new InvalidOperationException($\"These messages need the connector (call RegisterMessageConnector()): {string.Join(\", \", unsupported.Select(m => m.GetType().Name))}\");\n}","typeGuard":"static bool AllAnthropicChatMessages(IEnumerable<IMessage> messages) =>\n    messages.All(m => m is IMessage<ChatMessage>);","tryCatchPattern":"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); }","preventionTips":["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."],"tags":["anthropic","message-type","message-connector","autogen","type-mismatch"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}