microsoft/autogen · error · InvalidOperationException

Cannot pop a layer while the context is initialized.

Error message

Cannot pop a layer while the context is initialized.

What it means

RunContextStack.PopLayer throws InvalidOperationException when a layer is removed after the run context has been initialized. Note the pop guard throws before any stack mutation regardless — once initialized, layer removal is disallowed (the method body contains only the guard, mirroring PushLayer).

Source

Thrown at dotnet/src/Microsoft.AutoGen/AgentChat/GroupChat/RunContext.cs:82

    }

    // TODO: There is probably a way to have a sound manner by which pushing/popping a layer when initialized
    // would be allowed. But this is not necessary for now, so we will keep it simple.
    public void PushLayer(IRunContextLayer layer)
    {
        if (this.IsInitialized)
        {
            throw new InvalidOperationException("Cannot push a layer while the context is initialized.");
        }

        this.Uninitialized.Push(layer);
    }

    public void PopLayer()
    {
        if (this.IsInitialized)
        {
            throw new InvalidOperationException("Cannot pop a layer while the context is initialized.");
        }
    }

    private Action? initializeError;
    protected override void OnInitializeError()
    {
        (this.initializeError ?? base.OnInitializeError)();
    }

    private Action? deinitializeError;
    protected override void OnDeinitializeError()
    {
        (this.deinitializeError ?? base.OnDeinitializeError)();
    }

    public static IRunContextLayer OverrideErrors(Action? initializeError = null, Action? deinitializeError = null)
    {
        return new ErrorOverrideLayer(initializeError, deinitializeError);

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Do not use Push/Pop for scoped layers around a run; construct a new RunContextStack with exactly the layers needed per run.
  2. Ensure symmetric push/pop code paths both execute before initialization begins, or drop the pattern entirely.
  3. Recreate the Team/RunContext instead of mutating layer composition after a run.

Example fix

// before
ctx.PushLayer(layer);
try { await team.RunAsync(task); }
finally { ctx.PopLayer(); } // throws if run initialized the context

// after
var ctx = new RunContextStack(layer);
await team.RunAsync(task);
Defensive patterns

Strategy: validation

Validate before calling

if (!runContextStack.IsInitialized)
{
    runContextStack.PopLayer();
}

Prevention

When it happens

Trigger: Calling RunContextStack.PopLayer() at any point after IsInitialized is true — typically from cleanup/disposal code that runs after a chat has started, or between runs of a long-lived Team.

Common situations: Implementing 'scoped' context layers with try/finally pop in a using-pattern where the push happened pre-initialization but the pop happens post-initialization; unwinding layers in an exception path after a run started; shared/persistent Team instances across requests.

Related errors


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