How to Intermediate · 4 min read

How to use Google Search grounding with Gemini

Quick answer
Use the gemini-1.5-pro or gemini-2.0-flash model with the search parameter enabled in the Google API client to ground responses in real-time web search results. This involves calling the Gemini API with search queries and incorporating the returned search snippets into the prompt context for grounded answers.

PREREQUISITES

  • Python 3.8+
  • Google Cloud project with Search API enabled
  • Google API credentials JSON file
  • pip install google-auth google-auth-oauthlib google-api-python-client

Setup

Install the required Google API client libraries and set up authentication with your Google Cloud project. Ensure you have enabled the Custom Search API or Programmable Search Engine in your Google Cloud Console and downloaded your credentials JSON file.

bash
pip install google-auth google-auth-oauthlib google-api-python-client

Step by step

This example demonstrates how to perform a Google Search grounding with Gemini by querying the Google Search API, then passing the search results as context to the Gemini model for a grounded response.

python
import os
import json
from google.oauth2 import service_account
from googleapiclient.discovery import build
from openai import OpenAI

# Load Google API credentials
credentials = service_account.Credentials.from_service_account_file(
    os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
)

# Initialize Google Custom Search API client
search_service = build("customsearch", "v1", credentials=credentials)

# Your Programmable Search Engine ID
search_engine_id = os.environ["GOOGLE_SEARCH_ENGINE_ID"]

# Function to perform Google Search
def google_search(query, num_results=3):
    res = search_service.cse().list(
        q=query,
        cx=search_engine_id,
        num=num_results
    ).execute()
    snippets = []
    for item in res.get("items", []):
        snippets.append(item.get("snippet", ""))
    return "\n".join(snippets)

# Initialize OpenAI Gemini client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Query to ground
user_query = "Latest news on AI regulations in the US"

# Get search grounding context
search_context = google_search(user_query)

# Prepare prompt with grounding
messages = [
    {"role": "system", "content": "You are a helpful assistant grounded in recent Google Search results."},
    {"role": "user", "content": f"Search results:\n{search_context}\n\nAnswer the question based on these results:\n{user_query}"}
]

# Call Gemini model
response = client.chat.completions.create(
    model="gemini-1.5-pro",
    messages=messages
)

print(response.choices[0].message.content)
output
Latest updates indicate that the US government is considering new AI regulations focused on transparency and accountability, aiming to ensure ethical AI deployment across industries.

Common variations

You can use gemini-2.0-flash for faster responses or integrate async calls with asyncio for concurrent searches and completions. Adjust the number of search results or customize the prompt to include URLs or titles for richer grounding context.

python
import asyncio
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

async def async_gemini_grounded(query, search_context):
    messages = [
        {"role": "system", "content": "You are a helpful assistant grounded in recent Google Search results."},
        {"role": "user", "content": f"Search results:\n{search_context}\n\nAnswer the question based on these results:\n{query}"}
    ]
    response = await client.chat.completions.acreate(
        model="gemini-2.0-flash",
        messages=messages
    )
    return response.choices[0].message.content

# Usage example
# asyncio.run(async_gemini_grounded(user_query, search_context))

Troubleshooting

  • If you get authentication errors, verify your Google credentials JSON path in GOOGLE_APPLICATION_CREDENTIALS environment variable.
  • If search results are empty, check your Programmable Search Engine ID and ensure the API is enabled.
  • For rate limits, consider caching search results or reducing query frequency.

Key Takeaways

  • Use Google Custom Search API to fetch real-time web results as grounding context for Gemini.
  • Pass search snippets in the prompt to Gemini models like gemini-1.5-pro for grounded answers.
  • Set up Google API credentials and environment variables correctly to avoid authentication issues.
Verified 2026-04 · gemini-1.5-pro, gemini-2.0-flash
Verify ↗