-
Notifications
You must be signed in to change notification settings - Fork 726
Description
Summary
The automatic_function_calling configuration in GenerateContentConfig persists across API requests within the same client session. When a request with tools enabled (AFC active) is followed by a request with tools=None, Gemini still attempts function calls, resulting in FinishReason.UNEXPECTED_TOOL_CALL with malformed responses (candidate.content = None).
Steps to Reproduce
- Create a Gemini client with a model that supports function calling
- Make a request with tools enabled and
automatic_function_callingactive - Make a subsequent request with
tools=None(no tools declared) - Observe
FinishReason.UNEXPECTED_TOOL_CALLwith empty response content
Expected Behavior
When tools=None or empty, AFC should be disabled automatically for that request, regardless of previous requests' configurations.
Actual Behavior
AFC remains enabled from previous requests. Gemini attempts function calls from conversation context even when no tools are declared, resulting in:
FinishReason.UNEXPECTED_TOOL_CALLresponse.candidates[0].content = None- Log message:
"INFO:google_genai.models:AFC is enabled with max remote calls: 10"
Environment
- SDK:
google-genai(Python) - Integration: CrewAI (multi-agent framework making multiple LLM calls with varying tool configurations)
- Model:
gemini-2.5-pro
Workaround
Explicitly set config.automatic_function_calling = None when config.tools is None/empty:
if config and hasattr(config, 'tools') and not config.tools:
if hasattr(config, 'automatic_function_calling'):
config.automatic_function_calling = NoneRoot Cause Analysis
AFC appears to be a client-level or session-level setting that doesn't reset between individual generate_content() calls. This is problematic for frameworks that make multiple LLM calls with different tool configurations in the same session.
Related Issues
- LangChain.js issue with UNEXPECTED_TOOL_CALL: Gemini 2.5-flash-lite stops with UNEXPECTED_TOOL_CALL, even if no tools were actually passed langchain-ai/langchainjs#8589