{"record":{"id":"53d881b9925c0f9f","repo":"microsoft/autogen","slug":"invalid-message-type-m-gettype-name-53d881","errorCode":null,"errorMessage":"Invalid message type: {m.GetType().Name}","messagePattern":"Invalid message type: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.OpenAI/Middleware/OpenAIChatRequestMessageConnector.cs","lineNumber":227,"sourceCode":"    {\n        return messages.SelectMany<IMessage, IMessage>(m =>\n        {\n            if (m is IMessage<ChatMessage> crm)\n            {\n                return [crm];\n            }\n            else\n            {\n                var chatRequestMessages = m switch\n                {\n                    TextMessage textMessage => ProcessTextMessage(agent, textMessage),\n                    ImageMessage imageMessage when (imageMessage.From is null || imageMessage.From != agent.Name) => ProcessImageMessage(agent, imageMessage),\n                    MultiModalMessage multiModalMessage when (multiModalMessage.From is null || multiModalMessage.From != agent.Name) => ProcessMultiModalMessage(agent, multiModalMessage),\n                    ToolCallMessage toolCallMessage when (toolCallMessage.From is null || toolCallMessage.From == agent.Name) => ProcessToolCallMessage(agent, toolCallMessage),\n                    ToolCallResultMessage toolCallResultMessage => ProcessToolCallResultMessage(toolCallResultMessage),\n                    AggregateMessage<ToolCallMessage, ToolCallResultMessage> aggregateMessage => ProcessFunctionCallMiddlewareMessage(agent, aggregateMessage),\n                    _ when strictMode is false => [],\n                    _ => throw new InvalidOperationException($\"Invalid message type: {m.GetType().Name}\"),\n                };\n\n                if (chatRequestMessages.Any())\n                {\n                    return chatRequestMessages.Select(cm => MessageEnvelope.Create(cm, m.From));\n                }\n                else\n                {\n                    return [m];\n                }\n            }\n        });\n    }\n\n    private IEnumerable<ChatMessage> ProcessTextMessage(IAgent agent, TextMessage message)\n    {\n        if (message.Role == Role.System)\n        {","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI/Middleware/OpenAIChatRequestMessageConnector.cs#L209-L245","documentation":"ProcessIncomingMessages maps each incoming IMessage to OpenAI ChatMessages. When strictMode is true and the message matches none of the supported patterns (TextMessage, ImageMessage from others, MultiModalMessage from others, ToolCallMessage from self, ToolCallResultMessage, AggregateMessage), it throws this exception. In non-strict mode unknown messages are silently skipped.","triggerScenarios":"Constructing the connector with strictMode: true and feeding it a message type it does not handle — e.g. an ImageMessage whose From equals the agent name (its guard clause excludes it from the ImageMessage arm), or a custom IMessage implementation.","commonSituations":"Group-chat orchestration where an agent receives another agent's ImageMessage; custom message classes; enabling strict mode during hardening and discovering previously-dropped messages.","solutions":["Pass strictMode: false when creating OpenAIChatRequestMessageConnector so unsupported messages are ignored instead of throwing.","Convert or filter messages before sending: ensure ImageMessage/MultiModalMessage come from a sender different from the agent, and only send supported IMessage types.","For custom message types, wrap them in MessageEnvelope<ChatMessage> (IMessage<ChatMessage>) which passes through untouched.","Route ToolCallMessage only to the agent that produced it (message.From must equal agent.Name)."],"exampleFix":"// before\nvar connector = new OpenAIChatRequestMessageConnector(strictMode: true);\nagent.SendAsync(new ImageMessage(Role.Assistant, url, from: agent.Name)); // unsupported in strict mode\n\n// after\nvar connector = new OpenAIChatRequestMessageConnector(strictMode: false);\n// or set the sender correctly:\nagent.SendAsync(new ImageMessage(Role.User, url, from: \"user-1\"));","handlingStrategy":"validation","validationCode":"// Reject unsupported messages before strict-mode dispatch\nstatic bool IsSupportedByOpenAIConnector(IMessage m, string agentName) => m switch\n{\n    TextMessage => true,\n    ImageMessage img => img.From != agentName,\n    MultiModalMessage mm => mm.From != agentName,\n    ToolCallMessage tc => tc.From is null || tc.From == agentName,\n    ToolCallResultMessage => true,\n    AggregateMessage<ToolCallMessage, ToolCallResultMessage> => true,\n    IMessage<ChatMessage> => true,\n    _ => false,\n};","typeGuard":"static bool IsSupportedByOpenAIConnector(IMessage m, string agentName) => m switch\n{\n    TextMessage => true,\n    ImageMessage img => img.From != agentName,\n    MultiModalMessage mm => mm.From != agentName,\n    ToolCallMessage tc => tc.From is null || tc.From == agentName,\n    ToolCallResultMessage => true,\n    AggregateMessage<ToolCallMessage, ToolCallResultMessage> => true,\n    IMessage<ChatMessage> => true,\n    _ => false,\n};","tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Invalid message type\"))\n{\n    logger.LogError(\"Unsupported message {Type}; enable non-strict mode or convert it\", offendingType);\n    throw;\n}","preventionTips":["Default to strictMode: false unless you explicitly want fail-fast behavior.","Centralize message construction so only connector-supported types enter agent calls.","Wrap custom payloads in MessageEnvelope.Create(chatMessage) which the connector passes through."],"tags":["openai","message-connector","strict-mode","autogen","csharp"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}