-
Notifications
You must be signed in to change notification settings - Fork 698
Fix(Bug): Automatic_function_calling config persists across requests, causing UNEXPECTED_TOOL_CALL when tools=None #1841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
…causing UNEXPECTED_TOOL_CALL when tools=None
|
Hey @Muhammad-Rebaal |
BenjaminKazemi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please test tests/afc/test_get_max_remote_calls_for_afc.py as well. Thanks for your contribution.
| ), | ||
| ) | ||
| ) | ||
| is False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change the test cases as much as possible instead of changing the assertion. By changing only the assertion, the tests are not validating the actual test case. E.g. in this test case, pass in a tool and keep the assertion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback! I've updated the test cases as suggested.
Instead of changing the assertions, I've added tools parameter to the three failing tests:
test_afc_enable_unset_max_1 , test_afc_enable_unset_max_1_0 , test_afc_enable_true_max_unset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! In addition to that would you mind adding an example code that reproduce the bug? AFC logic may seem simple in the first glance but there are corner cases. Would be idea if you could add an example to regenerate the issue or refer me to an existing one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My pleasure , Here's the minimal that can reproduce the error mentioned by @npkanaka :
from google import genai
from google.genai import types
client = genai.Client(api_key="#####")
MODEL = "gemini-2.5-pro"
tools = [
types.Tool(
function_declarations=[
types.FunctionDeclaration(
name="noop", description="noop", parameters={"type": "object", "properties": {}}
)
]
)
]
# Call 1: with tools, force function_call
resp1 = client.models.generate_content(
model=MODEL,
contents="Call the noop tool. Do not answer in text. Only return a function call to noop with empty args.",
config=types.GenerateContentConfig(tools=tools),
)
print("Call 1:", resp1)
# Call 2: tools=None, fresh
resp2 = client.models.generate_content(
model=MODEL,
contents="hello from call without tools",
config=types.GenerateContentConfig(tools=None),
)
print("Call 2:", resp2)
# Call 3: history includes prior function_call, tools=None
history = [
types.Content(
role="user",
parts=[types.Part(text="Call the noop tool. Do not answer in text. Only return a function call to noop with empty args.")],
),
resp1.candidates[0].content, # contains function_call
types.Content(
role="user",
parts=[types.Part(text="Now answer normally (no tools). Just say hi in plain text.")],
),
]
resp3 = client.models.generate_content(
model=MODEL,
contents=history,
config=types.GenerateContentConfig(tools=None),
)
print("Call 3:", resp3)
Although, as you said there can be corner cases, so kindly let me know what is up to you. I'd be happy to implement that under your mentorship.
Fixes #1818
Hi @janasangeetha !
I've identified and fixed this issue
The bug was in the
should_disable_afc()function ingoogle/genai/_extra_utils.py. The function defaulted to enabling AFC even when tools=None or tools=[], because it checked theautomatic_function_callingconfig before checking whether any tools were actually provided.Thank you!