microsoft/autogen · error · InvalidOperationException
Invalid return message type {message.GetType().Name}
Error message
Invalid return message type {message.GetType().Name} What it means
InvalidOperationException thrown by OpenAIChatRequestMessageConnector.PostProcessMessage (official OpenAI SDK package) when strictMode is true and the agent's return message is not an IMessage<ChatCompletion>. The strict connector only converts full ChatCompletion objects into AutoGen messages; any other return type aborts.
Source
Thrown at dotnet/src/AutoGen.OpenAI/Middleware/OpenAIChatRequestMessageConnector.cs:149
toolCalls.Add(new ToolCall(currentToolName, currentToolArguments) { ToolCallId = currentToolId });
}
if (toolCalls.Any())
{
yield return new ToolCallMessage(toolCalls, from: agent.Name)
{
Content = text,
};
}
}
public IMessage PostProcessMessage(IMessage message)
{
return message switch
{
IMessage<ChatCompletion> m => PostProcessChatCompletions(m),
_ when strictMode is false => message,
_ => throw new InvalidOperationException($"Invalid return message type {message.GetType().Name}"),
};
}
private IMessage PostProcessChatCompletions(IMessage<ChatCompletion> message)
{
// throw exception if prompt filter results is not null
if (message.Content.FinishReason == ChatFinishReason.ContentFilter)
{
throw new InvalidOperationException("The content is filtered because its potential risk. Please try another input.");
}
// throw exception is there is more than on choice
if (message.Content.Content.Count > 1)
{
throw new InvalidOperationException("The content has more than one choice. Please try another input.");
}
return PostProcessChatResponseMessage(message.Content, message.From);View on GitHub (pinned to 027ecf0a37)
Solutions
- Construct the connector with strictMode: false (default) for passthrough of unknown types
- Ensure the OpenAIChatAgent's raw ChatCompletion envelopes reach this connector unconverted
- Use the matching connector package (AutoGen.OpenAI connector for OpenAIChatAgent, .V1 connector for Azure OpenAI agent)
Example fix
// before var connector = new OpenAIChatRequestMessageConnector(strictMode: true); // middleware returns TextMessage -> throws // after var connector = new OpenAIChatRequestMessageConnector(); // strictMode: false
Defensive patterns
Strategy: type-guard
Validate before calling
bool convertible = msg is IMessage<ChatCompletion>;
if (!convertible && strictMode) { /* convert or bypass */ } Type guard
static bool IsOpenAICompletionEnvelope(IMessage m) => m is IMessage<ChatCompletion>;
Try / catch
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Invalid return message type")) { /* pass message through without the connector */ } Prevention
- Use AutoGen.OpenAI's connector with OpenAIChatAgent, not the V1 one
- Avoid double conversion by ordering middlewares correctly
- Default to strictMode: false in composed pipelines
When it happens
Trigger: An agent wrapped with OpenAIChatRequestMessageConnector(strictMode: true) returns something other than MessageEnvelope<ChatCompletion> — e.g. a pre-converted TextMessage from an earlier middleware or a different SDK's completion type.
Common situations: Middleware ordering issues where conversion happens twice; mixing AutoGen.OpenAI and AutoGen.OpenAI.V1 connectors in one pipeline; strict mode turned on for safety in composed agents.
Related errors
- Invalid return message type {message.GetType().Name}
- Invalid message type: {m.GetType().Name}
- Invalid streaming message type {reply.GetType().Name}
- Invalid message type
- Invalid message type: {m.GetType().Name}
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/58a08b9f742b2983.
Report an issue: GitHub.