diff --git a/autogen/agentchat/conversable_agent.py b/autogen/agentchat/conversable_agent.py index 609a72b4768..0c6d4c8128e 100644 --- a/autogen/agentchat/conversable_agent.py +++ b/autogen/agentchat/conversable_agent.py @@ -130,6 +130,12 @@ def __init__( description (str): a short description of the agent. This description is used by other agents (e.g. the GroupChatManager) to decide when to call upon this agent. (Default: system_message) """ + # we change code_execution_config below and we have to make sure we don't change the input + # in case of UserProxyAgent, without this we could even change the default value {} + code_execution_config = ( + code_execution_config.copy() if hasattr(code_execution_config, "copy") else code_execution_config + ) + self._name = name # a dictionary of conversations, default value is list self._oai_messages = defaultdict(list) diff --git a/autogen/agentchat/user_proxy_agent.py b/autogen/agentchat/user_proxy_agent.py index a92d58cfde1..980ca12c633 100644 --- a/autogen/agentchat/user_proxy_agent.py +++ b/autogen/agentchat/user_proxy_agent.py @@ -30,7 +30,7 @@ def __init__( max_consecutive_auto_reply: Optional[int] = None, human_input_mode: Literal["ALWAYS", "TERMINATE", "NEVER"] = "ALWAYS", function_map: Optional[Dict[str, Callable]] = None, - code_execution_config: Optional[Union[Dict, Literal[False]]] = None, + code_execution_config: Union[Dict, Literal[False]] = {}, default_auto_reply: Optional[Union[str, Dict, None]] = "", llm_config: Optional[Union[Dict, Literal[False]]] = False, system_message: Optional[Union[str, List]] = "", diff --git a/test/agentchat/test_agent_setup_with_use_docker_settings.py b/test/agentchat/test_agent_setup_with_use_docker_settings.py index 8cda4466e16..4ec05ae1a75 100644 --- a/test/agentchat/test_agent_setup_with_use_docker_settings.py +++ b/test/agentchat/test_agent_setup_with_use_docker_settings.py @@ -16,17 +16,6 @@ skip = False or skip_openai -def get_current_autogen_env_var(): - return os.environ.get("AUTOGEN_USE_DOCKER", None) - - -def restore_autogen_env_var(current_env_value): - if current_env_value is None: - del os.environ["AUTOGEN_USE_DOCKER"] - else: - os.environ["AUTOGEN_USE_DOCKER"] = current_env_value - - def docker_running(): return is_docker_running() or in_docker_container() @@ -54,10 +43,9 @@ def test_agent_setup_with_use_docker_false(): @pytest.mark.skipif(skip, reason="openai not installed") -def test_agent_setup_with_env_variable_false_and_docker_running(): - current_env_value = get_current_autogen_env_var() +def test_agent_setup_with_env_variable_false_and_docker_running(monkeypatch): + monkeypatch.setenv("AUTOGEN_USE_DOCKER", "False") - os.environ["AUTOGEN_USE_DOCKER"] = "False" user_proxy = UserProxyAgent( name="test_agent", human_input_mode="NEVER", @@ -65,21 +53,26 @@ def test_agent_setup_with_env_variable_false_and_docker_running(): assert user_proxy._code_execution_config["use_docker"] is False - restore_autogen_env_var(current_env_value) - @pytest.mark.skipif(skip or (not docker_running()), reason="openai not installed OR docker not running") -def test_agent_setup_with_default_and_docker_running(): +def test_agent_setup_with_default_and_docker_running(monkeypatch): + monkeypatch.delenv("AUTOGEN_USE_DOCKER", raising=False) + + assert os.getenv("AUTOGEN_USE_DOCKER") is None + user_proxy = UserProxyAgent( name="test_agent", human_input_mode="NEVER", ) + assert os.getenv("AUTOGEN_USE_DOCKER") is None + assert user_proxy._code_execution_config["use_docker"] is True @pytest.mark.skipif(skip or (docker_running()), reason="openai not installed OR docker running") -def test_raises_error_agent_setup_with_default_and_docker_not_running(): +def test_raises_error_agent_setup_with_default_and_docker_not_running(monkeypatch): + monkeypatch.delenv("AUTOGEN_USE_DOCKER", raising=False) with pytest.raises(RuntimeError): UserProxyAgent( name="test_agent", @@ -88,10 +81,8 @@ def test_raises_error_agent_setup_with_default_and_docker_not_running(): @pytest.mark.skipif(skip or (docker_running()), reason="openai not installed OR docker running") -def test_raises_error_agent_setup_with_env_variable_true_and_docker_not_running(): - current_env_value = get_current_autogen_env_var() - - os.environ["AUTOGEN_USE_DOCKER"] = "True" +def test_raises_error_agent_setup_with_env_variable_true_and_docker_not_running(monkeypatch): + monkeypatch.setenv("AUTOGEN_USE_DOCKER", "True") with pytest.raises(RuntimeError): UserProxyAgent( @@ -99,14 +90,10 @@ def test_raises_error_agent_setup_with_env_variable_true_and_docker_not_running( human_input_mode="NEVER", ) - restore_autogen_env_var(current_env_value) - @pytest.mark.skipif(skip or (not docker_running()), reason="openai not installed OR docker not running") -def test_agent_setup_with_env_variable_true_and_docker_running(): - current_env_value = get_current_autogen_env_var() - - os.environ["AUTOGEN_USE_DOCKER"] = "True" +def test_agent_setup_with_env_variable_true_and_docker_running(monkeypatch): + monkeypatch.setenv("AUTOGEN_USE_DOCKER", "True") user_proxy = UserProxyAgent( name="test_agent", @@ -114,5 +101,3 @@ def test_agent_setup_with_env_variable_true_and_docker_running(): ) assert user_proxy._code_execution_config["use_docker"] is True - - restore_autogen_env_var(current_env_value)