microsoft/semantic-kernel · error · ArgumentNullException

client

Error message

client

What it means

This is a constructor guard on CopilotStudioAgentThread. The thread requires a CopilotClient to start conversations and ask questions via the Copilot Studio runtime. A null client would cause NullReferenceException during any subsequent operation, so the constructor fails fast with ArgumentNullException naming 'client'.

Source

Thrown at dotnet/src/Agents/Copilot/CopilotStudioAgentThread.cs:28

namespace Microsoft.SemanticKernel.Agents.Copilot;

/// <summary>
/// Represents a conversation thread for a <see cref="CopilotStudioAgent"/>.
/// </summary>
public sealed class CopilotStudioAgentThread : AgentThread
{
    private readonly CopilotClient _client;

    /// <summary>
    /// Initializes a new instance of the <see cref="CopilotStudioAgentThread"/> class.
    /// </summary>
    /// <param name="client">A client used to interact with the Copilot Agent runtime service.</param>
    /// <param name="conversationId">An optional session Id to continue an existing session.</param>
    /// <exception cref="ArgumentNullException"></exception>
    public CopilotStudioAgentThread(CopilotClient client, string? conversationId = null)
    {
        this._client = client ?? throw new ArgumentNullException(nameof(client));
        this.Id = conversationId;
    }

    internal ILogger Logger { get; init; } = NullLogger.Instance;

    /// <inheritdoc />
    protected override async Task<string?> CreateInternalAsync(CancellationToken cancellationToken)
    {
        try
        {
            await foreach (IActivity activity in this._client.StartConversationAsync(emitStartConversationEvent: true, cancellationToken).ConfigureAwait(false))
            {
                if (activity.Conversation is not null)
                {
                    return activity.Conversation.Id;
                }
            }

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Construct a valid CopilotClient (with the correct Copilot Studio endpoint/settings) and pass the non-null instance to the constructor.
  2. If using DI, register the CopilotClient and inject it into the component that creates threads.
  3. Add a null-check in your factory/build code so the thread is never created with a null client.
  4. In tests, pass a mocked CopilotClient instance (non-null) rather than null.

Example fix

// before
var thread = new CopilotStudioAgentThread(client: null, conversationId);

// after
var client = new CopilotClient(settings); // or resolved from DI
var thread = new CopilotStudioAgentThread(client, conversationId);
Defensive patterns

Strategy: validation

Validate before calling

CopilotClient? client = ResolveCopilotClient();
if (client is null)
    throw new InvalidOperationException("CopilotClient could not be resolved; check configuration.");
var thread = new CopilotStudioAgentThread(client, conversationId);

Type guard

static bool IsValidCopilotClient(CopilotClient? c) => c is not null;

Try / catch

try { var thread = new CopilotStudioAgentThread(client, conversationId); }
catch (ArgumentNullException ex) when (ex.ParamName == "client")
{
    logger.LogError("CopilotClient was null. Ensure the client is constructed/configured before creating the thread.");
    throw;
}

Prevention

When it happens

Trigger: Instantiating `new CopilotStudioAgentThread(null)` or passing a null CopilotClient (e.g., from a factory that returned null, or a DI resolution that failed). The guard fires before any service interaction.

Common situations: The CopilotClient was never constructed; a factory/builder returned null under an error path; the client was conditionally set and the branch that left it null was taken; unit test instantiation with a null mock.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/2392a01852578dea. Report an issue: GitHub.