microsoft/autogen · error · InvalidOperationException

Invalid Role

Error message

Invalid Role

What it means

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.

Source

Thrown at dotnet/src/AutoGen.Ollama/Middlewares/OllamaMessageConnector.cs:178

            return [MessageEnvelope.Create(message, agent.Name)];
        }
        else if (textMessage.From == agent.Name)
        {
            var message = new Message
            {
                Role = "assistant",
                Value = textMessage.Content
            };

            return [MessageEnvelope.Create(message, agent.Name)];
        }
        else
        {
            var message = textMessage.From switch
            {
                null when textMessage.Role == Role.User => new Message { Role = "user", Value = textMessage.Content },
                null when textMessage.Role == Role.Assistant => new Message { Role = "assistant", Value = textMessage.Content },
                null => throw new InvalidOperationException("Invalid Role"),
                _ when textMessage.From != agent.Name => new Message { Role = "user", Value = textMessage.Content },
                _ => throw new InvalidOperationException("The from field must be null or the agent name"),
            };

            return [MessageEnvelope.Create(message, agent.Name)];
        }
    }
}

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Pass system prompts via the agent's systemMessage constructor parameter or middleware instead of a TextMessage
  2. If the message is meant for the agent, set From to the sender's name so it falls into the user-mapping branch
  3. Ensure Role is exactly Role.User or Role.Assistant for anonymous (null-From) text messages

Example fix

// before
var sys = new TextMessage(Role.System, "You are terse"); // From is null
await agent.SendAsync(sys);

// after
var agent = new OllamaAgent(..., systemMessage: "You are terse");
// or: new TextMessage(Role.System, "You are terse", from: "user");
Defensive patterns

Strategy: validation

Validate before calling

bool IsRoleValidForAnonymousText(TextMessage msg) =>
    msg.From != null || msg.Role is Role.User or Role.Assistant;

Try / catch

catch (InvalidOperationException ex) when (ex.Message == "Invalid Role")
{
    logger.LogWarning("Rejected anonymous text message with role {Role}", msg.Role);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/4c96d8e0de886595. Report an issue: GitHub.