diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 3a21c0f7d0e..584a03ca0df 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -137,6 +137,10 @@ def __init__( ) self._name = name + + if ConversableAgent._validate_name(name) is False: + raise ValueError(f"Invalid name: '{name}'. Only letters, numbers, '_' and '-' are allowed.") + # a dictionary of conversations, default value is list if chat_messages is None: self._oai_messages = defaultdict(list) @@ -256,6 +260,12 @@ def __init__( "a_process_message_before_send": [], } + @staticmethod + def _validate_name(name: str) -> bool: + """Validate the name of the agent.""" + pattern = r'^[a-zA-Z0-9_-]+$' + return bool(re.match(pattern, name)) + def _validate_llm_config(self, llm_config): assert llm_config in (None, False) or isinstance( llm_config, dict