Skip to content

automatic_function_calling config persists across requests, causing UNEXPECTED_TOOL_CALL when tools=None #1818

@npkanaka

Description

@npkanaka

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

  1. Create a Gemini client with a model that supports function calling
  2. Make a request with tools enabled and automatic_function_calling active
  3. Make a subsequent request with tools=None (no tools declared)
  4. 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

Metadata

Metadata

Assignees

Labels

type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions