{"record":{"id":"64e93b3c97c7a8bd","repo":"microsoft/autogen","slug":"all-agents-must-have-a-unique-name","errorCode":null,"errorMessage":"All agents must have a unique name.","messagePattern":"All agents must have a unique name\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.Core/GroupChat/GroupChat.cs","lineNumber":86,"sourceCode":"        this.initializeMessages = initializeMessages ?? new List<IMessage>();\n        this.orchestrator = orchestrator;\n\n        this.Validation();\n    }\n\n    private void Validation()\n    {\n        // check if all agents has a name\n        if (this.agents.Any(x => string.IsNullOrEmpty(x.Name)))\n        {\n            throw new ArgumentException(\"All agents must have a name.\");\n        }\n\n        // check if any agents has the same name\n        var names = this.agents.Select(x => x.Name).ToList();\n        if (names.Distinct().Count() != names.Count)\n        {\n            throw new ArgumentException(\"All agents must have a unique name.\");\n        }\n\n        // if there's a workflow\n        // check if the agents in that workflow are in the group chat\n        if (this.workflow != null)\n        {\n            var agentNamesInWorkflow = this.workflow.Transitions.Select(x => x.From.Name!).Concat(this.workflow.Transitions.Select(x => x.To.Name!)).Distinct();\n            if (agentNamesInWorkflow.Any(x => !this.agents.Select(a => a.Name).Contains(x)))\n            {\n                throw new ArgumentException(\"All agents in the workflow must be in the group chat.\");\n            }\n        }\n    }\n\n    /// <summary>\n    /// Select the next speaker based on the conversation history.\n    /// The next speaker will be decided by a combination effort of the admin and the workflow.\n    /// Firstly, a group of candidates will be selected by the workflow. If there's only one candidate, then that candidate will be the next speaker.","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.Core/GroupChat/GroupChat.cs#L68-L104","documentation":"GroupChat.Validation() requires all member agents to have distinct names. It collects agents.Select(x => x.Name) and, if names.Distinct().Count() != names.Count, throws ArgumentException('All agents must have a unique name.'). Names are the identity used for speaker selection and message attribution, so duplicates would make routing ambiguous.","triggerScenarios":"Passing two agents constructed with the same name string (e.g. two GPTAgent(\"assistant\", ...)); generating members in a loop with a constant name; case-sensitive duplicates like \"Agent\" vs \"Agent\" (the check is ordinal, so exact matches only).","commonSituations":"Copy-pasted agent setup code where the name wasn't changed; factory loops creating N agents with the same label; merging agents from different modules that happen to share a default name.","solutions":["Assign a distinct Name to each member, e.g. suffix loop indices (\"worker-0\", \"worker-1\").","Pre-validate with GroupBy before construction and fail with a clear message showing the duplicates.","If two agents intentionally play the same role, either rename them (\"summarizer-a\"/\"summarizer-b\") or use a single agent.","Note the comparison is case-sensitive: \"Critic\" and \"critic\" both pass validation, but downstream matching lowercases names, so avoid case variants too."],"exampleFix":"// before\nvar members = Enumerable.Range(0, 3).Select(_ => new GPTAgent(\"worker\", ...));\nvar chat = new GroupChat(admin, members);\n\n// after\nvar members = Enumerable.Range(0, 3).Select(i => new GPTAgent($\"worker-{i}\", ...));\nvar chat = new GroupChat(admin, members);","handlingStrategy":"validation","validationCode":"var names = members.Select(m => m.Name).ToList();\nvar duplicates = names.GroupBy(n => n).Where(g => g.Count() > 1).Select(g => g.Key).ToList();\nif (duplicates.Count > 0)\n    throw new ConfigurationException($\"Duplicate agent names: {string.Join(\", \", duplicates)}\");","typeGuard":null,"tryCatchPattern":"try { var chat = new GroupChat(admin, members); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"unique name\"))\n{\n    // rename the duplicate members (e.g. suffix an index) and retry\n}","preventionTips":["Generate names programmatically (base + index) when creating agents in loops.","Run a distinctness pre-check on member names before construction.","Avoid case-variant names; downstream matching lowercases, which creates subtle routing bugs even though validation passes."],"tags":["autogen","dotnet","group-chat","validation","duplicate-names","argumentexception"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}