microsoft/autogen · error · InvalidOperationException

The from field must be null or the agent name

Error message

The from field must be null or the agent name

What it means

Thrown by OllamaMessageConnector when converting an ImageMessage for an Ollama agent. The connector maps an image to an Ollama 'user' Message only when imageMessage.From is null (with Role.User) or equals a different agent's name; if From is set and equals the current agent's own name, it throws because an agent cannot send itself a user-role image.

Source

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

            var uri = new Uri(imageMessage.Url);
            // download the image from the URL
            using var client = new HttpClient();
            var response = client.GetAsync(uri).Result;
            if (!response.IsSuccessStatusCode)
            {
                throw new HttpRequestException($"Failed to download the image from {uri}");
            }

            data = response.Content.ReadAsByteArrayAsync().Result;
        }

        var base64Image = Convert.ToBase64String(data);
        var message = imageMessage.From switch
        {
            null when imageMessage.Role == Role.User => new Message { Role = "user", Images = [base64Image] },
            null => throw new InvalidOperationException("Invalid Role, the role must be user"),
            _ when imageMessage.From != agent.Name => new Message { Role = "user", Images = [base64Image] },
            _ => throw new InvalidOperationException("The from field must be null or the agent name"),
        };

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

    private IEnumerable<IMessage> ProcessTextMessage(TextMessage textMessage, IAgent agent)
    {
        if (textMessage.Role == Role.System)
        {
            var message = new Message
            {
                Role = "system",
                Value = textMessage.Content
            };

            return [MessageEnvelope.Create(message, agent.Name)];
        }
        else if (textMessage.From == agent.Name)

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Set imageMessage.From = null (and Role = Role.User) before sending the image to the same agent
  2. If the image genuinely comes from another participant, set From to that participant's name instead of the receiving agent's name
  3. When persisting conversation history, strip or rewrite From on messages you intend to re-submit as user input

Example fix

// before
var img = new ImageMessage(role: Role.User, from: agent.Name, url: imageUrl);
var reply = await agent.SendAsync(img);

// after
var img = new ImageMessage(role: Role.User, from: null, url: imageUrl);
var reply = await agent.SendAsync(img);
Defensive patterns

Strategy: validation

Validate before calling

bool CanSendImage(OllamaAgent agent, ImageMessage msg) =>
    msg.From is null || msg.From != agent.Name;

Try / catch

try { var reply = await agent.SendAsync(img); }
catch (InvalidOperationException ex) when (ex.Message.Contains("from field"))
{
    var sanitized = new ImageMessage(Role.User, from: null, url: img.Url);
    reply = await agent.SendAsync(sanitized);
}

Prevention

When it happens

Trigger: ProcessImageMessage is invoked (directly or via the connector middleware) with an ImageMessage where From == agent.Name (non-null), e.g. replaying an agent's own prior output back into the same agent without clearing From.

Common situations: Building group-chat loops in AutoGen.Ollama where the assistant's own image output is fed back into its history; copying messages between agents while keeping the original From value.

Related errors


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