Skip to content

Commit 62b456b

Browse files
Validate name with regex
1 parent 09e8a33 commit 62b456b

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

autogen/agentchat/conversable_agent.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@ def __init__(
137137
)
138138

139139
self._name = name
140-
if self._name.isidentifier() is False:
141-
raise ValueError("The agent name must be a valid Python identifier.")
140+
141+
if ConversableAgent._validate_name(name) is False:
142+
raise ValueError(f"Invalid name: '{name}'. Only letters, numbers, '_' and '-' are allowed.")
143+
142144
# a dictionary of conversations, default value is list
143145
if chat_messages is None:
144146
self._oai_messages = defaultdict(list)
@@ -258,6 +260,12 @@ def __init__(
258260
"a_process_message_before_send": [],
259261
}
260262

263+
@staticmethod
264+
def _validate_name(name: str) -> bool:
265+
"""Validate the name of the agent."""
266+
pattern = r'^[a-zA-Z0-9_-]+$'
267+
return bool(re.match(pattern, name))
268+
261269
def _validate_llm_config(self, llm_config):
262270
assert llm_config in (None, False) or isinstance(
263271
llm_config, dict

0 commit comments

Comments
 (0)