microsoft/autogen · error · InvalidOperationException
Invalid ChatResponseMessage
Error message
Invalid ChatResponseMessage
What it means
Thrown by PostProcessChatResponseMessage when a ChatResponseMessage carries no function call, no tool calls, and null/empty text content. The connector has nothing to map into a TextMessage or ToolCallMessage, so it treats the response as malformed. Typical causes are content filtering (empty completion), reasoning models that return content in a different field, or SDK version drift where content moved.
Source
Thrown at dotnet/src/AutoGen.OpenAI.V1/Middleware/OpenAIChatRequestMessageConnector.cs:167
{
var functionToolCalls = chatResponseMessage.ToolCalls
.Where(tc => tc is ChatCompletionsFunctionToolCall)
.Select(tc => (ChatCompletionsFunctionToolCall)tc);
var toolCalls = functionToolCalls.Select(tc => new ToolCall(tc.Name, tc.Arguments) { ToolCallId = tc.Id });
return new ToolCallMessage(toolCalls, from)
{
Content = textContent,
};
}
if (textContent is string content && !string.IsNullOrEmpty(content))
{
return new TextMessage(Role.Assistant, content, 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<ChatRequestMessage> 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
- Inspect message.Content.Choices[0].FinishReason to see why content is empty (content_filter, length, etc.) and address that first
- Increase TokenChoiceCount/max tokens if truncation produced an empty completion
- Verify the Azure.OpenAI package version matches the one AutoGen.OpenAI.V1 was compiled against
- Catch InvalidOperationException and retry or substitute a default assistant message
Example fix
// before
var reply = await agent.SendAsync(prompt); // throws 'Invalid ChatResponseMessage'
// after
// check finish reason on the raw completion before the connector maps it
if (completion.Choices[0].FinishReason == CompletionsFinishReason.ContentFiltered)
{
// handle filtered response explicitly
} Defensive patterns
Strategy: validation
Validate before calling
var choice = completion.Choices[0];
bool hasContent = !string.IsNullOrEmpty(choice.Message.Content)
|| choice.Message.FunctionCall is not null
|| choice.Message.ToolCalls.Count > 0;
if (!hasContent) { /* inspect FinishReason before the connector throws */ } Try / catch
catch (InvalidOperationException ex) when (ex.Message == "Invalid ChatResponseMessage") { /* retry or log finish reason */ } Prevention
- Check FinishReason on raw completions in tests
- Keep Azure.OpenAI SDK version aligned with the AutoGen package
- Avoid max_tokens values that truncate completions to empty
When it happens
Trigger: A ChatCompletions response whose Message has empty Content and no FunctionCall/ToolCalls — e.g. finish_reason content_filter with empty body, or an assistant reply that only contains tool-call outputs not exposed on this SDK version.
Common situations: Deployments where content filter removes the text; models that return empty content on truncation (max_tokens hit); mismatch between the Azure.OpenAI SDK version the connector was built against and the API's actual response shape.
Related errors
- Invalid return message type {message.GetType().Name}
- Invalid message type: {m.GetType().Name}
- Please set AZURE_OPENAI_API_KEY environment variable.
- Please set AZURE_OPENAI_ENDPOINT environment variable.
- Please set environment variable AZURE_OPENAI_API_KEY
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/46e70e5cd3e521ba.
Report an issue: GitHub.