Skip to content

Commit

Permalink
Validate name with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
giorgossideris committed Nov 13, 2024
1 parent 09e8a33 commit 62b456b
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions autogen/agentchat/conversable_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 62b456b

Please sign in to comment.