Intro to Gemini 2.5 Flash

Source notebook

Repo path: gemini/getting-started/intro_gemini_2_5_flash.ipynb · Open on GitHub · intro

Introduces Gemini 2.5 Flash on Vertex AI with text, thinking, multimodal, tools, and structured output.

Summary

This notebook teaches how to use Gemini 2.5 Flash through the Google Gen AI SDK for Python. It walks from authentication and client setup through text generation, streaming, thinking configuration, chat, async calls, model parameters, system instructions, safety settings, multimodal inputs, controlled JSON output, token counting, grounding with Google Search, function calling, and code execution. It also includes reasoning examples for code generation and image-based math problems.

Key code patterns

Vertex AI client setup

from google import genai
 
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT")
LOCATION = "global"
client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION)

Creates a Google Gen AI SDK client backed by Vertex AI using a project and location.

Text generation

response = client.models.generate_content(
    model=MODEL_ID,
    contents="Roger has 5 tennis balls...",
)
display(Markdown(response.text))

Shows the basic request-response pattern and reads generated text from response.text.

Streaming generation

for chunk in client.models.generate_content_stream(
    model=MODEL_ID,
    contents="On average Joe throws 25 punches per minute...",
):
    output_text += chunk.text

Streams partial model output as chunks instead of waiting for the full response.

Thinking configuration

response = client.models.generate_content(
    model=MODEL_ID,
    contents="What are the practical implications of P vs. NP?",
    config=GenerateContentConfig(
        thinking_config=ThinkingConfig(thinking_budget=1024)
    ),
)

Controls reasoning budget for quality, speed, and latency tradeoffs.

Summarized thoughts

config=GenerateContentConfig(
    thinking_config=ThinkingConfig(include_thoughts=True)
)
for part in response.candidates[0].content.parts:
    if part.thought:
        display(Markdown(part.text))

Requests summarized thoughts and separates thought parts from final answer parts.

Multimodal input

response = client.models.generate_content(
    model=MODEL_ID,
    contents=[
        Part.from_uri(file_uri="gs://.../1706.03762v7.pdf", mime_type="application/pdf"),
        "Summarize the document.",
    ],
)

Uses Part.from_uri to send non-text inputs such as documents, audio, video, images, and web pages.

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),
)

Defines a response schema so model output follows a structured JSON shape.

Tools and 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),
)

Passes a Python function as a tool for automatic function calling.

Models & APIs used

  • Models: gemini-2.5-flash
  • APIs / services: Vertex AI, Gemini API, Google Search, Cloud Storage
  • SDKs / libraries: google-genai, pydantic

When to use this

Use this pattern to prototype Gemini 2.5 Flash applications on Vertex AI that combine generation, reasoning controls, multimodal inputs, tools, and structured outputs.

Gotchas & caveats

  • Gemini 2.5 Flash access on Gemini Enterprise Agent Platform is scheduled for removal for new and inactive projects on June 15, 2026, and model tuning is also scheduled to turn off on that date.
  • The notebook requires either a Google Cloud project with Vertex AI API enabled or a Vertex AI API key for Express Mode.
  • Colab users must authenticate with google.colab.auth.authenticate_user().
  • PROJECT_ID must be supplied or available from GOOGLE_CLOUD_PROJECT when using the Google Cloud project setup.
  • LOCATION is set to global, while the notebook links to supported Vertex AI locations for deployment considerations.
  • Setting thinking_budget to 0 disables thinking for lower-latency simpler tasks.
  • Safety response text can be None when blocked, and finish_reason can be SAFETY.
  • Web page URLs used with Part.from_uri must be publicly accessible.

Best practices

  • Use a Google Cloud project with Vertex AI API enabled for most users.
  • Use streaming generation when responses should appear as they are generated.
  • Set thinking_budget based on task complexity to manage quality and speed.
  • Set thinking_budget to 0 for simpler examples where lower latency is desired.
  • Inspect usage_metadata, thoughts_token_count, and total_token_count when evaluating thinking cost.
  • Use system_instruction to steer model behavior and guidelines.
  • Use safety_settings per request when content policy thresholds need adjustment.
  • Use response_schema and response_mime_type for controlled JSON generation.
  • Use count_tokens before sending requests when input size matters.
  • Use temperature=0 in function calling and code execution examples for more deterministic tool behavior.