{"record":{"id":"7c0352359bce1812","repo":"microsoft/autogen","slug":"invalid-chatresponsemessage-7c0352","errorCode":null,"errorMessage":"Invalid ChatResponseMessage","messagePattern":"Invalid ChatResponseMessage","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.OpenAI/Middleware/OpenAIChatRequestMessageConnector.cs","lineNumber":205,"sourceCode":"        {\n            var toolCalls = chatCompletion.ToolCalls.Select(tc => new ToolCall(tc.FunctionName, tc.FunctionArguments.ToString()) { ToolCallId = tc.Id });\n            return new ToolCallMessage(toolCalls, from)\n            {\n                Content = textContent?.Kind switch\n                {\n                    _ when textContent?.Kind == ChatMessageContentPartKind.Text => textContent.Text,\n                    _ => null,\n                },\n            };\n        }\n\n        // if the content is text, return TextMessage\n        if (textContent?.Kind == ChatMessageContentPartKind.Text)\n        {\n            return new TextMessage(Role.Assistant, textContent.Text, from);\n        }\n\n        throw new InvalidOperationException(\"Invalid ChatResponseMessage\");\n    }\n\n    public IEnumerable<IMessage> ProcessIncomingMessages(IAgent agent, IEnumerable<IMessage> messages)\n    {\n        return messages.SelectMany<IMessage, IMessage>(m =>\n        {\n            if (m is IMessage<ChatMessage> crm)\n            {\n                return [crm];\n            }\n            else\n            {\n                var chatRequestMessages = m switch\n                {\n                    TextMessage textMessage => ProcessTextMessage(agent, textMessage),\n                    ImageMessage imageMessage when (imageMessage.From is null || imageMessage.From != agent.Name) => ProcessImageMessage(agent, imageMessage),\n                    MultiModalMessage multiModalMessage when (multiModalMessage.From is null || multiModalMessage.From != agent.Name) => ProcessMultiModalMessage(agent, multiModalMessage),\n                    ToolCallMessage toolCallMessage when (toolCallMessage.From is null || toolCallMessage.From == agent.Name) => ProcessToolCallMessage(agent, toolCallMessage),","sourceCodeStart":187,"sourceCodeEnd":223,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI/Middleware/OpenAIChatRequestMessageConnector.cs#L187-L223","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar reply = await agent.SendAsync(prompt); // throws for image-only reply\n\n// after\ntry\n{\n    var reply = await agent.SendAsync(prompt);\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"Invalid ChatResponseMessage\")\n{\n    var reply = await agent.SendAsync(prompt + \"\\nAnswer with text only.\");\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var reply = await agent.SendAsync(prompt); }\ncatch (InvalidOperationException ex) when (ex.Message == \"Invalid ChatResponseMessage\")\n{\n    // completion had no text part and no tool calls; ask again for text\n    return await agent.SendAsync(prompt + \"\\nRespond with plain text.\");\n}","preventionTips":["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."],"tags":["openai","chat-completions","content-type","autogen","csharp"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}