{"record":{"id":"b1248eecd591b885","repo":"microsoft/autogen","slug":"invalid-agentid-type-type-must-be-alphanumer","errorCode":null,"errorMessage":"Invalid AgentId type: '{type}'. Must be alphanumeric (a-z, 0-9, _) and cannot start with a number or contain spaces.","messagePattern":"Invalid AgentId type: '(.+?)'\\. Must be alphanumeric \\(a-z, 0-9, _\\) and cannot start with a number or contain spaces\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Microsoft.AutoGen/Contracts/AgentId.cs","lineNumber":44,"sourceCode":"    /// </summary>\n    public string Type;\n\n    /// <summary>\n    /// Agent instance identifier.\n    /// Strings may only be composed of alphanumeric letters (a-z) and (0-9), or underscores (_).\n    /// </summary>\n    public string Key;\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"AgentId\"/> struct.\n    /// </summary>\n    /// <param name=\"type\">The agent type.</param>\n    /// <param name=\"key\">Agent instance identifier.</param>\n    public AgentId(string type, string key)\n    {\n        if (string.IsNullOrWhiteSpace(type) || !TypeRegex.IsMatch(type))\n        {\n            throw new ArgumentException($\"Invalid AgentId type: '{type}'. Must be alphanumeric (a-z, 0-9, _) and cannot start with a number or contain spaces.\");\n        }\n\n        if (string.IsNullOrWhiteSpace(key) || !KeyRegex.IsMatch(key))\n        {\n            throw new ArgumentException($\"Invalid AgentId key: '{key}'. Must only contain ASCII characters 32-126.\");\n        }\n\n        Type = type;\n        Key = key;\n    }\n\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"AgentId\"/> struct from a tuple.\n    /// </summary>\n    /// <param name=\"kvPair\">A tuple containing the agent type and key.</param>\n    public AgentId((string Type, string Key) kvPair) : this(kvPair.Type, kvPair.Key)\n    {\n    }","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/Microsoft.AutoGen/Contracts/AgentId.cs#L26-L62","documentation":"The AgentId constructor throws ArgumentException when the agent type string is null/whitespace or fails TypeRegex. Per the message, type must be alphanumeric lowercase (a-z, 0-9, _), cannot start with a digit, and must contain no spaces. This is contract-level validation performed on every AgentId construction.","triggerScenarios":"new AgentId(type, key) where type contains uppercase letters, hyphens, spaces, starts with a digit, or is empty/whitespace. Also reached via the (Type, Key) tuple constructor and any API that builds AgentIds from user/config-supplied strings (e.g. agent names from appsettings or environment variables).","commonSituations":"Using display names like \"Weather Agent\" or \"agent-1\" as agent types; deriving agent type from a class name with dots or capitals (\"MyNamespace.MyAgent\"); config-driven agent names from YAML/env that were never sanitized; version upgrades that tightened the regex.","solutions":["Normalize the type before construction: lowercase, replace invalid chars with '_', prefix a letter if it starts with a digit.","Use the same regex the library uses (^[a-z_][a-z0-9_]*$ shape) to validate names at startup/configuration load, not at AgentId construction.","If the name comes from configuration, fix it there so logs and AgentIds stay readable."],"exampleFix":"// before\nvar id = new AgentId(\"Weather Agent\", \"default\"); // throws: space + uppercase\n\n// after\nvar id = new AgentId(\"weather_agent\", \"default\");","handlingStrategy":"validation","validationCode":"using System.Text.RegularExpressions;\nstatic readonly Regex TypeRx = new(\"^[a-z_][a-z0-9_]*$\", RegexOptions.Compiled);\nbool IsValidAgentType(string? type) =>\n    !string.IsNullOrWhiteSpace(type) && TypeRx.IsMatch(type);","typeGuard":"static bool IsValidAgentType(string? type) =>\n    !string.IsNullOrWhiteSpace(type) && Regex.IsMatch(type, \"^[a-z_][a-z0-9_]*$\");","tryCatchPattern":"try { var id = new AgentId(type, key); }\ncatch (ArgumentException) { type = Sanitize(type); var id = new AgentId(type, key); }","preventionTips":["Validate agent type strings at configuration load time, not at AgentId construction.","Derive agent types from a fixed allow-list of snake_case names."],"tags":["validation","agent-id","contracts","configuration"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}