diff --git a/google/genai/_extra_utils.py b/google/genai/_extra_utils.py index e0fb9c105..be1853bd3 100644 --- a/google/genai/_extra_utils.py +++ b/google/genai/_extra_utils.py @@ -437,6 +437,15 @@ def should_disable_afc( ) return True + # If there are no tools, disable AFC. + if not config_model.tools: + if config_model.automatic_function_calling: + logger.warning( + 'No tools are specified in the config, but automatic_function_calling' + ' is enabled. Therefore, automatic function calling is disabled.' + ) + return True + # Default to enable AFC if not specified. if ( not config_model.automatic_function_calling diff --git a/google/genai/tests/afc/test_get_max_remote_calls_for_afc.py b/google/genai/tests/afc/test_get_max_remote_calls_for_afc.py index 5a72020ed..52d5038c4 100644 --- a/google/genai/tests/afc/test_get_max_remote_calls_for_afc.py +++ b/google/genai/tests/afc/test_get_max_remote_calls_for_afc.py @@ -26,7 +26,14 @@ def test_config_is_none(): def test_afc_unset_max_unset(): - assert get_max_remote_calls_afc(types.GenerateContentConfig()) == 10 + assert ( + get_max_remote_calls_afc( + types.GenerateContentConfig( + tools=[types.Tool(retrieval=types.Retrieval())] + ) + ) + == 10 + ) def test_afc_unset_max_set(): @@ -36,6 +43,7 @@ def test_afc_unset_max_set(): automatic_function_calling=types.AutomaticFunctionCallingConfig( maximum_remote_calls=20, ), + tools=[types.Tool(retrieval=types.Retrieval())], ) ) == 20 @@ -72,6 +80,7 @@ def test_afc_d_max_unset(): automatic_function_calling=types.AutomaticFunctionCallingConfig( disable=False, ), + tools=[types.Tool(retrieval=types.Retrieval())], ) ) == 10 @@ -86,6 +95,7 @@ def test_afc_d_max_set(): disable=False, maximum_remote_calls=5, ), + tools=[types.Tool(retrieval=types.Retrieval())], ) ) == 5 @@ -96,10 +106,11 @@ def test_afc_enabled_max_set_to_zero(): with pytest.raises(ValueError): get_max_remote_calls_afc( types.GenerateContentConfig( - automatic_function_calling=types.AutomaticFunctionCallingConfig( + automatic_function_calling=types.AutomaticFunctionCallingConfig( disable=False, maximum_remote_calls=0, ), + tools=[types.Tool(retrieval=types.Retrieval())], ) ) @@ -124,6 +135,7 @@ def test_afc_enabled_max_set_to_float(): disable=False, maximum_remote_calls=5.0, ), + tools=[types.Tool(retrieval=types.Retrieval())], ) ) == 5 diff --git a/google/genai/tests/afc/test_should_disable_afc.py b/google/genai/tests/afc/test_should_disable_afc.py index 188bf2b57..89bc03f22 100644 --- a/google/genai/tests/afc/test_should_disable_afc.py +++ b/google/genai/tests/afc/test_should_disable_afc.py @@ -26,7 +26,41 @@ def test_config_is_none(): def test_afc_config_unset(): - assert should_disable_afc(types.GenerateContentConfig()) is False + assert should_disable_afc(types.GenerateContentConfig()) is True + + +def test_afc_config_with_tools(): + assert ( + should_disable_afc( + types.GenerateContentConfig( + tools=[types.Tool(retrieval=types.Retrieval())] + ) + ) + is False + ) + + +def test_afc_config_with_empty_tools_when_afc_is_enabled(): + assert ( + should_disable_afc( + types.GenerateContentConfig( + automatic_function_calling=types.AutomaticFunctionCallingConfig(), + tools=[], + ) + ) + is True + ) + + +def test_afc_config_without_tools_when_afc_is_enabled(): + assert ( + should_disable_afc( + types.GenerateContentConfig( + automatic_function_calling=types.AutomaticFunctionCallingConfig() + ) + ) + is True + ) def test_afc_enable_unset_max_0(): @@ -71,6 +105,7 @@ def test_afc_enable_unset_max_1(): automatic_function_calling=types.AutomaticFunctionCallingConfig( maximum_remote_calls=1, ), + tools=[types.Tool(retrieval=types.Retrieval())], ) ) is False @@ -80,9 +115,11 @@ def test_afc_enable_unset_max_1(): def test_afc_enable_unset_max_1_0(): assert ( should_disable_afc( - {'automatic_function_calling': {'maximum_remote_calls': 1.0}} + { + 'automatic_function_calling': {'maximum_remote_calls': 1.0}, + } ) - is False + is True ) @@ -157,6 +194,7 @@ def test_afc_enable_true_max_unset(): automatic_function_calling=types.AutomaticFunctionCallingConfig( disable=False, ), + tools=[types.Tool(retrieval=types.Retrieval())], ) ) is False @@ -208,6 +246,7 @@ def test_afc_enable_true_max_1(): disable=False, maximum_remote_calls=1, ), + tools=[types.Tool(retrieval=types.Retrieval())], ) ) is False