microsoft/semantic-kernel · error · KernelException

The thread name {agentStep.ThreadName} does not have a match

Error message

The thread name {agentStep.ThreadName} does not have a matching thread variable defined.

What it means

Thrown during step initialization when a KernelProcessAgentStep references a ThreadName that does not exist in the process's _threads dictionary. Agent steps require a named thread to manage conversation history; if the thread name on the step doesn't match any entry declared in the process's Threads collection, the step cannot be constructed.

Source

Thrown at dotnet/src/Experimental/Process.LocalRuntime/LocalProcess.cs:291

                    {
                        ParentProcessId = this.Id,
                    };
            }
            else if (step is KernelProcessProxy proxyStep)
            {
                localStep =
                    new LocalProxy(proxyStep, this._kernel)
                    {
                        ParentProcessId = this.RootProcessId,
                        EventProxy = this.EventProxy,
                        ExternalMessageChannel = this.ExternalMessageChannel
                    };
            }
            else if (step is KernelProcessAgentStep agentStep)
            {
                if (!this._threads.TryGetValue(agentStep.ThreadName, out KernelProcessAgentThread? thread) || thread is null)
                {
                    throw new KernelException($"The thread name {agentStep.ThreadName} does not have a matching thread variable defined.").Log(this._logger);
                }

                localStep = new LocalAgentStep(agentStep, this._kernel, thread, this._processStateManager, this.ParentProcessId);
            }
            else
            {
                // The current step should already have an Id.
                Verify.NotNull(step.State?.Id);

                localStep =
                    new LocalStep(step, this._kernel)
                    {
                        ParentProcessId = this.Id,
                        EventProxy = this.EventProxy
                    };
            }

            this._steps.Add(localStep);

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Ensure every KernelProcessAgentStep.ThreadName exactly matches a key in the process's Threads dictionary.
  2. Search for the ThreadName value shown in the error message across all step definitions to find the mismatch.
  3. Add the missing thread definition to the process's Threads collection, or correct the step's ThreadName to reference an existing thread.

Example fix

// before - step references 'chatThread' but process defines 'assistantThread'
process.AddStep(new KernelProcessAgentStep { ThreadName = "chatThread" });
process.Threads["assistantThread"] = new KernelProcessThread(...);
// after - names match
process.AddStep(new KernelProcessAgentStep { ThreadName = "assistantThread" });
process.Threads["assistantThread"] = new KernelProcessThread(...);
Defensive patterns

Strategy: validation

Validate before calling

// Validate that every agent step's ThreadName exists in the process Threads dictionary before starting
var declaredThreadNames = process.Threads.Keys.ToHashSet();
foreach (var step in process.Steps.OfType<KernelProcessAgentStep>())
{
    if (!declaredThreadNames.Contains(step.ThreadName))
    {
        throw new InvalidOperationException($"Agent step '{step.State.Name}' references thread '{step.ThreadName}' which is not declared.");
    }
}

Prevention

When it happens

Trigger: A KernelProcessAgentStep is added to the process with a ThreadName value that has no corresponding key in the process.Threads dictionary. The lookup at line 289 fails and the exception is thrown.

Common situations: Typo in the ThreadName property of an agent step versus the key used when adding the thread definition; removing a thread from the process but forgetting to update agent steps that reference it; renaming a thread without updating all agent steps.

Related errors


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