{"record":{"id":"64bf64726b2478f3","repo":"microsoft/autogen","slug":"invalid-message-type-64bf64","errorCode":null,"errorMessage":"Invalid message type","messagePattern":"Invalid message type","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.OpenAI.V1/Agent/OpenAIChatAgent.cs","lineNumber":129,"sourceCode":"        var settings = this.CreateChatCompletionsOptions(options, messages);\n        var response = await this.openAIClient.GetChatCompletionsStreamingAsync(settings, cancellationToken);\n        await foreach (var update in response.WithCancellation(cancellationToken))\n        {\n            if (update.ChoiceIndex > 0)\n            {\n                throw new InvalidOperationException(\"Only one choice is supported in streaming response\");\n            }\n\n            yield return new MessageEnvelope<StreamingChatCompletionsUpdate>(update, from: this.Name);\n        }\n    }\n\n    private ChatCompletionsOptions CreateChatCompletionsOptions(GenerateReplyOptions? options, IEnumerable<IMessage> messages)\n    {\n        var oaiMessages = messages.Select(m => m switch\n        {\n            IMessage<ChatRequestMessage> chatRequestMessage => chatRequestMessage.Content,\n            _ => throw new ArgumentException(\"Invalid message type\")\n        });\n\n        // add system message if there's no system message in messages\n        if (!oaiMessages.Any(m => m is ChatRequestSystemMessage))\n        {\n            oaiMessages = new[] { new ChatRequestSystemMessage(systemMessage) }.Concat(oaiMessages);\n        }\n\n        // clone the options by serializing and deserializing\n        var json = JsonSerializer.Serialize(this.options);\n        var settings = JsonSerializer.Deserialize<ChatCompletionsOptions>(json) ?? throw new InvalidOperationException(\"Failed to clone options\");\n\n        foreach (var m in oaiMessages)\n        {\n            settings.Messages.Add(m);\n        }\n\n        settings.Temperature = options?.Temperature ?? settings.Temperature;","sourceCodeStart":111,"sourceCodeEnd":147,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI.V1/Agent/OpenAIChatAgent.cs#L111-L147","documentation":"CreateChatCompletionsOptions maps each incoming IMessage to a ChatRequestMessage and throws ArgumentException when a message is not IMessage<ChatRequestMessage>. OpenAIChatAgent's core path expects messages already converted to OpenAI SDK request types (normally done by the RegisterMessageConnector middleware).","triggerScenarios":"Calling GenerateReplyAsync/GenerateStreamingReplyAsync on a bare OpenAIChatAgent (without .RegisterMessageConnector()) with TextMessage, ImageMessage, or any AutoGen primitive message type.","commonSituations":"Using the low-level agent directly instead of the extension-method chain; forgetting that GPTAgent and the documentation's agent setup attach the connector implicitly; custom pipelines that skip middleware registration.","solutions":["Register the connector: agent = new OpenAIChatAgent(...).RegisterMessageConnector();","Or pre-convert messages to MessageEnvelope<ChatRequestMessage> before sending","Prefer the higher-level GPTAgent or the documented builder that wires the connector for you"],"exampleFix":"// before\nvar agent = new OpenAIChatAgent(client, \"gpt\", options);\nawait agent.SendAsync(new TextMessage(Role.User, \"hi\")); // throws\n\n// after\nvar agent = new OpenAIChatAgent(client, \"gpt\", options)\n    .RegisterMessageConnector();\nawait agent.SendAsync(new TextMessage(Role.User, \"hi\"));","handlingStrategy":"validation","validationCode":"bool allConvertible = messages.All(m => m is IMessage<ChatRequestMessage>);\nif (!allConvertible) agent = agent.RegisterMessageConnector();","typeGuard":"static bool IsChatRequestEnvelope(IMessage m) => m is IMessage<ChatRequestMessage>;","tryCatchPattern":"catch (ArgumentException ex) when (ex.Message == \"Invalid message type\")\n{\n    var wired = agent.RegisterMessageConnector();\n    return await wired.SendAsync(messages);\n}","preventionTips":["Always chain .RegisterMessageConnector() on OpenAIChatAgent","Prefer GPTAgent which wires the connector","Standardize on one agent construction helper across the codebase"],"tags":["openai","message-connector","middleware","message-type"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}