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_calling active
- Make a subsequent request with
tools=None (no tools declared)
- Observe
FinishReason.UNEXPECTED_TOOL_CALL with 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_CALL
response.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 = None
Root 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
Summary
The
automatic_function_callingconfiguration inGenerateContentConfigpersists across API requests within the same client session. When a request with tools enabled (AFC active) is followed by a request withtools=None, Gemini still attempts function calls, resulting inFinishReason.UNEXPECTED_TOOL_CALLwith malformed responses (candidate.content = None).Steps to Reproduce
automatic_function_callingactivetools=None(no tools declared)FinishReason.UNEXPECTED_TOOL_CALLwith empty response contentExpected Behavior
When
tools=Noneor 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"INFO:google_genai.models:AFC is enabled with max remote calls: 10"Environment
google-genai(Python)gemini-2.5-proWorkaround
Explicitly set
config.automatic_function_calling = Nonewhenconfig.toolsis None/empty:Root 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