{"record":{"id":"a4d38811d7f7a785","repo":"microsoft/autogen","slug":"invalid-return-message-type-message-gettype-nam-a4d388","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.V1/Middleware/OpenAIChatRequestMessageConnector.cs","lineNumber":97,"sourceCode":"                {\n                    throw new InvalidOperationException($\"Invalid streaming message type {reply.GetType().Name}\");\n                }\n                else\n                {\n                    yield return reply;\n                }\n            }\n        }\n    }\n\n    public IMessage PostProcessMessage(IMessage message)\n    {\n        return message switch\n        {\n            IMessage<ChatResponseMessage> m => PostProcessChatResponseMessage(m.Content, m.From),\n            IMessage<ChatCompletions> m => PostProcessChatCompletions(m),\n            _ when strictMode is false => message,\n            _ => throw new InvalidOperationException($\"Invalid return message type {message.GetType().Name}\"),\n        };\n    }\n\n    public IMessage? PostProcessStreamingMessage(IMessage<StreamingChatCompletionsUpdate> update, string? currentToolName)\n    {\n        if (update.Content.ContentUpdate is string contentUpdate)\n        {\n            // text message\n            return new TextMessageUpdate(Role.Assistant, contentUpdate, from: update.From);\n        }\n        else if (update.Content.FunctionName is string functionName)\n        {\n            return new ToolCallMessageUpdate(functionName, string.Empty, from: update.From);\n        }\n        else if (update.Content.FunctionArgumentsUpdate is string functionArgumentsUpdate && currentToolName is string)\n        {\n            return new ToolCallMessageUpdate(currentToolName, functionArgumentsUpdate, from: update.From);\n        }","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI.V1/Middleware/OpenAIChatRequestMessageConnector.cs#L79-L115","documentation":"Thrown by OpenAIChatRequestMessageConnector.PostProcessMessage when the middleware is running in strict mode and an agent's return message is neither an IMessage<ChatResponseMessage> nor an IMessage<ChatCompletions> envelope. The connector's job is to convert Azure OpenAI SDK response objects into AutoGen messages (TextMessage, ToolCallMessage); in strict mode it refuses to pass through any unrecognized message type. This almost always means the reply was produced by a non-OpenAI agent or an unconverted message flowed into this middleware.","triggerScenarios":"Constructing OpenAIChatRequestMessageConnector(strictMode: true) and letting it post-process a message that is not a MessageEnvelope<ChatResponseMessage> or MessageEnvelope<ChatCompletions> (e.g. a plain TextMessage returned by a custom/inner middleware, or a message from a different provider's agent).","commonSituations":"Chaining middlewares where a later middleware emits a plain TextMessage; mixing agents from AutoGen.OpenAI.V1 with agents from AutoGen.OpenAI or AutoGen.SemanticKernel in strict pipelines; upgrading samples that assumed lenient passthrough.","solutions":["Construct the connector with strictMode: false (the default) so unknown message types pass through unchanged","Verify the agent wrapped by the connector actually returns Azure.OpenAI ChatResponseMessage/ChatCompletions envelopes","If you need strictness, add a converting middleware before this one that maps foreign messages to IMessage<ChatCompletions>","Check the actual runtime type of the failing message (message.GetType().Name in the exception) to find which middleware produced it"],"exampleFix":"// before\nvar connector = new OpenAIChatRequestMessageConnector(strictMode: true);\n// agent returns plain TextMessage -> InvalidOperationException\n\n// after\nvar connector = new OpenAIChatRequestMessageConnector(); // strictMode: false, unknown types pass through","handlingStrategy":"type-guard","validationCode":"// verify before post-processing\nbool isConvertible = msg is IMessage<ChatResponseMessage> || msg is IMessage<ChatCompletions>;\nif (!isConvertible && connectorStrict) { /* convert or skip */ }","typeGuard":"static bool IsOpenAIV1Response(IMessage m) => m is IMessage<ChatResponseMessage> || m is IMessage<ChatCompletions>;","tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Invalid return message type\")) { /* route message outside the connector */ }","preventionTips":["Keep connector strictMode off unless you control every middleware in the pipeline","Use the connector from the package matching your agent (V1 vs OpenAI)","Add integration tests that run the full middleware chain"],"tags":["autogen","dotnet","azure-openai","middleware","message-connector","strict-mode"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}