Intro to Gemini 2.5 Pro
Source notebook
Repo path:
gemini/getting-started/intro_gemini_2_5_pro.ipynb· Open on GitHub · intermediate
Introduces Gemini 2.5 Pro on Vertex AI using the Google Gen AI SDK for text, multimodal, tools, and reasoning.
Summary
This notebook teaches how to call Gemini 2.5 Pro with the Google Gen AI SDK on Vertex AI. It walks through setup, text generation, thinking configuration, streaming, system instructions, safety settings, chat, async calls, multimodal inputs, controlled JSON output, token counting, grounding with Google Search, function calling, and code execution.
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 Gen AI SDK client for Vertex AI project-based authentication.
Generate text
response = client.models.generate_content(
model=MODEL_ID,
contents="What's the largest planet in our solar system?"
)
print(response.text)Shows the basic Gemini request pattern and reading generated text from the response.
Configure thinking
response = client.models.generate_content(
model=MODEL_ID,
contents="How many R's are in the word strawberry?",
config=GenerateContentConfig(
thinking_config=ThinkingConfig(thinking_budget=1024)
),
)Demonstrates controlling reasoning token budget for quality and latency tradeoffs.
Stream thoughts and answer
for chunk in client.models.generate_content_stream(
model=MODEL_ID,
contents=prompt,
config=GenerateContentConfig(
thinking_config=ThinkingConfig(
thinking_budget=1024,
include_thoughts=True,
)
),
):
for part in chunk.candidates[0].content.parts:
print(part.text)Shows streaming generation with summarized thought parts and final answer parts.
Multimodal URI input
response = client.models.generate_content(
model=MODEL_ID,
contents=[
Part.from_uri(file_uri=image_file_url, mime_type="image/png"),
"What's the area of the overlapping region?",
],
)Uses Part.from_uri to combine image input with a text prompt.
Controlled JSON output
class Recipe(BaseModel):
name: str
description: str
ingredients: list[str]
response = client.models.generate_content(
model=MODEL_ID,
contents="List a few popular cookie recipes.",
config=GenerateContentConfig(
response_mime_type="application/json",
response_schema=Recipe,
),
)Constrains model output to a Pydantic schema and accesses parsed results.
Ground with Google Search
google_search_tool = Tool(google_search=GoogleSearch())
response = client.models.generate_content(
model=MODEL_ID,
contents="What is the current temperature in Austin, TX?",
config=GenerateContentConfig(tools=[google_search_tool]),
)Adds Google Search as a tool so Gemini can ground answers in runtime web results.
Function calling
def get_current_weather(location: str) -> str:
return weather_map.get(location, "unknown")
response = client.models.generate_content(
model=MODEL_ID,
contents="What is the weather like in San Francisco?",
config=GenerateContentConfig(
tools=[get_current_weather],
temperature=0,
),
)Demonstrates automatic Python function calling from a model request.
Code execution tool
code_execution_tool = Tool(code_execution=ToolCodeExecution())
response = client.models.generate_content(
model=MODEL_ID,
contents="Calculate 20th fibonacci number.",
config=GenerateContentConfig(
tools=[code_execution_tool],
temperature=0,
),
)Lets Gemini generate and execute Python code to solve code-based reasoning tasks.
Models & APIs used
- Models: gemini-2.5-pro
- APIs / services: Vertex AI, Gemini API, Google Search
- SDKs / libraries:
google-genai,pydantic
When to use this
Use this pattern to quickly learn and prototype Gemini 2.5 Pro features on Vertex AI with the Google Gen AI SDK.
Gotchas & caveats
- Requires google-genai installation.
- Colab users must authenticate with google.colab.auth.authenticate_user().
- Project-based setup requires enabling the Vertex AI API.
- Notebook uses LOCATION = “global”.
- Authentication uses either a Google Cloud project or Vertex AI API key, but the tutorial uses a Google Cloud project.
- Thinking budget defaults up to 8192 tokens; explicit supported range shown is 128 to 32768 tokens.
- Safety settings are stated as OFF by default with default block thresholds BLOCK_NONE.
- Public URL input requires the URL to be publicly accessible.
- For remaining examples, the notebook sets thinking_budget=128 to reduce latency.
Best practices
- Use a Google Cloud Project for authentication for most users.
- Set thinking_budget to control quality and speed of response.
- Inspect usage_metadata token counts when evaluating thinking budget behavior.
- Use include_thoughts only when summarized thoughts are needed.
- Use lower thinking budget for examples that do not need extra reasoning to reduce latency.
- Use system_instruction to steer model behavior and guidelines.
- Check finish_reason and safety_ratings when safety filters may block a response.
- Use response_schema and response_mime_type for controlled JSON output.
- Use count_tokens before sending requests when input token size matters.
- Set temperature=0 for deterministic tool-calling examples.
Related
- Concepts: Getting Started · Gemini Capabilities · Function Calling & Tools
- Entities: Vertex AI · Google GenAI SDK · Grounding · Function Calling · Gemini
- Area: Gemini Notebooks
- Best practices: Getting Started - Best Practices · Gemini Capabilities - Best Practices · Function Calling & Tools - Best Practices