From 09e8a3327bb917d167ad1a7ea4c676d15908d02b Mon Sep 17 00:00:00 2001 From: giorgossideris Date: Tue, 12 Nov 2024 22:39:56 +0200 Subject: [PATCH 1/2] Check that agent name is valid python identifier --- autogen/agentchat/conversable_agent.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 3a21c0f7d0e..83712fe285c 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -137,6 +137,8 @@ def __init__( ) self._name = name + if self._name.isidentifier() is False: + raise ValueError("The agent name must be a valid Python identifier.") # a dictionary of conversations, default value is list if chat_messages is None: self._oai_messages = defaultdict(list) From 62b456be0cf9472924f5ceceb66def0e17ad958f Mon Sep 17 00:00:00 2001 From: giorgossideris Date: Wed, 13 Nov 2024 09:58:24 +0200 Subject: [PATCH 2/2] Validate name with regex --- autogen/agentchat/conversable_agent.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 83712fe285c..584a03ca0df 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -137,8 +137,10 @@ def __init__( ) self._name = name - if self._name.isidentifier() is False: - raise ValueError("The agent name must be a valid Python identifier.") + + 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) @@ -258,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