{"record":{"id":"4c96d8e0de886595","repo":"microsoft/autogen","slug":"invalid-role-4c96d8","errorCode":null,"errorMessage":"Invalid Role","messagePattern":"Invalid Role","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.Ollama/Middlewares/OllamaMessageConnector.cs","lineNumber":178,"sourceCode":"            return [MessageEnvelope.Create(message, agent.Name)];\n        }\n        else if (textMessage.From == agent.Name)\n        {\n            var message = new Message\n            {\n                Role = \"assistant\",\n                Value = textMessage.Content\n            };\n\n            return [MessageEnvelope.Create(message, agent.Name)];\n        }\n        else\n        {\n            var message = textMessage.From switch\n            {\n                null when textMessage.Role == Role.User => new Message { Role = \"user\", Value = textMessage.Content },\n                null when textMessage.Role == Role.Assistant => new Message { Role = \"assistant\", Value = textMessage.Content },\n                null => throw new InvalidOperationException(\"Invalid Role\"),\n                _ when textMessage.From != agent.Name => new Message { Role = \"user\", Value = textMessage.Content },\n                _ => throw new InvalidOperationException(\"The from field must be null or the agent name\"),\n            };\n\n            return [MessageEnvelope.Create(message, agent.Name)];\n        }\n    }\n}\n","sourceCodeStart":160,"sourceCodeEnd":187,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.Ollama/Middlewares/OllamaMessageConnector.cs#L160-L187","documentation":"Thrown by OllamaMessageConnector.ProcessTextMessage when a TextMessage has From == null but its Role is neither User nor Assistant. With a null sender the connector can only map User or Assistant roles onto Ollama's 'user'/'assistant' roles; any other role (e.g. System) with a null From is rejected.","triggerScenarios":"Creating new TextMessage(Role.System, ...) (or Role.Function, or a custom role) without setting the From field, then routing it through the Ollama agent's message connector.","commonSituations":"Porting code from the OpenAI connector where system messages are handled differently; constructing prompts by hand and forgetting that the connector treats null-From system messages as invalid.","solutions":["Pass system prompts via the agent's systemMessage constructor parameter or middleware instead of a TextMessage","If the message is meant for the agent, set From to the sender's name so it falls into the user-mapping branch","Ensure Role is exactly Role.User or Role.Assistant for anonymous (null-From) text messages"],"exampleFix":"// before\nvar sys = new TextMessage(Role.System, \"You are terse\"); // From is null\nawait agent.SendAsync(sys);\n\n// after\nvar agent = new OllamaAgent(..., systemMessage: \"You are terse\");\n// or: new TextMessage(Role.System, \"You are terse\", from: \"user\");","handlingStrategy":"validation","validationCode":"bool IsRoleValidForAnonymousText(TextMessage msg) =>\n    msg.From != null || msg.Role is Role.User or Role.Assistant;","typeGuard":null,"tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message == \"Invalid Role\")\n{\n    logger.LogWarning(\"Rejected anonymous text message with role {Role}\", msg.Role);\n}","preventionTips":["Deliver system prompts via agent construction, not messages","Default anonymous messages to Role.User","Unit-test connector mapping for every role you use"],"tags":["ollama","message-connector","role","validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}