{"record":{"id":"3af7933357790b10","repo":"microsoft/semantic-kernel","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/Agents/Runtime/Abstractions/AgentType.cs","lineNumber":28,"sourceCode":"/// This is a strongly-typed wrapper around a string, ensuring type safety when working with agent types.\n/// </summary>\n/// <remarks>\n/// This struct is immutable and provides implicit conversion to and from <see cref=\"string\"/>.\n/// </remarks>\npublic readonly partial struct AgentType : IEquatable<AgentType>\n{\n#if NET\n    [GeneratedRegex(\"^[a-zA-Z_][a-zA-Z0-9_]*$\")]\n    private static partial Regex TypeRegex();\n#else\n    private static Regex TypeRegex() => new(\"^[a-zA-Z_][a-zA-Z0-9_]*$\", RegexOptions.Compiled);\n#endif\n\n    internal static void Validate(string type)\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\n    /// <summary>\n    /// Initializes a new instance of the <see cref=\"AgentId\"/> struct.\n    /// </summary>\n    /// <param name=\"type\">The agent type.</param>\n    public AgentType(string type)\n    {\n        Validate(type);\n        this.Name = type;\n    }\n\n    /// <summary>\n    /// The string representation of this agent type.\n    /// </summary>\n    public string Name { get; }\n","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Runtime/Abstractions/AgentType.cs#L10-L46","documentation":"AgentType.Validate enforces the regex ^[a-zA-Z_][a-zA-Z0-9_]*$ (must start with a letter or underscore, then only letters/digits/underscores) and rejects null/whitespace. Because AgentType has an implicit conversion from string, this validation fires whenever you assign a string to an AgentType or construct one. It also fires inside AgentId's constructor for the type component.","triggerScenarios":"Using a type string with spaces, hyphens, dots, or starting with a digit; passing a fully-qualified class name (e.g. \"MyNs.MyAgent\") as the type; an empty or whitespace type; a type containing slashes.","commonSituations":"Using a human-friendly display name or namespace-qualified type name as the agent type; reading the type from config with leading/trailing spaces; dynamic generation that yields disallowed characters.","solutions":["Use a simple identifier (letters, digits, underscores, leading non-digit) for the agent type.","Sanitize generated names by replacing disallowed characters with underscores and ensuring a letter/underscore prefix.","Derive the type from a safe constant rather than free-form input."],"exampleFix":"// before\nAgentType type = \"My.Namespace.Agent\"; // '.' rejected\n\n// after\nAgentType type = \"MyNamespaceAgent\";","handlingStrategy":"validation","validationCode":"static readonly Regex AgentTypeRegex = new(@\"^[a-zA-Z_][a-zA-Z0-9_]*$\", RegexOptions.Compiled);\nbool IsValidAgentType(string type) => !string.IsNullOrWhiteSpace(type) && AgentTypeRegex.IsMatch(type);\nstring SanitizeType(string type) => Regex.Replace(type ?? \"\", \"[^a-zA-Z0-9_]\", \"_\") is var s && char.IsDigit(s[0]) ? \"_\" + s : s;","typeGuard":"bool IsValidAgentType(string type) => !string.IsNullOrWhiteSpace(type) && Regex.IsMatch(type, @\"^[a-zA-Z_][a-zA-Z0-9_]*$\");","tryCatchPattern":null,"preventionTips":["Use simple identifiers (letters/digits/underscore, leading non-digit) for agent types.","Do not use namespace-qualified or display names as the type.","Trim and sanitize config-derived type strings."],"tags":["agenttype","validation","naming","runtime","dotnet"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}