Since April 2026, Gemini 2.0 Flash has an intermittent bug: google_search grounding returns empty results or refuses to trigger entirely. The March 2026 image grounding regression made it worse. Flash does not reliably invoke search, especially when external tools are also registered. This tutorial builds a detection-and-fallback layer so your agent always gets search results.
Prerequisites
- Google AI Studio or Vertex AI credentials
- Scavio API key
- Python 3.8+
Walkthrough
Step 1: Call Gemini with google_search grounding
Set up the standard Gemini call with grounding enabled.
import google.generativeai as genai
import os
genai.configure(api_key=os.environ['GEMINI_API_KEY'])
model = genai.GenerativeModel('gemini-2.0-flash',
tools=[genai.Tool(google_search=genai.GoogleSearch())])
def gemini_search(query):
response = model.generate_content(query)
return responseStep 2: Detect empty or failed search results
Check if Gemini actually performed a search grounding call. The April 2026 bug returns candidates with no grounding metadata.
def search_failed(response):
# No grounding metadata = search did not trigger
if not hasattr(response, 'candidates') or not response.candidates:
return True
candidate = response.candidates[0]
grounding = getattr(candidate, 'grounding_metadata', None)
if not grounding or not grounding.grounding_chunks:
return True
# Empty chunks = search triggered but returned nothing
return len(grounding.grounding_chunks) == 0Step 3: Route to Scavio on failure
When Gemini search fails, extract the query and call Scavio.
import requests
H = {'x-api-key': os.environ['SCAVIO_API_KEY']}
def search_with_fallback(query):
response = gemini_search(query)
if search_failed(response):
results = requests.post('https://api.scavio.dev/api/v1/search',
headers=H,
json={'platform': 'google', 'query': query}).json()
return {'source': 'scavio', 'results': results}
return {'source': 'gemini', 'response': response}Step 4: Log failure rates for monitoring
Track how often Gemini search fails so you know when Google fixes the bug.
import json, datetime
def log_search_event(query, source, failed):
with open('search_fallback_log.jsonl', 'a') as f:
f.write(json.dumps({
'ts': datetime.datetime.now().isoformat(),
'query': query, 'source': source, 'gemini_failed': failed
}) + '\n')Python Example
import os, requests
import google.generativeai as genai
genai.configure(api_key=os.environ['GEMINI_API_KEY'])
model = genai.GenerativeModel('gemini-2.0-flash',
tools=[genai.Tool(google_search=genai.GoogleSearch())])
H = {'x-api-key': os.environ['SCAVIO_API_KEY']}
def search(query):
resp = model.generate_content(query)
grounding = getattr(resp.candidates[0], 'grounding_metadata', None) if resp.candidates else None
if not grounding or not grounding.grounding_chunks:
return requests.post('https://api.scavio.dev/api/v1/search',
headers=H, json={'platform': 'google', 'query': query}).json()
return respJavaScript Example
const res = await fetch('https://api.scavio.dev/api/v1/search', {
method: 'POST',
headers: {'x-api-key': process.env.SCAVIO_API_KEY, 'Content-Type': 'application/json'},
body: JSON.stringify({platform: 'google', query: userQuery})
});
const fallbackResults = await res.json();Expected Output
Agent that always returns search results: Gemini grounding when it works, Scavio API when Gemini fails. JSONL log of failure rates for monitoring.