{"record":{"id":"76df3bb61b46a324","repo":"microsoft/autogen","slug":"value-cannot-be-null-parameter-name","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'Name')","messagePattern":"Value cannot be null\\. \\(Parameter 'Name'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.SemanticKernel/SemanticKernelChatCompletionAgent.cs","lineNumber":22,"sourceCode":"using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Threading;\nusing System.Threading.Tasks;\nusing Microsoft.SemanticKernel;\nusing Microsoft.SemanticKernel.Agents;\nusing Microsoft.SemanticKernel.ChatCompletion;\n\nnamespace AutoGen.SemanticKernel;\n\npublic class SemanticKernelChatCompletionAgent : IAgent\n{\n    public string Name { get; }\n    private readonly ChatCompletionAgent _chatCompletionAgent;\n\n    public SemanticKernelChatCompletionAgent(ChatCompletionAgent chatCompletionAgent)\n    {\n        this.Name = chatCompletionAgent.Name ?? throw new ArgumentNullException(nameof(chatCompletionAgent.Name));\n        this._chatCompletionAgent = chatCompletionAgent;\n    }\n\n    public async Task<IMessage> GenerateReplyAsync(IEnumerable<IMessage> messages, GenerateReplyOptions? options = null,\n        CancellationToken cancellationToken = default)\n    {\n        var agentThread = new ChatHistoryAgentThread(BuildChatHistory(messages));\n        var reply = await _chatCompletionAgent\n            .InvokeAsync(agentThread, cancellationToken: cancellationToken)\n            .ToArrayAsync(cancellationToken: cancellationToken);\n\n        return reply.Length > 1\n            ? throw new InvalidOperationException(\"ResultsPerPrompt greater than 1 is not supported in this semantic kernel agent\")\n            : new MessageEnvelope<ChatMessageContent>(reply[0], from: this.Name);\n    }\n\n    private ChatHistory BuildChatHistory(IEnumerable<IMessage> messages)\n    {","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.SemanticKernel/SemanticKernelChatCompletionAgent.cs#L4-L40","documentation":"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').","triggerScenarios":"new SemanticKernelChatCompletionAgent(new ChatCompletionAgent { ... }) where the Name/AgentName property was never assigned.","commonSituations":"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.","solutions":["Always set Name (older SK: AgentName) on the ChatCompletionAgent before wrapping it","Check 'chatCompletionAgent.Name is null' and assign a fallback name before constructing the adapter","After upgrading Microsoft.SemanticKernel, re-verify which property populates the agent name"],"exampleFix":"// before\nvar skAgent = new ChatCompletionAgent { Instructions = \"You are helpful.\" };\nvar agent = new SemanticKernelChatCompletionAgent(skAgent); // throws: Name is null\n\n// after\nvar skAgent = new ChatCompletionAgent { Name = \"assistant\", Instructions = \"You are helpful.\" };\nvar agent = new SemanticKernelChatCompletionAgent(skAgent);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(chatCompletionAgent.Name))\n    throw new InvalidOperationException(\"Set ChatCompletionAgent.Name before wrapping in SemanticKernelChatCompletionAgent.\");\nvar agent = new SemanticKernelChatCompletionAgent(chatCompletionAgent);","typeGuard":"static bool HasName(ChatCompletionAgent a) => !string.IsNullOrEmpty(a.Name);","tryCatchPattern":"try { return new SemanticKernelChatCompletionAgent(skAgent); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"Name\")\n{\n    skAgent.Name = \"assistant\";\n    return new SemanticKernelChatCompletionAgent(skAgent);\n}","preventionTips":["Always assign Name when constructing ChatCompletionAgent","Wrap adapter creation in a factory that defaults missing names","After SK package upgrades, re-check which property carries the agent name (Name vs AgentName across versions)"],"tags":["csharp","semantic-kernel","constructor","null-argument"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}