{"record":{"id":"15bc87f941a91954","repo":"microsoft/autogen","slug":"invalid-message-type-15bc87","errorCode":null,"errorMessage":"Invalid message type","messagePattern":"Invalid message type","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.SemanticKernel/SemanticKernelChatCompletionAgent.cs","lineNumber":49,"sourceCode":"            .InvokeAsync(agentThread, cancellationToken: cancellationToken)\n            .ToArrayAsync(cancellationToken: cancellationToken);\n\n        return reply.Length > 1\n            ? throw new InvalidOperationException(\"ResultsPerPrompt greater than 1 is not supported in this semantic kernel agent\")\n            : new MessageEnvelope<ChatMessageContent>(reply[0], from: this.Name);\n    }\n\n    private ChatHistory BuildChatHistory(IEnumerable<IMessage> messages)\n    {\n        return new ChatHistory(ProcessMessage(messages));\n    }\n\n    private IEnumerable<ChatMessageContent> ProcessMessage(IEnumerable<IMessage> messages)\n    {\n        return messages.Select(m => m switch\n        {\n            IMessage<ChatMessageContent> cmc => cmc.Content,\n            _ => throw new ArgumentException(\"Invalid message type\")\n        });\n    }\n}\n","sourceCodeStart":31,"sourceCodeEnd":53,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.SemanticKernel/SemanticKernelChatCompletionAgent.cs#L31-L53","documentation":"SemanticKernelChatCompletionAgent.ProcessMessage only accepts IMessage<ChatMessageContent>. Any other IMessage implementation (TextMessage, ImageMessage, MessageEnvelope<T> with T != ChatMessageContent) throws ArgumentException 'Invalid message type' while building the ChatHistory.","triggerScenarios":"Passing TextMessage or any non-ChatMessageContent message to GenerateReplyAsync on the SemanticKernelChatCompletionAgent adapter.","commonSituations":"Feeding history produced by AutoGen core or OpenAI agents into a SemanticKernel-backed agent, or hand-assembling history with the convenient TextMessage constructors.","solutions":["Convert messages to MessageEnvelope<ChatMessageContent> before calling GenerateReplyAsync","Add the SemanticKernelChatMessageContentConnector middleware to the pipeline to translate TextMessage/ImageMessage automatically","Filter/transform the message list with a helper that maps each message to ChatMessageContent"],"exampleFix":"// before\nvar msgs = new[] { new TextMessage(Role.User, \"hi\") };\nawait adapter.GenerateReplyAsync(msgs);\n\n// after\nvar msgs = new[]\n{\n    new MessageEnvelope<ChatMessageContent>(new ChatMessageContent(AuthorRole.User, \"hi\"))\n};\nawait adapter.GenerateReplyAsync(msgs);","handlingStrategy":"type-guard","validationCode":"var unsupported = messages.Where(m => m is not IMessage<ChatMessageContent>).Select(m => m.GetType().Name).ToList();\nif (unsupported.Count > 0) throw new InvalidOperationException($\"Convert first: {string.Join(\",\", unsupported)}\");","typeGuard":"static bool IsChatMessageContentEnvelope(IMessage m) => m is IMessage<ChatMessageContent>;","tryCatchPattern":"try { await adapter.GenerateReplyAsync(messages); }\ncatch (ArgumentException ex) when (ex.Message == \"Invalid message type\")\n{\n    var converted = messages.Select(m => m switch\n    {\n        IMessage<ChatMessageContent> c => c,\n        _ => new MessageEnvelope<ChatMessageContent>(new ChatMessageContent(AuthorRole.User, m.GetContent() ?? \"\"))\n    });\n    await adapter.GenerateReplyAsync(converted);\n}","preventionTips":["Use SemanticKernelChatMessageContentConnector in the agent pipeline for automatic conversion","Never feed raw TextMessage lists to SK adapters","Centralize message conversion at the orchestrator boundary"],"tags":["csharp","semantic-kernel","message-type","argument"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}