Getting started with Gemini using Vertex AI in Express Mode
Source notebook
Repo path:
gemini/getting-started/intro_gemini_express.ipynb· Open on GitHub · intro
Introduces Gemini on Vertex AI Express Mode using the Google Gen AI SDK.
Summary
This notebook teaches how to call Gemini through Vertex AI in Express Mode with an API key. It demonstrates text generation, streaming, chat, async calls, model configuration, system instructions, safety settings, multimodal inputs, controlled JSON output, token counting, Google Search grounding, function calling, and code execution.
Key code patterns
Create Express Mode client
from google import genai
client = genai.Client(
enterprise=True,
api_key=API_KEY,
)Authenticates to Vertex AI Express Mode using an API key.
Generate text
response = client.models.generate_content(
model=MODEL_ID,
contents="What's the largest planet in our solar system?",
)
response.textShows the basic request-response pattern for Gemini text generation.
Stream content
for chunk in client.models.generate_content_stream(
model=MODEL_ID,
contents="Tell me a story...",
):
output_text += chunk.textStreams chunks as they are generated to reduce perceived latency.
Multi-turn chat
chat = client.chats.create(model=MODEL_ID)
response = chat.send_message("Write a function...")
response = chat.send_message("Write a unit test...")Preserves conversation context across turns.
Configure generation
config=GenerateContentConfig(
temperature=0.4,
top_p=0.95,
top_k=20,
max_output_tokens=100,
seed=5,
)Controls sampling, length, reproducibility, and stopping behavior.
Send multimodal input
response = client.models.generate_content(
model=MODEL_ID,
contents=[
Part.from_bytes(data=image, mime_type="image/png"),
"Write a short blog post...",
],
)Combines media parts and text prompts in one Gemini request.
Controlled JSON output
response = client.models.generate_content(
model=MODEL_ID,
contents="List cookie recipes.",
config=GenerateContentConfig(
response_mime_type="application/json",
response_schema=Recipe,
),
)Constrains model output to a declared schema and exposes parsed results.
Tool use
google_search_tool = Tool(google_search=GoogleSearch())
response = client.models.generate_content(
model=MODEL_ID,
contents="When is the next total solar eclipse...",
config=GenerateContentConfig(tools=[google_search_tool]),
)Adds Google Search as a grounding tool that Gemini can use at runtime.
Models & APIs used
- Models: gemini-3.5-flash
- APIs / services: Vertex AI, Gemini API
- SDKs / libraries:
google-genai,pydantic
When to use this
Use this pattern to quickly prototype Gemini features on Vertex AI with an API key before building a fuller application.
Gotchas & caveats
- An API key is required for Vertex AI Express Mode, either inline or from GOOGLE_API_KEY.
- compute_tokens is only supported in Vertex AI.
- Public URLs are required when sending web pages by URI.
- Lower media_resolution can reduce processing time and cost but may affect output quality.
- Safety response text can be None when content is blocked, with finish_reason set to SAFETY.
Best practices
- Use streaming responses when reducing perceived latency matters.
- Use system instructions to steer model behavior for a task.
- Inspect finish_reason and safety_ratings when using safety settings.
- Use response_schema and response_mime_type for structured JSON output.
- Use count_tokens before sending a request to estimate input token usage.
- Set temperature to 0 for deterministic tool and function 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