{"record":{"id":"9cffa32bef1712e9","repo":"microsoft/autogen","slug":"invalid-role-9cffa3","errorCode":null,"errorMessage":"Invalid Role","messagePattern":"Invalid Role","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.OpenAI/Middleware/OpenAIChatRequestMessageConnector.cs","lineNumber":259,"sourceCode":"\n    private IEnumerable<ChatMessage> ProcessTextMessage(IAgent agent, TextMessage message)\n    {\n        if (message.Role == Role.System)\n        {\n            return [new SystemChatMessage(message.Content) { ParticipantName = message.From }];\n        }\n\n        if (agent.Name == message.From)\n        {\n            return [new AssistantChatMessage(message.Content) { ParticipantName = agent.Name }];\n        }\n        else\n        {\n            return message.From switch\n            {\n                null when message.Role == Role.User => [new UserChatMessage(message.Content)],\n                null when message.Role == Role.Assistant => [new AssistantChatMessage(message.Content)],\n                null => throw new InvalidOperationException(\"Invalid Role\"),\n                _ => [new UserChatMessage(message.Content) { ParticipantName = message.From }]\n            };\n        }\n    }\n\n    private IEnumerable<ChatMessage> ProcessImageMessage(IAgent agent, ImageMessage message)\n    {\n        if (agent.Name == message.From)\n        {\n            // image message from assistant is not supported\n            throw new ArgumentException(\"ImageMessage is not supported when message.From is the same with agent\");\n        }\n\n        var imageContentItem = this.CreateChatMessageImageContentItemFromImageMessage(message);\n        return [new UserChatMessage([imageContentItem]) { ParticipantName = message.From }];\n    }\n\n    private IEnumerable<ChatMessage> ProcessMultiModalMessage(IAgent agent, MultiModalMessage message)","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI/Middleware/OpenAIChatRequestMessageConnector.cs#L241-L277","documentation":"ProcessTextMessage maps a TextMessage with From == null to a UserChatMessage or AssistantChatMessage based on Role. Only Role.User and Role.Assistant are handled; any other role (typically Role.System, or a custom/undefined role) with a null sender throws 'Invalid Role'.","triggerScenarios":"Sending new TextMessage(Role.System, ...) (or Role.Custom) without setting the From field to this agent's conversation partner.","commonSituations":"Developers moving a system prompt from agent construction into the message history; porting code from the legacy Message type where Role.System with null From was accepted; typos in role selection.","solutions":["Set From on the message (any non-null value) so it maps to UserChatMessage with ParticipantName.","Use Role.User or Role.Assistant for sender-less messages instead of Role.System.","Deliver system prompts via the agent's system instructions / SystemChatMessage at construction, not as a runtime TextMessage."],"exampleFix":"// before\nvar msg = new TextMessage(Role.System, \"You are a helpful pirate\"); // From is null\n\n// after\nvar msg = new TextMessage(Role.System, \"You are a helpful pirate\", from: \"system\");","handlingStrategy":"validation","validationCode":"static TextMessage CreateSafeText(Role role, string content, string? from = null)\n{\n    if (from is null && role is not (Role.User or Role.Assistant))\n        throw new ArgumentException($\"TextMessage with role {role} must set From.\");\n    return new TextMessage(role, content, from);\n}","typeGuard":null,"tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message == \"Invalid Role\")\n{\n    // re-issue with explicit sender\n    return new TextMessage(Role.User, content, from: \"system\");\n}","preventionTips":["Set From on every history message you build (system prompts included).","Deliver system prompts through the agent's system-message construction, not runtime TextMessages."],"tags":["openai","message-connector","role-mapping","autogen","csharp"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}