Skip to content

A programming framework for agentic AI

Add additional tests to capture edge cases and more error conditions (#3237)

Umer Mansoora246a79cdae3
diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py
index 2d8958f32..ca1a538e0 100644
--- a/autogen/agentchat/conversable_agent.py
+++ b/autogen/agentchat/conversable_agent.py
@@ -2185,7 +2185,7 @@ class ConversableAgent(LLMAgent):
Ex 2:
"{\n \"location\": \"Boston, MA\"\n}" -> "{"location": "Boston, MA"}"
- 2. this function also handles JSON escape sequences inside quotes,
+ 2. this function also handles JSON escape sequences inside quotes.
Ex 1:
'{"args": "a\na\na\ta"}' -> '{"args": "a\\na\\na\\ta"}'
"""
diff --git a/test/agentchat/test_function_call.py b/test/agentchat/test_function_call.py
index 7023a709d..7745181ea 100755
--- a/test/agentchat/test_function_call.py
+++ b/test/agentchat/test_function_call.py
@@ -91,6 +91,12 @@ def test_json_extraction():
jstr = '{"code": "a=\\"hello\\""}'
assert user._format_json_str(jstr) == '{"code": "a=\\"hello\\""}'
+ jstr = '{\n"tool": "python",\n"query": "print(\'hello\')\n\tprint(\'world\')"\n}' # mixed newlines and tabs
+ assert user._format_json_str(jstr) == '{"tool": "python","query": "print(\'hello\')\\n\\tprint(\'world\')"}'
+
+ jstr = "{}" # empty json
+ assert user._format_json_str(jstr) == "{}"
+
def test_execute_function():
from autogen.agentchat import UserProxyAgent
@@ -117,7 +123,7 @@ def test_execute_function():
} # should be "given_num" with quotes
assert "The argument must be in JSON format." in user.execute_function(func_call=wrong_json_format)[1]["content"]
- # function execution error with wrong arguments passed
+ # function execution error with extra arguments
wrong_args = {"name": "add_num", "arguments": '{ "num_to_be_added": 5, "given_num": 10 }'}
assert "Error: " in user.execute_function(func_call=wrong_args)[1]["content"]
@@ -143,6 +149,19 @@ def test_execute_function():
func_call = {"name": "get_number", "arguments": "{}"}
assert user.execute_function(func_call)[1]["content"] == "42"
+ # 4. test with a non-existent function
+ user = UserProxyAgent(name="test", function_map={})
+ func_call = {"name": "nonexistent_function", "arguments": "{}"}
+ assert "Error: Function" in user.execute_function(func_call=func_call)[1]["content"]
+
+ # 5. test calling a function that raises an exception
+ def raise_exception():
+ raise ValueError("This is an error")
+
+ user = UserProxyAgent(name="test", function_map={"raise_exception": raise_exception})
+ func_call = {"name": "raise_exception", "arguments": "{}"}
+ assert "Error: " in user.execute_function(func_call=func_call)[1]["content"]
+
@pytest.mark.asyncio
async def test_a_execute_function():