C.W.K.
Stream
Lesson 04 of 04 · published

Auto Calling & Special Tools

~12 min · auto-calling, code-execution, google-search, grounding

Level 0Spark
0 XP0/35 lessons0/10 achievements
0/140 XP to next level140 XP to go0% complete

Automatic function calling — Python only

The Python SDK can run the entire tool loop for you. Pass plain callables to tools=[...]; the SDK introspects type hints + docstring, builds the declarations, and handles the call/response loop internally. Only available in Python. TypeScript still requires the manual loop.

Two built-in tools you can hand the model directly

Google ships two tools that they execute, not you:

  • Code execution (ToolCodeExecution) — the model writes Python, Google runs it server-side, returns the output. Useful for math, parsing, plotting.
  • Google Search (GoogleSearch) — model issues a search and reads results. Provides grounding for current events.

You don't write a dispatcher for these — they execute inside Google's infrastructure.

Combining tools

You can mix custom function declarations with built-in tools in the same call. The model picks whichever fits the user's intent.

Code

Auto-calling — SDK handles the loop·python
from google import genai
from google.genai import types

client = genai.Client()

def set_light_values(brightness: int, color_temp: str) -> dict:
    """Sets brightness (0-100) and color_temp (daylight/cool/warm)."""
    return {'brightness': brightness, 'colorTemperature': color_temp}

def get_weather(location: str) -> dict:
    """Get current weather for a location."""
    return {'location': location, 'temp_c': 22, 'conditions': 'clear'}

# SDK introspects type hints + docstring, runs the entire tool loop.
response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents='Set the lights warm and 30%, then tell me the weather in Seoul.',
    config=types.GenerateContentConfig(
        tools=[set_light_values, get_weather],
    ),
)
print(response.text)
# Both tools were called by the SDK; you got the final natural-language reply.

# Disable auto-calling and go back to manual loop:
config = types.GenerateContentConfig(
    tools=[set_light_values],
    automatic_function_calling=types.AutomaticFunctionCallingConfig(disable=True),
)
Code execution·python
response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents='What is the sum of the first 50 prime numbers?',
    config=types.GenerateContentConfig(
        tools=[types.Tool(code_execution=types.ToolCodeExecution())],
    ),
)
print(response.text)
# The model wrote and ran Python server-side, then explained the answer.
Google Search grounding·python
response = client.models.generate_content(
    model='gemini-2.5-flash',
    contents='Who won the 2024 European Championship in football, and what was the final score?',
    config=types.GenerateContentConfig(
        tools=[types.Tool(google_search=types.GoogleSearch())],
    ),
)
print(response.text)
# Grounded answer with citations available in response.candidates[0].grounding_metadata.
Mixing custom + built-in tools·python
config = types.GenerateContentConfig(
    tools=[
        set_light_values,                                           # custom
        get_weather,                                                # custom
        types.Tool(code_execution=types.ToolCodeExecution()),       # built-in
        types.Tool(google_search=types.GoogleSearch()),             # built-in
    ],
)
# The model picks whichever tool fits each step of the conversation.

External links

Exercise

Write a 2-line program: get_weather(location) + convert_celsius_to_fahrenheit(c). Pass both to generate_content with auto-calling enabled. Ask: "What's the weather in Seoul, and convert the temperature to Fahrenheit?" Confirm the SDK chained both tool calls automatically. Then turn off auto-calling and do the same thing manually with the loop from the previous lesson — you'll feel the difference.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.