{"record":{"id":"58a08b9f742b2983","repo":"microsoft/autogen","slug":"invalid-return-message-type-message-gettype-nam-58a08b","errorCode":null,"errorMessage":"Invalid return message type {message.GetType().Name}","messagePattern":"Invalid return message type (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.OpenAI/Middleware/OpenAIChatRequestMessageConnector.cs","lineNumber":149,"sourceCode":"            toolCalls.Add(new ToolCall(currentToolName, currentToolArguments) { ToolCallId = currentToolId });\n        }\n\n        if (toolCalls.Any())\n        {\n            yield return new ToolCallMessage(toolCalls, from: agent.Name)\n            {\n                Content = text,\n            };\n        }\n    }\n\n    public IMessage PostProcessMessage(IMessage message)\n    {\n        return message switch\n        {\n            IMessage<ChatCompletion> m => PostProcessChatCompletions(m),\n            _ when strictMode is false => message,\n            _ => throw new InvalidOperationException($\"Invalid return message type {message.GetType().Name}\"),\n        };\n    }\n\n    private IMessage PostProcessChatCompletions(IMessage<ChatCompletion> message)\n    {\n        // throw exception if prompt filter results is not null\n        if (message.Content.FinishReason == ChatFinishReason.ContentFilter)\n        {\n            throw new InvalidOperationException(\"The content is filtered because its potential risk. Please try another input.\");\n        }\n\n        // throw exception is there is more than on choice\n        if (message.Content.Content.Count > 1)\n        {\n            throw new InvalidOperationException(\"The content has more than one choice. Please try another input.\");\n        }\n\n        return PostProcessChatResponseMessage(message.Content, message.From);","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI/Middleware/OpenAIChatRequestMessageConnector.cs#L131-L167","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)"],"exampleFix":"// before\nvar connector = new OpenAIChatRequestMessageConnector(strictMode: true);\n// middleware returns TextMessage -> throws\n\n// after\nvar connector = new OpenAIChatRequestMessageConnector(); // strictMode: false","handlingStrategy":"type-guard","validationCode":"bool convertible = msg is IMessage<ChatCompletion>;\nif (!convertible && strictMode) { /* convert or bypass */ }","typeGuard":"static bool IsOpenAICompletionEnvelope(IMessage m) => m is IMessage<ChatCompletion>;","tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Invalid return message type\")) { /* pass message through without the connector */ }","preventionTips":["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"],"tags":["autogen","dotnet","openai","middleware","strict-mode","message-connector"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}