{"record":{"id":"67997ba621537ae8","repo":"microsoft/autogen","slug":"invalid-name-name-only-letters-numbers-a-67997b","errorCode":null,"errorMessage":"Invalid name: {name}. Only letters, numbers, '_' and '-' are allowed.","messagePattern":"Invalid name: (.+?)\\. Only letters, numbers, '_' and '-' are allowed\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/models/azure/_azure_ai_client.py","lineNumber":175,"sourceCode":"\n\ndef normalize_name(name: str) -> str:\n    \"\"\"\n    LLMs sometimes ask functions while ignoring their own format requirements, this function should be used to replace invalid characters with \"_\".\n\n    Prefer _assert_valid_name for validating user configuration or input\n    \"\"\"\n    return re.sub(r\"[^a-zA-Z0-9_-]\", \"_\", name)[:64]\n\n\ndef assert_valid_name(name: str) -> str:\n    \"\"\"\n    Ensure that configured names are valid, raises ValueError if not.\n\n    For munging LLM responses use _normalize_name to ensure LLM specified names don't break the API.\n    \"\"\"\n    if not re.match(r\"^[a-zA-Z0-9_-]+$\", name):\n        raise ValueError(f\"Invalid name: {name}. Only letters, numbers, '_' and '-' are allowed.\")\n    if len(name) > 64:\n        raise ValueError(f\"Invalid name: {name}. Name must be less than 64 characters.\")\n    return name\n\n\nclass AzureAIChatCompletionClient(ChatCompletionClient):\n    \"\"\"\n    Chat completion client for models hosted on Azure AI Foundry or GitHub Models.\n    See `here <https://learn.microsoft.com/en-us/azure/ai-studio/reference/reference-model-inference-chat-completions>`_ for more info.\n\n    Args:\n        endpoint (str): The endpoint to use. **Required.**\n        credential (union, AzureKeyCredential, AsyncTokenCredential): The credentials to use. **Required**\n        model_info (ModelInfo): The model family and capabilities of the model. **Required.**\n        model (str): The name of the model. **Required if model is hosted on GitHub Models.**\n        frequency_penalty: (optional,float)\n        presence_penalty: (optional,float)\n        temperature: (optional,float)","sourceCodeStart":157,"sourceCodeEnd":193,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/models/azure/_azure_ai_client.py#L157-L193","documentation":"Raised by assert_valid_name in autogen_ext.models.azure._azure_ai_client when an agent/source name contains characters outside [a-zA-Z0-9_-] (the regex ^[a-zA-Z0-9_-]+$ must fully match). It exists because the Azure AI service rejects such names; assert_valid_name is meant for validating user-supplied configuration, while _normalize_name should be used to munge LLM-generated names.","triggerScenarios":"Calling client.create()/create_stream() with a message whose source (or a tool/agent name validated via assert_valid_name) contains spaces, dots, '@', unicode, or is empty. Also triggered by AssistantMessage conversion, which calls assert_valid_name(message.source) first.","commonSituations":"Human-readable agent names like \"helper agent\" or \"agent@example.com\" used as message sources; empty string source; names with leading/trailing whitespace from config files.","solutions":["Rename the agent/source to use only letters, digits, underscore and hyphen (e.g. 'helper_agent')","If the name comes from an LLM response, pass it through _normalize_name (re.sub(r\"[^a-zA-Z0-9_-]\", \"_\", name)[:64]) instead of assert_valid_name","Add an early check in your agent factory that validates names against ^[a-zA-Z0-9_-]+$ before any request is sent"],"exampleFix":"# before\nagent = AssistantAgent(\"helper agent\", ...)  # space is invalid\n\n# after\nagent = AssistantAgent(\"helper_agent\", ...)","handlingStrategy":"validation","validationCode":"import re\n\nVALID_NAME = re.compile(r\"^[a-zA-Z0-9_-]+$\")\n\ndef name_ok(name: str) -> bool:\n    return bool(VALID_NAME.match(name)) and len(name) <= 64","typeGuard":"def is_valid_agent_name(name: str) -> bool:\n    import re\n    return bool(re.match(r\"^[a-zA-Z0-9_-]+$\", name)) and len(name) <= 64","tryCatchPattern":null,"preventionTips":["Validate agent/source names against ^[a-zA-Z0-9_-]+$ at construction time","Use _normalize_name for any name originating from an LLM","Add a lint/CI check over config files for agent name fields"],"tags":["azure","name-validation","configuration","regex"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}