{"record":{"id":"abce277ed6947bc0","repo":"microsoft/semantic-kernel","slug":"agent-with-type-type-already-exists","errorCode":null,"errorMessage":"Agent with type {type} already exists.","messagePattern":"Agent with type (.+?) already exists\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/Runtime/InProcess/InProcessRuntime.cs","lineNumber":278,"sourceCode":"\n    /// <summary>\n    /// Registers an agent factory with the runtime, associating it with a specific agent type.\n    /// </summary>\n    /// <typeparam name=\"TAgent\">The type of agent created by the factory.</typeparam>\n    /// <param name=\"type\">The agent type to associate with the factory.</param>\n    /// <param name=\"factoryFunc\">A function that asynchronously creates the agent instance.</param>\n    /// <returns>A task representing the asynchronous operation, returning the registered agent type.</returns>\n    public ValueTask<AgentType> RegisterAgentFactoryAsync<TAgent>(AgentType type, Func<AgentId, IAgentRuntime, ValueTask<TAgent>> factoryFunc) where TAgent : IHostableAgent\n        // Declare the lambda return type explicitly, as otherwise the compiler will infer 'ValueTask<TAgent>'\n        // and recurse into the same call, causing a stack overflow.\n        => this.RegisterAgentFactoryAsync(type, async ValueTask<IHostableAgent> (agentId, runtime) => await factoryFunc(agentId, runtime).ConfigureAwait(false));\n\n    /// <inheritdoc/>\n    public ValueTask<AgentType> RegisterAgentFactoryAsync(AgentType type, Func<AgentId, IAgentRuntime, ValueTask<IHostableAgent>> factoryFunc)\n    {\n        if (this._agentFactories.ContainsKey(type))\n        {\n            throw new InvalidOperationException($\"Agent with type {type} already exists.\");\n        }\n\n        this._agentFactories.Add(type, factoryFunc);\n\n#if !NETCOREAPP\n        return type.AsValueTask();\n#else\n        return ValueTask.FromResult(type);\n#endif\n    }\n\n    /// <inheritdoc/>\n    public ValueTask<AgentProxy> TryGetAgentProxyAsync(AgentId agentId)\n    {\n        AgentProxy proxy = new(agentId, this);\n\n#if !NETCOREAPP\n        return proxy.AsValueTask();","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Runtime/InProcess/InProcessRuntime.cs#L260-L296","documentation":"RegisterAgentFactoryAsync throws InvalidOperationException if an agent factory is already registered for the given AgentType. The runtime keys factories strictly by type, so the same type cannot be registered twice. Note RegisterAgentFactoryAsync<TAgent> delegates to the non-generic overload after wrapping, so both paths share this guard.","triggerScenarios":"Registering the same AgentType twice (e.g. two orchestrations both registering \"Agent_1\"); re-registering after a partial setup; orchestrations reusing a topic/format that collide on generated agent types.","commonSituations":"Running two orchestrations on one runtime without distinct topic/type namespaces; init code that runs twice; tests reusing a runtime and re-registering factories.","solutions":["Use distinct AgentType values per orchestration/registration (namespacing by topic or orchestration label).","Use a fresh InProcessRuntime for independent agent sets.","Guard registration by checking existing factories if the API exposes them, or catch the exception for idempotent re-init."],"exampleFix":"// before\nruntime.RegisterAgentFactoryAsync(\"worker\", factoryA);\nruntime.RegisterAgentFactoryAsync(\"worker\", factoryB); // throws\n\n// after\nruntime.RegisterAgentFactoryAsync(\"orch1_worker\", factoryA);\nruntime.RegisterAgentFactoryAsync(\"orch2_worker\", factoryB);","handlingStrategy":"validation","validationCode":"HashSet<AgentType> registered = new();\nasync ValueTask RegisterOnceAsync(InProcessRuntime rt, AgentType type, Func<AgentId, IAgentRuntime, ValueTask<IHostableAgent>> factory)\n{\n    if (registered.Add(type)) await rt.RegisterAgentFactoryAsync(type, factory);\n}","typeGuard":null,"tryCatchPattern":"try { await runtime.RegisterAgentFactoryAsync(type, factory); }\ncatch (InvalidOperationException) { /* type already registered */ }","preventionTips":["Namespace agent types per orchestration to avoid collisions.","Use a fresh runtime for independent agent sets.","Track registered types to make registration idempotent."],"tags":["inprocess-runtime","registration","duplicate","agents","dotnet"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}