{"record":{"id":"799f2a16469c133f","repo":"microsoft/autogen","slug":"invalid-message-type-799f2a","errorCode":null,"errorMessage":"Invalid message type","messagePattern":"Invalid message type","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.SemanticKernel/SemanticKernelAgent.cs","lineNumber":134,"sourceCode":"            MaxTokens = options?.MaxToken ?? 1024,\n            StopSequences = options?.StopSequence,\n            ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions,\n        };\n    }\n\n    private IChatCompletionService GetChatCompletionService()\n    {\n        return string.IsNullOrEmpty(_modelServiceId)\n            ? _kernel.GetRequiredService<IChatCompletionService>()\n            : _kernel.GetRequiredService<IChatCompletionService>(_modelServiceId);\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":116,"sourceCodeEnd":138,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.SemanticKernel/SemanticKernelAgent.cs#L116-L138","documentation":"SemanticKernelAgent.BuildChatHistory calls ProcessMessage, which pattern-matches each IMessage against IMessage<ChatMessageContent> only. Anything else — TextMessage, ImageMessage, MessageEnvelope of another type, plain Message — throws ArgumentException with 'Invalid message type'.","triggerScenarios":"Passing TextMessage/IMessage<TextContent>, ImageMessage, or any non-ChatMessageContent envelope to SemanticKernelAgent.GenerateReplyAsync/GenerateStreamingReplyAsync.","commonSituations":"Mixing messages produced by AutoGen core agents (which emit TextMessage) with a SemanticKernel agent, or building chat history manually with the convenience TextMessage type instead of wrapping ChatMessageContent.","solutions":["Wrap every message in MessageEnvelope<ChatMessageContent>, e.g. new MessageEnvelope<ChatMessageContent>(new ChatMessageContent(AuthorRole.User, text))","Use the provided SemanticKernelChatMessageContentConnector middleware, which converts TextMessage and other common types into ChatMessageContent before the agent sees them","Check each message with 'is IMessage<ChatMessageContent>' before sending and convert the ones that fail"],"exampleFix":"// before\nvar messages = new List<IMessage> { new TextMessage(Role.User, \"hello\") };\nawait skAgent.GenerateReplyAsync(messages);\n\n// after\nvar messages = new List<IMessage>\n{\n    new MessageEnvelope<ChatMessageContent>(new ChatMessageContent(AuthorRole.User, \"hello\"))\n};\nawait skAgent.GenerateReplyAsync(messages);","handlingStrategy":"type-guard","validationCode":"var invalid = messages.Where(m => m is not IMessage<ChatMessageContent>).ToList();\nif (invalid.Count > 0)\n    throw new InvalidOperationException($\"Non-ChatMessageContent messages: {string.Join(\",\", invalid.Select(m => m.GetType().Name))}\");","typeGuard":"static IMessage<ChatMessageContent>? AsChatMessageContent(IMessage m) => m as IMessage<ChatMessageContent>;\n\n// usage: messages.Select(AsChatMessageContent).Where(m => m is not null)","tryCatchPattern":"try { await skAgent.GenerateReplyAsync(messages); }\ncatch (ArgumentException ex) when (ex.Message == \"Invalid message type\")\n{\n    var converted = messages.Select(m => new MessageEnvelope<ChatMessageContent>(\n        new ChatMessageContent(AuthorRole.User, m.GetContent()?.ToString() ?? string.Empty)));\n    await skAgent.GenerateReplyAsync(converted); // fallback conversion retry\n}","preventionTips":["Adopt SemanticKernelChatMessageContentConnector middleware so TextMessage converts automatically","Build SK agent history exclusively with MessageEnvelope<ChatMessageContent>","Write a helper ToChatMessageContent(IMessage) and route every message through it"],"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"}