Working with Parallel Function Calls and Multiple Function Responses in Gemini

Source notebook

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

Shows how Gemini returns and handles parallel function calls with Wikipedia lookup tools.

Summary

This notebook teaches parallel function calling in Gemini using the Google GenAI SDK on Vertex AI. It defines function declarations, sends prompts that trigger multiple function calls, extracts those calls from the Gemini response, executes Wikipedia API calls in application code, and returns multiple function responses to Gemini for a final natural language summary.

Key code patterns

Create Vertex AI GenAI client

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

Configures the Google GenAI SDK to call Gemini through Vertex AI.

Declare function tool

search_wikipedia = FunctionDeclaration(
    name="search_wikipedia",
    description="Search for articles on Wikipedia",
    parameters={"type": "object", "properties": {"query": {"type": "string"}}},
)
 
wikipedia_tool = Tool(function_declarations=[search_wikipedia])

Gives Gemini a structured function schema it can choose when a prompt requires external data.

Start chat with tools

MODEL_ID = "gemini-3.5-flash"
 
chat = client.chats.create(
    model=MODEL_ID,
    config=GenerateContentConfig(temperature=0, tools=[wikipedia_tool]),
)

Attaches function declarations to the Gemini chat session and uses a model version described as supporting parallel function calling.

Extract function calls

def extract_function_calls(response):
    function_calls = []
    for function_call in response.function_calls:
        call = {function_call.name: {}}
        for key, value in function_call.args.items():
            call[function_call.name][key] = value
        function_calls.append(call)
    return function_calls

Converts Gemini’s structured function call response into application-friendly dictionaries.

Return multiple function responses

response = chat.send_message([
    Part.from_function_response(name="search_wikipedia", response={"content": api_response[0]}),
    Part.from_function_response(name="search_wikipedia", response={"content": api_response[1]}),
    Part.from_function_response(name="search_wikipedia", response={"content": api_response[2]}),
])

Sends multiple external API results back to Gemini in one message so it can produce the final summary.

Models & APIs used

  • Models: gemini-3.5-flash
  • APIs / services: Vertex AI, Gemini API, Wikipedia API
  • SDKs / libraries: google-genai, wikipedia

When to use this

Use this pattern when a prompt can trigger multiple independent external lookups or actions before Gemini writes the final response.

Gotchas & caveats

  • Requires an existing Google Cloud project with the Vertex AI API enabled.
  • Colab users must authenticate with google.colab.auth.authenticate_user().
  • PROJECT_ID must be set directly or via GOOGLE_CLOUD_PROJECT.
  • LOCATION is set to global in the notebook.
  • Parallel function calling support depends on specific Gemini model versions.
  • Gemini may choose parallel or chained function calls based on FunctionDeclaration information and dependencies.
  • The notebook manually indexes three same-function responses, so production code should handle variable call counts.

Best practices

  • Use FunctionDeclaration parameters to describe callable functions clearly.
  • Wrap function declarations in a Tool and pass it through GenerateContentConfig.
  • Use temperature=0 for these tool-calling examples.
  • Extract structured function calls from response.function_calls before executing application code.
  • Fan out independent external API calls in application code, then return all function responses to Gemini in bulk.
  • Account for Gemini choosing whether calls can run in parallel or must be chained.