# Grounding with Google search and live features
Ask Gemini "Who won the men's singles title at the most recent Australian Open?" with grounding off, and you may get a confident answer about a tournament that finished before the model's training cut. Turn grounding on, and Gemini issues a real Google Search, reads fresh results, and hands you the current answer with links you can click to verify. This lesson shows you exactly how that works, and how to enable it in the Gemini APIAPIApplication Programming Interface: a standardised interface that lets applications communicate and exchange data without knowing each other's internal workings.Voir la définition complète → and AI Studio.
A base model answers from its parameters. That knowledge is frozen at training time and has no concept of "today." For anything time-sensitive (prices, scores, news, leadership changes, product versions) the model is either stale or guessing.
Grounding connects the model to an external source of truth at inference time and forces it to base its response on what it retrieves. With grounding with Google Search, that source is live Google Search. Gemini decides when a query needs fresh facts, runs the search, and conditions its answer on the results.
The payoff is two things you cannot get from a raw model: freshness and citations. The returns the supporting search results and the text spans they back up, so you can show users where each claim came from.
Prompt: *"What is the latest stable version of the Gemini APIAPIApplication Programming Interface: a standardised interface that lets applications communicate and exchange data without knowing each other's internal workings.Voir la définition complète → SDK for Python, and when was it released?"*
Ungrounded answer: plausible-sounding version number, possibly wrong, no source, no date you can trust.
Grounded answer: the version reported by current search results, plus groundingChunks pointing at the official docs and release notes. If the answer is wrong, you can see *why* by inspecting the sources rather than re-promptingpromptingPrompt engineering is the practice of designing and refining text inputs to guide large language models toward accurate, relevant, and reliable outputs.Voir la définition complète → blindly.
This is the core mental shift: with grounding, a wrong answer becomes a *debuggable* answer.
Grounding is a tool you attach to a request. You do not change the prompt; you give the model the Google Search tool and let it decide when to call it.
from google import genai
from google.genai import types
client = genai.Client() # reads GEMINI_API_KEY from the environment
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Who won the most recent F1 Grand Prix, and what was the date?",
config=types.GenerateContentConfig(
tools=[types.Tool(google_search=types.GoogleSearch())]
),
)
print(response.text)
# Inspect what the model actually grounded on
metadata = response.candidates[0].grounding_metadata
for chunk in metadata.grounding_chunks:
print(chunk.web.title, "->", chunk.web.uri)A few things to notice:
google_search as a tool, alongside any function-calling tools you already use. The model chooses whether to invoke it per request.grounding_metadata block. That is where citations live, not in the prose.Get a key and try this in minutes at aistudio.google.com. The full tool reference is in the Google Search grounding docs.
grounding_metadata gives you three useful structures:
grounding_chunks: the sources (title + URI). These are your citations.grounding_supports: maps spans of the answer text to the chunks that support them. This is how you build per-sentence footnotes.web_search_queries: the actual queries Gemini sent to Search. Great for debugging when an answer looks off; you can see what it searched for.If you display grounded answers to users, Google's terms require you to show the search suggestions returned in the response (searchEntryPoint). Render it. It is not optional decoration.
entry = response.candidates[0].grounding_metadata.search_entry_point
if entry and entry.rendered_content:
html_to_show_in_ui = entry.rendered_contentIn AI Studio, open a chat prompt and toggle Grounding with Google Search in the right-hand tools panel. Send your time-sensitive question and watch the response come back with a sources strip underneath.
This is the fastest way to *compare* behavior: run the same prompt with the toggle off, then on. The contrast makes the value obvious, and the "Get code" button exports the exact request (including the tool config above) so you can lift it straight into your app.
You already know RAG conceptually. Grounding with Search is not a replacement for it. Choose by where the truth lives:
Many production assistants use both: RAG for company knowledge, Search grounding for the open web. They compose cleanly because both are just tools the model can call.
Grounding makes answers *fresh*. The Live APIAPIApplication Programming Interface: a standardised interface that lets applications communicate and exchange data without knowing each other's internal workings.Voir la définition complète → makes interaction *real-time*. It is a different capability, and they are often confused, so be precise:
The Live APIAPIApplication Programming Interface: a standardised interface that lets applications communicate and exchange data without knowing each other's internal workings.Voir la définition complète → opens a persistent, low-latency, bidirectional stream between your app and Gemini. Instead of "send prompt, wait for full response," you stream audio (and optionally video frames) *in* and receive audio and text *out* as it is generated. The user can interrupt mid-sentence, and the model handles it. This is what powers natural voice conversations, live screen-sharing help, and real-time tutoring.
Key properties:
import asyncio
from google import genai
client = genai.Client()
MODEL = "gemini-2.5-flash-native-audio-preview-09-2025"
async def main():
config = {"response_modalities": ["AUDIO"]}
async with client.aio.live.connect(model=MODEL, config=config) as session:
await session.send_client_content(
turns={"parts": [{"text": "In one sentence, what's the weather typically like in Lisbon in May?"}]}
)
async for message in session.receive():
if message.data:
play_audio_chunk(message.data) # your audio sink
asyncio.run(main())The exact model names for live/native-audio evolve, so check the Live API docs for the current preview model before you build. The shape of the code (connect, send, async-iterate over streamed messages) is stable.
Use the Live APIAPIApplication Programming Interface: a standardised interface that lets applications communicate and exchange data without knowing each other's internal workings.Voir la définition complète → when the *interaction* is the product:
If you only need a fast text answer, you do not need the Live APIAPIApplication Programming Interface: a standardised interface that lets applications communicate and exchange data without knowing each other's internal workings.Voir la définition complète →. Plain generate_content (with streaming output if you want tokenstokensA token is the basic unit of text that language models process, often a word fragment, whole word, or punctuation mark rather than a single character.Voir la définition complète → as they arrive) is simpler and cheaper.
Vérification des acquis
1. What core problem does grounding with Google Search solve for a language model?
2. The lesson describes a 'core mental shift' when using grounding. What is it?
3. According to the lesson, how do you enable grounding in a Gemini API request?
4. Select ALL benefits that grounding with Google Search provides which a raw, ungrounded model cannot.
Sélectionnez toutes les réponses correctes.
5. Select ALL situations where enabling grounding is clearly preferable to relying on the base model alone.
Sélectionnez toutes les réponses correctes.
The two features combine into something genuinely useful: a real-time assistant that speaks, listens, and answers from live facts. You open a Live session, enable the Search tool in the session config, and the model grounds spoken answers automatically.
config = {
"response_modalities": ["AUDIO"],
"tools": [{"google_search": {}}],
}
# Inside the live session, a spoken question like
# "What did the markets do today?" now triggers a real Search,
# and the spoken answer is grounded in live results.That single tools line is the difference between an assistant that confidently makes up today's market move and one that actually checks. Same pattern as the non-live APIAPIApplication Programming Interface: a standardised interface that lets applications communicate and exchange data without knowing each other's internal workings.Voir la définition complète →: grounding is a tool, and the model decides when to use it.
A few things that separate a demo from something you ship:
grounding_chunks in the UI.searchEntryPoint) when required by Google's grounding terms.grounding_metadata is present before claiming "this is verified."For enterprise deployments, the same grounding concept lives in Vertex AI, with grounding on Google Search *and* grounding on your own enterprise data, plus the governance, logging, and IAM controls a regulated org needs. If your assistant graduates from prototype to a production system inside Google Cloud, that is the path: see cloud.google.com/vertex-ai. The APIAPIApplication Programming Interface: a standardised interface that lets applications communicate and exchange data without knowing each other's internal workings.Voir la définition complète → surface is close enough that prototypes built in AI Studio port over without a rewrite of your core logic.
google_search to your request; the model decides when to search and returns citations in grounding_metadata. Read and display those sources.Ces actions sont compilées dans le plan d'action du rôle.