{"record":{"id":"84ff82fa086d9812","repo":"microsoft/autogen","slug":"agent-name-name-is-not-a-valid-identifier","errorCode":null,"errorMessage":"Agent name '{name}' is not a valid identifier.","messagePattern":"Agent name '(.+?)' is not a valid identifier\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/AgentChat/Abstractions/ChatAgent.cs","lineNumber":109,"sourceCode":"\n    private static readonly Regex AgentNameRegex = new Regex($\"^{IdStartClass}{IdContinueClass}*$\", RegexOptions.Compiled | RegexOptions.Singleline);\n\n    public string Value { get; }\n\n    public AgentName(string name)\n    {\n        AgentName.CheckValid(name);\n\n        this.Value = name;\n    }\n\n    public static bool IsValid(string name) => AgentNameRegex.IsMatch(name);\n\n    public static void CheckValid(string name)\n    {\n        if (!AgentName.IsValid(name))\n        {\n            throw new ArgumentException($\"Agent name '{name}' is not a valid identifier.\");\n        }\n    }\n\n    // Implicit cast to string\n    public static implicit operator string(AgentName agentName) => agentName.Value;\n}\n\n/// <summary>\n/// A response from calling <see cref=\"IChatAgent\"/>'s <see cref=\"IHandleChat{TIn, Response}.HandleAsync(TIn)\"/>.\"/>\n/// </summary>\npublic class Response\n{\n    /// <summary>\n    /// A chat message produced by the agent as a response.\n    /// </summary>\n    public required ChatMessage Message { get; set; }\n\n    /// <summary>","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/AgentChat/Abstractions/ChatAgent.cs#L91-L127","documentation":"Thrown by AgentName's constructor via AgentName.CheckValid. Agent and team names must be valid Python-style identifiers (regex ^[IdStart][IdContinue]*$ over Unicode letter/underscore start and word-char continue classes) to keep parity with the Python AutoGen runtime. Names containing spaces, hyphens, dots, leading digits, or other punctuation are rejected.","triggerScenarios":"Constructing any Microsoft.AutoGen.AgentChat type that takes an agent name (ChatAgent name, AgentName) with strings like \"primary-agent\", \"Writer Agent\", \"1st_agent\", or names derived from unvalidated user/environment input.","commonSituations":"Deriving agent names from display names, file names, or LLM-generated labels that contain hyphens/spaces; interop with systems that allow arbitrary name strings; multi-agent teams where names come from a config file without the identifier restriction documented.","solutions":["Use identifier-safe names: letters/digits/underscore, not starting with a digit (e.g. 'writer_agent' not 'writer-agent')","Sanitize names before construction: replace non-word characters with underscores and strip leading digits","Validate early with AgentName.IsValid(name) and surface a friendly error before building the team"],"exampleFix":"// before\nvar agent = new SomeChatAgent(name: \"primary-agent\");\n// after\nstatic string Sanitize(string n) { var s = Regex.Replace(n, @\"\\W\", \"_\"); return char.IsDigit(s[0]) ? \"_\" + s : s; }\nvar agent = new SomeChatAgent(name: Sanitize(\"primary-agent\")); // primary_agent","handlingStrategy":"validation","validationCode":"if (!AgentName.IsValid(name))\n    name = Regex.Replace(name, @\"\\W\", \"_\").TrimStart('0','1','2','3','4','5','6','7','8','9');\nif (!AgentName.IsValid(name)) throw new ArgumentException($\"'{name}' cannot be made a valid agent name\");","typeGuard":"static bool IsValidAgentName(string name) => AgentName.IsValid(name); // letters/digits/underscore, no leading digit, no spaces/hyphens","tryCatchPattern":"catch (ArgumentException ex) when (ex.Message.Contains(\"not a valid identifier\")) { throw new ArgumentException(\"Agent names must be Python-style identifiers (letters, digits, underscore; no leading digit)\", ex); }","preventionTips":["Validate or sanitize names with AgentName.IsValid before constructing agents","Never feed raw display names, file names, or LLM-generated labels directly as agent names","Add a unit test asserting every configured agent name passes AgentName.IsValid"],"tags":["autogen","agent-name","identifier","validation","csharp"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}