Forced Function Calling with Tool Configurations in Gemini

Source notebook

Repo path: gemini/function-calling/forced_function_calling.ipynb · Open on GitHub · intermediate

Shows how to force, allow, or disable Gemini function calls with tool configurations.

Summary

This notebook teaches forced function calling in Gemini using the Google GenAI SDK on Vertex AI. It defines an arXiv search function declaration, compares AUTO, ANY, and NONE function calling modes, executes a real arXiv search when Gemini emits a function call, and sends the tool response back to Gemini for a final answer.

Key code patterns

Create Vertex AI GenAI client

from google import genai
 
client = genai.Client(
    enterprise=True,
    project=PROJECT_ID,
    location="global",
)

Initializes the Google GenAI SDK client against a Google Cloud project and Vertex AI location.

Declare a function tool

search_arxiv = FunctionDeclaration(
    name="search_arxiv",
    description="Search for articles and publications in arXiv",
    parameters=Schema(
        type=Type.OBJECT,
        properties={"query": Schema(type=Type.STRING)},
    ),
)
search_tool = Tool(function_declarations=[search_arxiv])

Gives Gemini a typed function schema it can call with structured arguments.

Default AUTO mode

config.tool_config = ToolConfig(
    function_calling_config=FunctionCallingConfig(
        mode=FunctionCallingConfigMode.AUTO,
    )
)
response = client.models.generate_content(
    model=MODEL_ID,
    contents=prompt,
    config=config,
)

Lets Gemini decide between a natural language answer and a function call.

Force a specific function

config.tool_config = ToolConfig(
    function_calling_config=FunctionCallingConfig(
        mode=FunctionCallingConfigMode.ANY,
        allowed_function_names=["search_arxiv"],
    )
)
response = client.models.generate_content(
    model=MODEL_ID,
    contents=prompt,
    config=config,
)

Uses ANY mode and allowed_function_names to force Gemini to emit a call to search_arxiv.

Execute tool and return result

if response.function_calls[0].name == "search_arxiv":
    search = arxiv.Search(
        query=params["query"], max_results=3,
        sort_by=arxiv.SortCriterion.Relevance,
    )
    results = str(list(arxiv.Client().results(search)))

Validates the function name, calls arXiv with Gemini-generated arguments, and stores results for the model.

Send function response back

response = client.models.generate_content(
    model=MODEL_ID,
    contents=[
        prompt,
        model_response_content,
        Content(role="tool", parts=[Part.from_function_response(
            name="search_arxiv", response={"content": results}
        )]),
    ],
    config=config,
)

Completes the tool-calling loop by passing the tool response back to Gemini for a final grounded answer.

Disable function calling

config.tool_config = ToolConfig(
    function_calling_config=FunctionCallingConfig(
        mode=FunctionCallingConfigMode.NONE,
    )
)

Instructs Gemini to behave as if no tools or function declarations were provided.

Models & APIs used

  • Models: gemini-3.5-flash
  • APIs / services: Vertex AI
  • SDKs / libraries: google-genai, arxiv

When to use this

Use this pattern when an application needs Gemini to call a required tool, restrict calls to approved functions, or explicitly disable tool use.

Gotchas & caveats

  • Requires an existing Google Cloud project and the Vertex AI API enabled.
  • Colab users must authenticate with google.colab.auth.authenticate_user().
  • The notebook sets LOCATION to global.
  • Model and model version support for forced function calling and tool configurations must be checked in the Gemini Function Calling documentation.
  • ANY mode forces a function call; if allowed_function_names is empty, any provided function can be called.
  • NONE mode returns natural language without calling tools, even when the prompt asks for arXiv papers.
  • The final tool-response request includes model_response_content saved for thought signature.
  • The arXiv search depends on the external arxiv package and arXiv service.

Best practices

  • Use typed FunctionDeclaration parameters with Schema and Type values.
  • Set temperature=0 for deterministic function-calling examples.
  • Use AUTO when the model should decide whether a tool is needed.
  • Use ANY with allowed_function_names to force a specific function or subset of functions.
  • Check response.function_calls[0].name before executing the external function.
  • Clear config.tool_config before sending the tool response back for the final answer.
  • Use NONE when tools are defined but should not be used for a request.