microsoft/autogen · error · Exception

Subscription with id {subscription.Id} already exists.

Error message

Subscription with id {subscription.Id} already exists.

What it means

GrpcAgentTypesHost.AddSubscription throws when a subscription with the same Id is already present in the Subscriptions dictionary. Subscription ids are expected to be unique per host; adding a semantically equal subscription twice is treated as a programming error rather than an idempotent no-op.

Source

Thrown at dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs:68

        return agentId;
    }

    public AgentType RegisterAgentFactory(AgentType type, Func<Contracts.AgentId, IAgentRuntime, ValueTask<IHostableAgent>> factoryFunc)
    {
        if (this.agentFactories.ContainsKey(type))
        {
            throw new Exception($"Agent factory with type {type} already exists.");
        }

        this.agentFactories.Add(type, factoryFunc);
        return type;
    }

    public void AddSubscription(ISubscriptionDefinition subscription)
    {
        if (this.Subscriptions.ContainsKey(subscription.Id))
        {
            throw new Exception($"Subscription with id {subscription.Id} already exists.");
        }

        this.Subscriptions.Add(subscription.Id, subscription);
    }

    public bool RemoveSubscriptionAsync(string subscriptionId)
    {
        if (!this.Subscriptions.ContainsKey(subscriptionId))
        {
            throw new Exception($"Subscription with id {subscriptionId} does not exist.");
        }

        return this.Subscriptions.Remove(subscriptionId);
    }

    public HashSet<AgentType> RegisteredAgentTypes => this.agentFactories.Keys.ToHashSet();
    public IEnumerable<IHostableAgent> LiveAgents => this.agentInstances.Values;
}

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Check host.Subscriptions.ContainsKey(subscription.Id) before adding, or call RemoveSubscriptionAsync first if refreshing is intended.
  2. Fix the id-generation logic so logically distinct subscriptions get distinct ids.
  3. De-duplicate subscription registration in startup code paths.

Example fix

// before
host.AddSubscription(new TypePrefixSubscription("events", id: "sub1"));
host.AddSubscription(new TypePrefixSubscription("events", id: "sub1")); // throws

// after
if (!host.Subscriptions.ContainsKey("sub1"))
{
    host.AddSubscription(new TypePrefixSubscription("events", id: "sub1"));
}
Defensive patterns

Strategy: validation

Validate before calling

if (!host.Subscriptions.ContainsKey(subscription.Id))
{
    host.AddSubscription(subscription);
}

Type guard

static bool IsSubscriptionAdded(GrpcAgentTypesHost host, string id) =>
    host.Subscriptions.ContainsKey(id);

Prevention

When it happens

Trigger: Calling AddSubscription(subscription) twice with the same subscription.Id — e.g. re-adding a TypePrefixSubscription/TopicSubscription on restart-in-place, or two components subscribing to the same topic producing identical ids.

Common situations: Auto-generated subscription ids colliding (e.g. deterministic ids derived from topic prefix that are identical by design); startup code that re-runs on reconnect or hot reload; registering subscriptions both in a shared extension and in Program.cs; tests reusing one host.

Related errors


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