import subprocess, sys
subprocess.check_call(
[sys.executable, "-m", "pip", "install", "-qU", "google-genai"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
import getpass, json, textwrap, os, time
from google import genai
from google.genai import varieties
if "GOOGLE_API_KEY" not in os.environ:
os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter your Gemini API key: ")
shopper = genai.Consumer(api_key=os.environ["GOOGLE_API_KEY"])
TOOL_COMBO_MODEL = "gemini-3-flash-preview"
MAPS_MODEL = "gemini-2.5-flash"
DIVIDER = "=" * 72
def heading(title: str):
print(f"n{DIVIDER}")
print(f" {title}")
print(DIVIDER)
def wrap(textual content: str, width: int = 80):
for line in textual content.splitlines():
print(textwrap.fill(line, width=width) if line.strip() else "")
def describe_parts(response):
elements = response.candidates[0].content material.elements
fc_ids = {}
for i, half in enumerate(elements):
prefix = f" Part {i:2d}:"
if hasattr(half, "tool_call") and half.tool_call:
tc = half.tool_call
print(f"{prefix} [toolCall] type={tc.tool_type} id={tc.id}")
if hasattr(half, "tool_response") and half.tool_response:
tr = half.tool_response
print(f"{prefix} [toolResponse] type={tr.tool_type} id={tr.id}")
if hasattr(half, "executable_code") and half.executable_code:
code = half.executable_code.code[:90].substitute("n", " ↵ ")
print(f"{prefix} [executableCode] {code}...")
if hasattr(half, "code_execution_result") and half.code_execution_result:
out = (half.code_execution_result.output or "")[:90]
print(f"{prefix} [codeExecResult] {out}")
if hasattr(half, "function_call") and half.function_call:
fc = half.function_call
fc_ids[fc.name] = fc.id
print(f"{prefix} [functionCall] name={fc.name} id={fc.id}")
print(f" └─ args: {dict(fc.args)}")
if hasattr(half, "text") and half.textual content:
snippet = half.textual content[:110].substitute("n", " ")
print(f"{prefix} [text] {snippet}...")
if hasattr(half, "thought_signature") and half.thought_signature:
print(f" └─ thought_signature present ✓")
return fc_ids
heading("DEMO 1: Combine Google Search + Custom Function in One Request")
print("""
This demo reveals the flagship new function: passing BOTH a built-in instrument
(Google Search) and a customized operate declaration in a single API name.
Gemini will:
Flip 1 → Search the online for real-time information, then request our customized
operate to get climate information.
Flip 2 → We provide the operate response; Gemini synthesizes the whole lot.
Key factors:
• google_search and function_declarations go within the SAME Instrument object
• include_server_side_tool_invocations have to be True (on ToolConfig)
• Return ALL elements (incl. thought_signatures) in subsequent turns
""")
get_weather_func = varieties.FunctionDeclaration(
title="getWeather",
description="Gets the current weather for a requested city.",
parameters=varieties.Schema(
kind="OBJECT",
properties={
"city": varieties.Schema(
kind="STRING",
description="The city and state, e.g. Utqiagvik, Alaska",
),
},
required=["city"],
),
)
print("▶ Turn 1: Sending prompt with Google Search + getWeather tools...n")
response_1 = shopper.fashions.generate_content(
mannequin=TOOL_COMBO_MODEL,
contents=(
"What is the northernmost city in the United States? "
"What's the weather like there today?"
),
config=varieties.GenerateContentConfig(
instruments=[
types.Tool(
google_search=types.GoogleSearch(),
function_declarations=[get_weather_func],
),
],
tool_config=varieties.ToolConfig(
include_server_side_tool_invocations=True,
),
),
)
print(" Parts returned by the model:n")
fc_ids = describe_parts(response_1)
function_call_id = fc_ids.get("getWeather")
print(f"n ✅ Captured function_call id for getWeather: {function_call_id}")
print("n▶ Turn 2: Returning function result & requesting final synthesis...n")
historical past = [
types.Content(
role="user",
parts=[
types.Part(
text=(
"What is the northernmost city in the United States? "
"What's the weather like there today?"
)
)
],
),
response_1.candidates[0].content material,
varieties.Content material(
function="user",
elements=[
types.Part(
function_response=types.FunctionResponse(
name="getWeather",
response={"response": "Very cold. 22°F / -5.5°C with strong Arctic winds."},
id=function_call_id,
)
)
],
),
]
response_2 = shopper.fashions.generate_content(
mannequin=TOOL_COMBO_MODEL,
contents=historical past,
config=varieties.GenerateContentConfig(
instruments=[
types.Tool(
google_search=types.GoogleSearch(),
function_declarations=[get_weather_func],
),
],
tool_config=varieties.ToolConfig(
include_server_side_tool_invocations=True,
),
),
)
print(" ✅ Final synthesized response:n")
for half in response_2.candidates[0].content material.elements:
if hasattr(half, "text") and half.textual content:
wrap(half.textual content)Subscribe to Updates
Get the latest tech insights from TechnologiesDigest.com on AI, innovation, and the future of digital technology.
Trending
- Is Bitcoin Nonetheless A Sovereign Device?
- April Patch Tuesday Fixes Essential Flaws Throughout SAP, Adobe, Microsoft, Fortinet, and Extra
- NotebookLM for the Inventive Architect
- It’s tax day and the IRS is providing extra assist to extension filers
- A Blueprint For the Way forward for City Dwelling: Powering up the Good Cities of Tomorrow
- Stereotaxis to amass Robocath for as much as $45M
- The right way to Maximize Claude Cowork
- AWS Interconnect is now typically accessible, with a brand new choice to simplify last-mile connectivity



