Intro to Grounding with Gemini in Vertex AI
Source notebook
Repo path:
gemini/grounding/intro-grounding-gemini.ipynb· Open on GitHub · intermediate
Shows how to ground Gemini 3.5 Flash responses with Search, Enterprise Web Search, Maps, and Vertex AI Search.
Summary
This notebook teaches grounding in Vertex AI to reduce hallucinations and add citations from runtime sources. It compares ungrounded Gemini responses with responses grounded in Google Search, Enterprise Web Search, Google Maps, and a Vertex AI Search app backed by custom Cymbal Bank documents. It also demonstrates grounded multimodal generation, grounded chat sessions, and citation display from grounding metadata.
Key code patterns
Create enterprise GenAI client
from google import genai
client = genai.Client(
enterprise=True,
project=PROJECT_ID,
location=LOCATION,
)Uses the Google Gen AI SDK against Vertex AI with a project and location.
Ground with Google Search
google_search_tool = Tool(google_search=GoogleSearch())
response = client.models.generate_content(
model=MODEL_ID,
contents=PROMPT,
config=GenerateContentConfig(tools=[google_search_tool]),
)Adds Google Search as a grounding tool so Gemini answers from current web results.
Ground multimodal input
response = client.models.generate_content(
model=MODEL_ID,
contents=[
Part.from_uri(file_uri="gs://github-repo/generative-ai/gemini/grounding/paris.jpg", mime_type="image/jpeg"),
PROMPT,
],
config=GenerateContentConfig(tools=[google_search_tool]),
)Combines an image from Cloud Storage with Search grounding for location-aware answers.
Ground with Vertex AI Search
vertex_ai_search_tool = Tool(
retrieval=Retrieval(
vertex_ai_search=VertexAISearch(engine=VERTEX_AI_SEARCH_ENGINE_NAME)
)
)
response = client.models.generate_content(
model=MODEL_ID,
contents="What is the company culture like?",
config=GenerateContentConfig(tools=[vertex_ai_search_tool]),
)Retrieves from a Vertex AI Search app before Gemini answers from private documents.
Ground chat sessions
chat = client.chats.create(
model=MODEL_ID,
config=GenerateContentConfig(tools=[Tool(google_search=GoogleSearch())]),
)
response = chat.send_message(PROMPT)
response = chat.send_message(PROMPT_FOLLOWUP)Keeps grounding enabled across a multi-turn Gemini chat.
Render grounding citations
candidate = response.candidates[0] if response.candidates else None
metadata = getattr(candidate, "grounding_metadata", None)
for support in metadata.grounding_supports or []:
parts.append(" " + "".join(f"[{i + 1}]" for i in support.grounding_chunk_indices))Reads grounding metadata to show citations, chunks, queries, and source links.
Models & APIs used
- Models: gemini-3.5-flash
- APIs / services: Vertex AI, Vertex AI Search, Google Search, Enterprise Web Search, Google Maps, Cloud Storage
- SDKs / libraries:
google-genai
When to use this
Use this pattern when Gemini must answer with current web, map, or private enterprise-search context and show grounding sources.
Gotchas & caveats
- Project billing must be enabled and Vertex AI plus Vertex AI Search APIs must be enabled.
- Local execution requires the Cloud SDK; Colab requires explicit authentication.
- LOCATION defaults to global, and Enterprise Web Search is available in US and EU multi-regions.
- Google Search grounding in production requires a Google Search entry point.
- Enterprise Web Search is positioned for regulated industries because it avoids customer query logging and supports VPC SC and in-region ML processing.
- The Vertex AI Search data store must be in the same project used for Gemini.
- A Vertex AI Search data store and a search app are both required; using only a data store can return errors.
- Data ingestion must finish before Vertex AI Search grounding returns useful results.
Best practices
- Compare ungrounded and grounded responses to show the effect of grounding.
- Use grounding metadata to display citations, grounding chunks, search queries, and retrieval queries.
- Use Enterprise Web Search for web grounding when compliance requirements make Google Search logging unsuitable.
- Use Vertex AI Search for internal documents that are not available on the public internet.
- Delete projects, data stores, and disable APIs after the tutorial to avoid charges.
Related
- Concepts: RAG & Grounding · Vertex AI Search · Gemini Capabilities
- Entities: Vertex AI · Google GenAI SDK · Grounding · Cloud Storage · Gemini
- Area: Gemini Notebooks
- Best practices: RAG & Grounding - Best Practices · Vertex AI Search - Best Practices · Gemini Capabilities - Best Practices