microsoft/autogen · warning · ArgumentNullException

Value cannot be null. (Parameter 'Content')

Error message

Value cannot be null. (Parameter 'Content')

What it means

CreateMultiModaMessageFromOpenAIUserMultiModalMessage guards against a null Content array with ArgumentNullException('Content'). In the normal ProcessMessages flow this is unreachable (the caller already required Length > 0), but any future caller that skips that guard, or a refactor of the 'when' clause, will hit it — it exists to fail fast rather than NRE inside LINQ Select.

Source

Thrown at dotnet/src/AutoGen.WebAPI/OpenAI/Service/OpenAIChatCompletionService.cs:144

            _ => throw new ArgumentException($"Unsupported message type {m.GetType()}")
        });
    }

    private GenerateReplyOptions ProcessReplyOptions(OpenAIChatCompletionOption request)
    {
        return new GenerateReplyOptions()
        {
            Temperature = request.Temperature,
            MaxToken = request.MaxTokens,
            StopSequence = request.Stop,
        };
    }

    private MultiModalMessage CreateMultiModaMessageFromOpenAIUserMultiModalMessage(OpenAIUserMultiModalMessage message)
    {
        if (message.Content is null)
        {
            throw new ArgumentNullException(nameof(message.Content));
        }

        IEnumerable<IMessage> items = message.Content.Select<OpenAIUserMessageItem, IMessage>(item => item switch
        {
            OpenAIUserImageContent imageContent when imageContent.Url is string url => new ImageMessage(Role.User, url, this.agent.Name),
            OpenAIUserTextContent textContent when textContent.Content is string content => new TextMessage(Role.User, content, this.agent.Name),
            _ => throw new ArgumentException($"Unsupported content type {item.GetType()}")
        });

        return new MultiModalMessage(Role.User, items, this.agent.Name);
    }
}

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Keep the 'when userMultiModalMessage.Content is { Length: > 0 }' guard intact in ProcessMessages
  2. Treat null-content multimodal messages as empty input: skip them or reject with 400 before conversion
  3. Add a unit test covering null and empty Content arrays for this method

Example fix

// before
var mm = new OpenAIUserMultiModalMessage { Content = null };
CreateMultiModaMessageFromOpenAIUserMultiModalMessage(mm); // throws ArgumentNullException

// after
var mm = new OpenAIUserMultiModalMessage { Content = Array.Empty<OpenAIUserMessageItem>() };
if (mm.Content is { Length: > 0 }) CreateMultiModaMessageFromOpenAIUserMultiModalMessage(mm);
Defensive patterns

Strategy: validation

Validate before calling

if (message.Content is null || message.Content.Length == 0)
    return BadRequest("Multimodal content array must contain at least one item.");
var mm = CreateMultiModaMessageFromOpenAIUserMultiModalMessage(message);

Type guard

static bool HasNonEmptyMultimodalContent(OpenAIUserMultiModalMessage m) => m.Content is { Length: > 0 };

Try / catch

catch (ArgumentNullException ex) when (ex.ParamName == "Content")
{
    return BadRequest("Multimodal message Content must not be null.");
}

Prevention

When it happens

Trigger: Invoking multimodal conversion with an OpenAIUserMultiModalMessage whose Content array is null — only possible if the ProcessMessages guard is removed or the method is called from new code paths.

Common situations: Refactoring ProcessMessages' pattern guards, adding a new message-mapping path that forgets the empty check, or deserialization changes that make Content default to null.

Related errors


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