microsoft/autogen · error · InvalidOperationException
Invalid ChatResponseMessage
Error message
Invalid ChatResponseMessage
What it means
PostProcessChatResponseMessage throws 'Invalid ChatResponseMessage' when the completion has no tool calls and its first content part is not text (or Content is empty). The connector can only convert text replies and tool-call replies into AutoGen messages, so image/audio-only or empty completions fall through to this exception.
Source
Thrown at dotnet/src/AutoGen.OpenAI/Middleware/OpenAIChatRequestMessageConnector.cs:205
{
var toolCalls = chatCompletion.ToolCalls.Select(tc => new ToolCall(tc.FunctionName, tc.FunctionArguments.ToString()) { ToolCallId = tc.Id });
return new ToolCallMessage(toolCalls, from)
{
Content = textContent?.Kind switch
{
_ when textContent?.Kind == ChatMessageContentPartKind.Text => textContent.Text,
_ => null,
},
};
}
// if the content is text, return TextMessage
if (textContent?.Kind == ChatMessageContentPartKind.Text)
{
return new TextMessage(Role.Assistant, textContent.Text, from);
}
throw new InvalidOperationException("Invalid ChatResponseMessage");
}
public IEnumerable<IMessage> ProcessIncomingMessages(IAgent agent, IEnumerable<IMessage> messages)
{
return messages.SelectMany<IMessage, IMessage>(m =>
{
if (m is IMessage<ChatMessage> crm)
{
return [crm];
}
else
{
var chatRequestMessages = m switch
{
TextMessage textMessage => ProcessTextMessage(agent, textMessage),
ImageMessage imageMessage when (imageMessage.From is null || imageMessage.From != agent.Name) => ProcessImageMessage(agent, imageMessage),
MultiModalMessage multiModalMessage when (multiModalMessage.From is null || multiModalMessage.From != agent.Name) => ProcessMultiModalMessage(agent, multiModalMessage),
ToolCallMessage toolCallMessage when (toolCallMessage.From is null || toolCallMessage.From == agent.Name) => ProcessToolCallMessage(agent, toolCallMessage),View on GitHub (pinned to 027ecf0a37)
Solutions
- Ensure the model is asked for plain text output (no image/audio response formats) when using this connector.
- Check Content.Count and ToolCalls.Count on the raw completion before it reaches the connector to detect empty/non-text replies early.
- Retry the request — empty completions are occasionally transient.
- Handle the completion yourself without OpenAIChatRequestMessageConnector if you need non-text parts.
Example fix
// before
var reply = await agent.SendAsync(prompt); // throws for image-only reply
// after
try
{
var reply = await agent.SendAsync(prompt);
}
catch (InvalidOperationException ex) when (ex.Message == "Invalid ChatResponseMessage")
{
var reply = await agent.SendAsync(prompt + "\nAnswer with text only.");
} Defensive patterns
Strategy: try-catch
Try / catch
try { var reply = await agent.SendAsync(prompt); }
catch (InvalidOperationException ex) when (ex.Message == "Invalid ChatResponseMessage")
{
// completion had no text part and no tool calls; ask again for text
return await agent.SendAsync(prompt + "\nRespond with plain text.");
} Prevention
- Include 'answer in text' instructions in system prompts for vision/audio-capable models.
- Prefer tool-call flows when structured output is needed instead of relying on exotic content parts.
When it happens
Trigger: FinishReason is Stop (not ContentFilter), ToolCalls is empty, and textContent is null or its Kind is not ChatMessageContentPartKind.Text — e.g. a vision model returning only an image part, or an empty completion.
Common situations: Using image-generation/audiio output modes through the chat connector; a model returning an empty body under heavy load or truncation; requesting response_format that yields non-text parts.
Related errors
- The content has more than one choice. Please try another inp
- Invalid message type: {m.GetType().Name}
- Invalid Role
- The method or operation is not implemented.
- Unsupported content type
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/7c0352359bce1812.
Report an issue: GitHub.