{"record":{"id":"b1798a73466cef71","repo":"microsoft/autogen","slug":"agent-factory-with-type-type-already-exists","errorCode":null,"errorMessage":"Agent factory with type {type} already exists.","messagePattern":"Agent factory with type (.+?) already exists\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs","lineNumber":57,"sourceCode":"\n        return this.agentInstances[agentId];\n    }\n\n    public async ValueTask<Contracts.AgentId> GetAgentAsync(Contracts.AgentId agentId, bool lazy = true)\n    {\n        if (!lazy)\n        {\n            await this.EnsureAgentAsync(agentId);\n        }\n\n        return agentId;\n    }\n\n    public AgentType RegisterAgentFactory(AgentType type, Func<Contracts.AgentId, IAgentRuntime, ValueTask<IHostableAgent>> factoryFunc)\n    {\n        if (this.agentFactories.ContainsKey(type))\n        {\n            throw new Exception($\"Agent factory with type {type} already exists.\");\n        }\n\n        this.agentFactories.Add(type, factoryFunc);\n        return type;\n    }\n\n    public void AddSubscription(ISubscriptionDefinition subscription)\n    {\n        if (this.Subscriptions.ContainsKey(subscription.Id))\n        {\n            throw new Exception($\"Subscription with id {subscription.Id} already exists.\");\n        }\n\n        this.Subscriptions.Add(subscription.Id, subscription);\n    }\n\n    public bool RemoveSubscriptionAsync(string subscriptionId)\n    {","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/Core.Grpc/GrpcAgentRuntime.cs#L39-L75","documentation":"GrpcAgentTypesHost.RegisterAgentFactory throws when a factory for the same AgentType is already registered. The host keeps a Dictionary<AgentType, Func<...>> and refuses duplicate keys to prevent silently replacing agent implementations.","triggerScenarios":"Calling RegisterAgentFactory(type, factory) twice with the same AgentType — commonly from double execution of startup code, hosting the same agent in two hosting groups, or DI lifetimes that re-run registration (e.g. scoped resolution invoking a register method).","commonSituations":"Adding the same agent via two AddAgent/RegisterAgent calls (e.g. in a shared extension method called twice); hot-reload or retry logic in startup that re-executes registration; combining an auto-scan/registration assembly attribute with explicit manual registration; tests that build the host multiple times against a shared host instance.","solutions":["Remove the duplicate registration — search for all RegisterAgentFactory/AddAgent calls for that type and keep one.","If using auto-registration attributes, delete the manual registration (or vice versa).","Guard registration with a check of RegisteredAgentTypes before calling RegisterAgentFactory in shared/pluggable code."],"exampleFix":"// before\nhost.RegisterAgentFactory(new AgentType(\"planner\"), plannerFactory);\nhost.RegisterAgentFactory(new AgentType(\"planner\"), plannerFactory); // throws: duplicate\n\n// after\nif (!host.RegisteredAgentTypes.Contains(new AgentType(\"planner\")))\n{\n    host.RegisterAgentFactory(new AgentType(\"planner\"), plannerFactory);\n}","handlingStrategy":"validation","validationCode":"if (!host.RegisteredAgentTypes.Contains(type))\n{\n    host.RegisterAgentFactory(type, factoryFunc);\n}","typeGuard":"static bool IsFactoryRegistered(GrpcAgentTypesHost host, AgentType type) =>\n    host.RegisteredAgentTypes.Contains(type);","tryCatchPattern":null,"preventionTips":["Centralize agent registration in one startup path; avoid mixing auto-scan and manual registration.","Make registration code idempotent with a RegisteredAgentTypes check when it runs in shared extensions."],"tags":["grpc","agent-registration","duplicate","startup"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}