From cce258d0c36e0337d51b6ec1fc3e7a837d67fd09 Mon Sep 17 00:00:00 2001 From: Muhammad-Rebaal Date: Wed, 10 Dec 2025 19:48:15 +0500 Subject: [PATCH 1/3] Fix(Bug):automatic_function_calling config persists across requests, causing UNEXPECTED_TOOL_CALL when tools=None --- google/genai/_extra_utils.py | 2 ++ google/genai/tests/afc/test_should_disable_afc.py | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/google/genai/_extra_utils.py b/google/genai/_extra_utils.py index e0fb9c105..9cd80be2a 100644 --- a/google/genai/_extra_utils.py +++ b/google/genai/_extra_utils.py @@ -438,6 +438,8 @@ def should_disable_afc( return True # Default to enable AFC if not specified. + if not config_model.tools or len(config_model.tools) == 0: + return True if ( not config_model.automatic_function_calling or config_model.automatic_function_calling.disable is None diff --git a/google/genai/tests/afc/test_should_disable_afc.py b/google/genai/tests/afc/test_should_disable_afc.py index 188bf2b57..d3eebf323 100644 --- a/google/genai/tests/afc/test_should_disable_afc.py +++ b/google/genai/tests/afc/test_should_disable_afc.py @@ -26,7 +26,7 @@ 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_enable_unset_max_0(): @@ -65,6 +65,7 @@ def test_afc_enable_unset_max_0_0(): def test_afc_enable_unset_max_1(): + # Fixed: should disable AFC when no tools provided (GitHub issue #1818) assert ( should_disable_afc( types.GenerateContentConfig( @@ -73,16 +74,17 @@ def test_afc_enable_unset_max_1(): ), ) ) - is False + is True ) def test_afc_enable_unset_max_1_0(): + # Fixed: should disable AFC when no tools provided (GitHub issue #1818) assert ( should_disable_afc( {'automatic_function_calling': {'maximum_remote_calls': 1.0}} ) - is False + is True ) @@ -151,6 +153,7 @@ def test_afc_enable_false_max_1(): def test_afc_enable_true_max_unset(): + # Fixed: should disable AFC when no tools provided (GitHub issue #1818) assert ( should_disable_afc( types.GenerateContentConfig( @@ -159,7 +162,7 @@ def test_afc_enable_true_max_unset(): ), ) ) - is False + is True # No tools = disabled, even if disable=False ) @@ -201,6 +204,7 @@ def test_afc_enable_true_max_0_0(): def test_afc_enable_true_max_1(): + # Fixed: should disable AFC when no tools provided (GitHub issue #1818) assert ( should_disable_afc( types.GenerateContentConfig( @@ -210,5 +214,5 @@ def test_afc_enable_true_max_1(): ), ) ) - is False + is True # No tools = disabled, even if disable=False ) From 1cd1c9d364cdaafc876b21712d69da1ed783ec49 Mon Sep 17 00:00:00 2001 From: Muhammad-Rebaal Date: Wed, 10 Dec 2025 20:02:18 +0500 Subject: [PATCH 2/3] Removed development comments added just to remember --- google/genai/tests/afc/test_should_disable_afc.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/google/genai/tests/afc/test_should_disable_afc.py b/google/genai/tests/afc/test_should_disable_afc.py index d3eebf323..2392912dc 100644 --- a/google/genai/tests/afc/test_should_disable_afc.py +++ b/google/genai/tests/afc/test_should_disable_afc.py @@ -65,7 +65,6 @@ def test_afc_enable_unset_max_0_0(): def test_afc_enable_unset_max_1(): - # Fixed: should disable AFC when no tools provided (GitHub issue #1818) assert ( should_disable_afc( types.GenerateContentConfig( @@ -79,7 +78,6 @@ def test_afc_enable_unset_max_1(): def test_afc_enable_unset_max_1_0(): - # Fixed: should disable AFC when no tools provided (GitHub issue #1818) assert ( should_disable_afc( {'automatic_function_calling': {'maximum_remote_calls': 1.0}} @@ -153,7 +151,6 @@ def test_afc_enable_false_max_1(): def test_afc_enable_true_max_unset(): - # Fixed: should disable AFC when no tools provided (GitHub issue #1818) assert ( should_disable_afc( types.GenerateContentConfig( @@ -162,7 +159,7 @@ def test_afc_enable_true_max_unset(): ), ) ) - is True # No tools = disabled, even if disable=False + is True ) @@ -204,7 +201,6 @@ def test_afc_enable_true_max_0_0(): def test_afc_enable_true_max_1(): - # Fixed: should disable AFC when no tools provided (GitHub issue #1818) assert ( should_disable_afc( types.GenerateContentConfig( @@ -214,5 +210,5 @@ def test_afc_enable_true_max_1(): ), ) ) - is True # No tools = disabled, even if disable=False + is True ) From f7fa88aa06228c17522422d46c479397933b444a Mon Sep 17 00:00:00 2001 From: Muhammad-Rebaal Date: Sat, 13 Dec 2025 00:04:24 +0500 Subject: [PATCH 3/3] test: Update AFC tests to include tools and correct expected outcomes --- .../genai/tests/afc/test_should_disable_afc.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/google/genai/tests/afc/test_should_disable_afc.py b/google/genai/tests/afc/test_should_disable_afc.py index 2392912dc..c89085f24 100644 --- a/google/genai/tests/afc/test_should_disable_afc.py +++ b/google/genai/tests/afc/test_should_disable_afc.py @@ -71,18 +71,24 @@ def test_afc_enable_unset_max_1(): automatic_function_calling=types.AutomaticFunctionCallingConfig( maximum_remote_calls=1, ), + tools=[types.Tool(function_declarations=[ + types.FunctionDeclaration(name='tool_name') + ])], ) ) - is True + is False ) 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}, + 'tools': [{'function_declarations': [{'name': 'tool_name'}]}] + } ) - is True + is False ) @@ -157,9 +163,12 @@ def test_afc_enable_true_max_unset(): automatic_function_calling=types.AutomaticFunctionCallingConfig( disable=False, ), + tools=[types.Tool(function_declarations=[ + types.FunctionDeclaration(name='tool_name') + ])], ) ) - is True + is False )