Skip to content

Commit 02d8416

Browse files
BenjaminKazemicopybara-github
authored andcommitted
fix: disabling automatic function calling when tools are not set
PiperOrigin-RevId: 843351821
1 parent 5af1978 commit 02d8416

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

google/genai/_extra_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,15 @@ def should_disable_afc(
437437
)
438438
return True
439439

440+
# If there are no tools, disable AFC.
441+
if not config_model.tools:
442+
if config_model.automatic_function_calling:
443+
logger.warning(
444+
'No tools are specified in the config, but automatic_function_calling'
445+
' is enabled. Therefore, automatic function calling is disabled.'
446+
)
447+
return True
448+
440449
# Default to enable AFC if not specified.
441450
if (
442451
not config_model.automatic_function_calling

google/genai/tests/afc/test_get_max_remote_calls_for_afc.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,14 @@ def test_config_is_none():
2626

2727

2828
def test_afc_unset_max_unset():
29-
assert get_max_remote_calls_afc(types.GenerateContentConfig()) == 10
29+
assert (
30+
get_max_remote_calls_afc(
31+
types.GenerateContentConfig(
32+
tools=[types.Tool(retrieval=types.Retrieval())]
33+
)
34+
)
35+
== 10
36+
)
3037

3138

3239
def test_afc_unset_max_set():
@@ -36,6 +43,7 @@ def test_afc_unset_max_set():
3643
automatic_function_calling=types.AutomaticFunctionCallingConfig(
3744
maximum_remote_calls=20,
3845
),
46+
tools=[types.Tool(retrieval=types.Retrieval())],
3947
)
4048
)
4149
== 20
@@ -72,6 +80,7 @@ def test_afc_d_max_unset():
7280
automatic_function_calling=types.AutomaticFunctionCallingConfig(
7381
disable=False,
7482
),
83+
tools=[types.Tool(retrieval=types.Retrieval())],
7584
)
7685
)
7786
== 10
@@ -86,6 +95,7 @@ def test_afc_d_max_set():
8695
disable=False,
8796
maximum_remote_calls=5,
8897
),
98+
tools=[types.Tool(retrieval=types.Retrieval())],
8999
)
90100
)
91101
== 5
@@ -96,10 +106,11 @@ def test_afc_enabled_max_set_to_zero():
96106
with pytest.raises(ValueError):
97107
get_max_remote_calls_afc(
98108
types.GenerateContentConfig(
99-
automatic_function_calling=types.AutomaticFunctionCallingConfig(
109+
automatic_function_calling=types.AutomaticFunctionCallingConfig(
100110
disable=False,
101111
maximum_remote_calls=0,
102112
),
113+
tools=[types.Tool(retrieval=types.Retrieval())],
103114
)
104115
)
105116

@@ -124,6 +135,7 @@ def test_afc_enabled_max_set_to_float():
124135
disable=False,
125136
maximum_remote_calls=5.0,
126137
),
138+
tools=[types.Tool(retrieval=types.Retrieval())],
127139
)
128140
)
129141
== 5

google/genai/tests/afc/test_should_disable_afc.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,41 @@ def test_config_is_none():
2626

2727

2828
def test_afc_config_unset():
29-
assert should_disable_afc(types.GenerateContentConfig()) is False
29+
assert should_disable_afc(types.GenerateContentConfig()) is True
30+
31+
32+
def test_afc_config_with_tools():
33+
assert (
34+
should_disable_afc(
35+
types.GenerateContentConfig(
36+
tools=[types.Tool(retrieval=types.Retrieval())]
37+
)
38+
)
39+
is False
40+
)
41+
42+
43+
def test_afc_config_with_empty_tools_when_afc_is_enabled():
44+
assert (
45+
should_disable_afc(
46+
types.GenerateContentConfig(
47+
automatic_function_calling=types.AutomaticFunctionCallingConfig(),
48+
tools=[],
49+
)
50+
)
51+
is True
52+
)
53+
54+
55+
def test_afc_config_without_tools_when_afc_is_enabled():
56+
assert (
57+
should_disable_afc(
58+
types.GenerateContentConfig(
59+
automatic_function_calling=types.AutomaticFunctionCallingConfig()
60+
)
61+
)
62+
is True
63+
)
3064

3165

3266
def test_afc_enable_unset_max_0():
@@ -71,6 +105,7 @@ def test_afc_enable_unset_max_1():
71105
automatic_function_calling=types.AutomaticFunctionCallingConfig(
72106
maximum_remote_calls=1,
73107
),
108+
tools=[types.Tool(retrieval=types.Retrieval())],
74109
)
75110
)
76111
is False
@@ -80,9 +115,11 @@ def test_afc_enable_unset_max_1():
80115
def test_afc_enable_unset_max_1_0():
81116
assert (
82117
should_disable_afc(
83-
{'automatic_function_calling': {'maximum_remote_calls': 1.0}}
118+
{
119+
'automatic_function_calling': {'maximum_remote_calls': 1.0},
120+
}
84121
)
85-
is False
122+
is True
86123
)
87124

88125

@@ -157,6 +194,7 @@ def test_afc_enable_true_max_unset():
157194
automatic_function_calling=types.AutomaticFunctionCallingConfig(
158195
disable=False,
159196
),
197+
tools=[types.Tool(retrieval=types.Retrieval())],
160198
)
161199
)
162200
is False
@@ -208,6 +246,7 @@ def test_afc_enable_true_max_1():
208246
disable=False,
209247
maximum_remote_calls=1,
210248
),
249+
tools=[types.Tool(retrieval=types.Retrieval())],
211250
)
212251
)
213252
is False

0 commit comments

Comments
 (0)