microsoft/autogen · error · ArgumentNullException

Value cannot be null. (Parameter 'Name')

Error message

Value cannot be null. (Parameter 'Name')

What it means

The SemanticKernelChatCompletionAgent constructor requires the wrapped Microsoft.SemanticKernel.Agents.ChatCompletionAgent to have a non-null Name, because IAgent.Name is a non-nullable string used to tag replies. If the ChatCompletionAgent was created without a name, the ctor throws ArgumentNullException('Name').

Source

Thrown at dotnet/src/AutoGen.SemanticKernel/SemanticKernelChatCompletionAgent.cs:22

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.ChatCompletion;

namespace AutoGen.SemanticKernel;

public class SemanticKernelChatCompletionAgent : IAgent
{
    public string Name { get; }
    private readonly ChatCompletionAgent _chatCompletionAgent;

    public SemanticKernelChatCompletionAgent(ChatCompletionAgent chatCompletionAgent)
    {
        this.Name = chatCompletionAgent.Name ?? throw new ArgumentNullException(nameof(chatCompletionAgent.Name));
        this._chatCompletionAgent = chatCompletionAgent;
    }

    public async Task<IMessage> GenerateReplyAsync(IEnumerable<IMessage> messages, GenerateReplyOptions? options = null,
        CancellationToken cancellationToken = default)
    {
        var agentThread = new ChatHistoryAgentThread(BuildChatHistory(messages));
        var reply = await _chatCompletionAgent
            .InvokeAsync(agentThread, cancellationToken: cancellationToken)
            .ToArrayAsync(cancellationToken: cancellationToken);

        return reply.Length > 1
            ? throw new InvalidOperationException("ResultsPerPrompt greater than 1 is not supported in this semantic kernel agent")
            : new MessageEnvelope<ChatMessageContent>(reply[0], from: this.Name);
    }

    private ChatHistory BuildChatHistory(IEnumerable<IMessage> messages)
    {

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Always set Name (older SK: AgentName) on the ChatCompletionAgent before wrapping it
  2. Check 'chatCompletionAgent.Name is null' and assign a fallback name before constructing the adapter
  3. After upgrading Microsoft.SemanticKernel, re-verify which property populates the agent name

Example fix

// before
var skAgent = new ChatCompletionAgent { Instructions = "You are helpful." };
var agent = new SemanticKernelChatCompletionAgent(skAgent); // throws: Name is null

// after
var skAgent = new ChatCompletionAgent { Name = "assistant", Instructions = "You are helpful." };
var agent = new SemanticKernelChatCompletionAgent(skAgent);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(chatCompletionAgent.Name))
    throw new InvalidOperationException("Set ChatCompletionAgent.Name before wrapping in SemanticKernelChatCompletionAgent.");
var agent = new SemanticKernelChatCompletionAgent(chatCompletionAgent);

Type guard

static bool HasName(ChatCompletionAgent a) => !string.IsNullOrEmpty(a.Name);

Try / catch

try { return new SemanticKernelChatCompletionAgent(skAgent); }
catch (ArgumentNullException ex) when (ex.ParamName == "Name")
{
    skAgent.Name = "assistant";
    return new SemanticKernelChatCompletionAgent(skAgent);
}

Prevention

When it happens

Trigger: new SemanticKernelChatCompletionAgent(new ChatCompletionAgent { ... }) where the Name/AgentName property was never assigned.

Common situations: Creating a ChatCompletionAgent with only Kernel and Instructions/Template set (name is optional in SK), then adapting it for AutoGen; or a newer SK version renaming/defaulting the name property so an old initializer no longer sets it.

Related errors


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