From 9b281b1055b4d356faad41aa509f334befb3b224 Mon Sep 17 00:00:00 2001 From: Mark Sze Date: Wed, 20 Nov 2024 23:50:19 +0000 Subject: [PATCH] Changes to support json error message changes for Python 3.13 --- .../test_function_and_tool_calling.py | 61 +++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/test/agentchat/test_function_and_tool_calling.py b/test/agentchat/test_function_and_tool_calling.py index cd7064cd7..eaaea6a8a 100644 --- a/test/agentchat/test_function_and_tool_calling.py +++ b/test/agentchat/test_function_and_tool_calling.py @@ -5,12 +5,16 @@ # Portions derived from https://github.com/microsoft/autogen are under the MIT License. # SPDX-License-Identifier: MIT import json +import sys from typing import Any, Callable, Dict, List import pytest from autogen.agentchat.conversable_agent import ConversableAgent +# Cater for Python version 3.13+ changes in json error messages +PYTHON131PLUS = sys.version_info >= (3, 13) + def _tool_func_1(arg1: str, arg2: str) -> str: return f"_tool_func_1: {arg1} {arg2}" @@ -107,6 +111,20 @@ async def _a_tool_func_error(arg1: str, arg2: str) -> str: "content": "Error: Expecting property name enclosed in double quotes: line 1 column 37 (char 36)\n The argument must be in JSON format.\n\n_tool_func_2: value3 value4", } +# Python 3.13+ has different json error messages +_tool_use_message_1_bad_json_expected_reply_313 = { + "role": "tool", + "tool_responses": [ + { + "tool_call_id": "1", + "role": "tool", + "content": "Error: Illegal trailing comma before end of object: line 1 column 36 (char 35)\n The argument must be in JSON format.", + }, + {"tool_call_id": "2", "role": "tool", "content": "_tool_func_2: value3 value4"}, + ], + "content": "Error: Illegal trailing comma before end of object: line 1 column 36 (char 35)\n The argument must be in JSON format.\n\n_tool_func_2: value3 value4", +} + _tool_use_message_1_error_expected_reply = { "role": "tool", "tool_responses": [ @@ -163,6 +181,13 @@ async def _a_tool_func_error(arg1: str, arg2: str) -> str: "content": "Error: Expecting property name enclosed in double quotes: line 1 column 37 (char 36)\n The argument must be in JSON format.", } +# Python 3.13+ has different json error messages +_function_use_message_1_bad_json_expected_reply_313 = { + "name": "_tool_func_1", + "role": "function", + "content": "Error: Illegal trailing comma before end of object: line 1 column 36 (char 35)\n The argument must be in JSON format.", +} + _function_use_message_1_error_expected_reply = { "name": "_tool_func_1", "role": "function", @@ -240,7 +265,14 @@ def test_generate_function_call_reply_on_function_call_message(is_function_async # bad JSON messages = [_function_use_message_1_bad_json] finished, retval = agent.generate_function_call_reply(messages) - assert (finished, retval) == (True, _function_use_message_1_bad_json_expected_reply) + assert (finished, retval) == ( + True, + ( + _function_use_message_1_bad_json_expected_reply_313 + if PYTHON131PLUS + else _function_use_message_1_bad_json_expected_reply + ), + ) # tool call messages = [_tool_use_message_1] @@ -282,7 +314,14 @@ async def test_a_generate_function_call_reply_on_function_call_message(is_functi # bad JSON messages = [_function_use_message_1_bad_json] finished, retval = await agent.a_generate_function_call_reply(messages) - assert (finished, retval) == (True, _function_use_message_1_bad_json_expected_reply) + assert (finished, retval) == ( + True, + ( + _function_use_message_1_bad_json_expected_reply_313 + if PYTHON131PLUS + else _function_use_message_1_bad_json_expected_reply + ), + ) # tool call messages = [_tool_use_message_1] @@ -323,7 +362,14 @@ def test_generate_tool_calls_reply_on_function_call_message(is_function_async: b # bad JSON messages = [_tool_use_message_1_bad_json] finished, retval = agent.generate_tool_calls_reply(messages) - assert (finished, retval) == (True, _tool_use_message_1_bad_json_expected_reply) + assert (finished, retval) == ( + True, + ( + _tool_use_message_1_bad_json_expected_reply_313 + if PYTHON131PLUS + else _tool_use_message_1_bad_json_expected_reply + ), + ) # function call messages = [_function_use_message_1] @@ -365,7 +411,14 @@ async def test_a_generate_tool_calls_reply_on_function_call_message(is_function_ # bad JSON messages = [_tool_use_message_1_bad_json] finished, retval = await agent.a_generate_tool_calls_reply(messages) - assert (finished, retval) == (True, _tool_use_message_1_bad_json_expected_reply) + assert (finished, retval) == ( + True, + ( + _tool_use_message_1_bad_json_expected_reply_313 + if PYTHON131PLUS + else _tool_use_message_1_bad_json_expected_reply + ), + ) # function call messages = [_function_use_message_1]