microsoft/autogen · error · ArgumentException
Invalid message type
Error message
Invalid message type
What it means
ArgumentException thrown by OpenAIChatAgent.CreateChatMessages: this agent only accepts messages that are IMessage<ChatMessage> envelopes (the official OpenAI SDK ChatMessage type). It performs no conversion of TextMessage or other AutoGen types itself — that is the job of the OpenAIChatRequestMessageConnector middleware.
Source
Thrown at dotnet/src/AutoGen.OpenAI/Agent/OpenAIChatAgent.cs:123
var settings = this.CreateChatCompletionsOptions(options);
var response = this.chatClient.CompleteChatStreamingAsync(chatHistory, settings, cancellationToken);
await foreach (var update in response.WithCancellation(cancellationToken))
{
if (update.ContentUpdate.Count > 1)
{
throw new InvalidOperationException("Only one choice is supported in streaming response");
}
yield return new MessageEnvelope<StreamingChatCompletionUpdate>(update, from: this.Name);
}
}
private IEnumerable<ChatMessage> CreateChatMessages(IEnumerable<IMessage> messages)
{
var oaiMessages = messages.Select(m => m switch
{
IMessage<ChatMessage> chatMessage => chatMessage.Content,
_ => throw new ArgumentException("Invalid message type")
});
// add system message if there's no system message in messages
if (!oaiMessages.Any(m => m is SystemChatMessage) && systemMessage is not null)
{
oaiMessages = new[] { new SystemChatMessage(systemMessage) }.Concat(oaiMessages);
}
return oaiMessages;
}
private ChatCompletionOptions CreateChatCompletionsOptions(GenerateReplyOptions? options)
{
var option = new ChatCompletionOptions()
{
Seed = this.options.Seed,
Temperature = options?.Temperature ?? this.options.Temperature,
MaxOutputTokenCount = options?.MaxToken ?? this.options.MaxOutputTokenCount,View on GitHub (pinned to 027ecf0a37)
Solutions
- Register the message connector: agent.RegisterMessageConnector() wraps the agent with OpenAIChatRequestMessageConnector which converts TextMessage et al. to ChatMessage
- Or convert messages yourself to MessageEnvelope.Create(new UserChatMessage("...")) before sending
- Verify no middleware is stripping ChatMessage envelopes back into plain messages
Example fix
// before
var agent = new OpenAIChatAgent(chatClient, name: "assistant");
await agent.SendAsync("hi"); // ArgumentException: Invalid message type
// after
var agent = new OpenAIChatAgent(chatClient, name: "assistant")
.RegisterMessageConnector();
await agent.SendAsync("hi"); Defensive patterns
Strategy: type-guard
Validate before calling
// ensure every message is a ChatMessage envelope before calling a bare OpenAIChatAgent bool ok = messages.All(m => m is IMessage<ChatMessage>);
Type guard
static bool IsChatMessageEnvelope(IMessage m) => m is IMessage<ChatMessage>;
Try / catch
catch (ArgumentException ex) when (ex.Message == "Invalid message type") { /* register message connector and retry */ } Prevention
- Always chain .RegisterMessageConnector() on OpenAIChatAgent
- Wrap agent construction in a factory that applies required middlewares
- Integration-test one send per agent configuration
When it happens
Trigger: Calling GenerateReplyAsync/GenerateStreamingReplyAsync on a bare OpenAIChatAgent with TextMessage, IMessage<string>, or any non-MessageEnvelope<ChatMessage> input.
Common situations: Using OpenAIChatAgent directly without calling RegisterMessageConnector(); migrating code from AutoGen.OpenAI.V1 where types differ; assuming the agent does its own message translation.
Related errors
- Invalid return message type {message.GetType().Name}
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
- Please set OPENAI_API_KEY environment variable.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/b959eae8c2adabf0.
Report an issue: GitHub.