Grounding & Search
Gemini Grounding API - Enhance AI responses with real-time Google Search data. Build fact-checked, up-to-date AI applications with web search integration.
Grounding with Google Search
Grounding connects Gemini to real-time information from Google Search, ensuring responses are based on current, factual data rather than just training knowledge.
Supported Models: Grounding is available on Gemini 1.5+ models.
How It Works
- Enable Google Search grounding
- Model queries Google Search for relevant information
- Responses cite sources from search results
- Grounding metadata shows which sources were used
Enable Grounding
curl -X POST "https://api.uniterouter.ai/v1beta/models/gemini-2.0-flash:generateContent" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $UNITEROUTER_API_KEY" \
-d '{
"contents": [
{
"role": "user",
"parts": [{"text": "What are the latest developments in AI regulation?"}]
}
],
"tools": [
{
"googleSearch": {}
}
]
}'import google.generativeai as genai
genai.configure(
api_key="your_uniterouter_api_key",
transport="rest",
client_options={"api_endpoint": "api.uniterouter.ai"}
)
model = genai.GenerativeModel(
"gemini-2.0-flash",
tools=["google_search"]
)
response = model.generate_content(
"What are the latest developments in AI regulation?"
)
print(response.text)
# Access grounding metadata
if response.candidates[0].grounding_metadata:
for chunk in response.candidates[0].grounding_metadata.grounding_chunks:
if chunk.web:
print(f"Source: {chunk.web.title} - {chunk.web.uri}")import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("your_uniterouter_api_key", {
baseUrl: "https://api.uniterouter.ai"
});
const model = genAI.getGenerativeModel({
model: "gemini-2.0-flash",
tools: [{ googleSearch: {} }]
});
const result = await model.generateContent(
"What are the latest developments in AI regulation?"
);
console.log(result.response.text());
// Access grounding metadata
const metadata = result.response.candidates?.[0]?.groundingMetadata;
if (metadata?.groundingChunks) {
for (const chunk of metadata.groundingChunks) {
if (chunk.web) {
console.log(`Source: ${chunk.web.title} - ${chunk.web.uri}`);
}
}
}Response with Grounding
{
"candidates": [{
"content": {
"parts": [{
"text": "Recent developments in AI regulation include..."
}],
"role": "model"
},
"groundingMetadata": {
"searchEntryPoint": {
"renderedContent": "<search results HTML>"
},
"groundingChunks": [
{
"web": {
"uri": "https://example.com/ai-regulation",
"title": "AI Regulation Update 2024"
}
}
],
"groundingSupports": [
{
"segment": {
"startIndex": 0,
"endIndex": 100
},
"groundingChunkIndices": [0],
"confidenceScores": [0.95]
}
],
"webSearchQueries": ["AI regulation 2024 latest news"]
}
}]
}Grounding Metadata
| Field | Description |
|---|---|
searchEntryPoint | Rendered search results |
groundingChunks | Sources used (web pages) |
groundingSupports | Which response parts cite which sources |
webSearchQueries | Queries sent to Google Search |
retrievalMetadata | Additional retrieval info |
Dynamic Retrieval
Control when grounding is used:
{
"tools": [{
"googleSearch": {
"dynamicRetrievalConfig": {
"mode": "MODE_DYNAMIC",
"dynamicThreshold": 0.5
}
}
}]
}| Mode | Description |
|---|---|
MODE_UNSPECIFIED | Default behavior |
MODE_DYNAMIC | Model decides based on query |
Use Cases
| Use Case | Example Query |
|---|---|
| Current Events | "What happened in the news today?" |
| Fact Checking | "What is the population of Tokyo?" |
| Research | "Latest research on quantum computing" |
| Product Info | "When was iPhone 15 released?" |
| Sports | "Who won the last World Cup?" |
Google Maps Grounding
For location-based queries:
model = genai.GenerativeModel(
"gemini-2.0-flash",
tools=["google_maps"]
)
response = model.generate_content(
"Find Italian restaurants near Times Square"
)Combining Grounding with Other Tools
model = genai.GenerativeModel(
"gemini-2.0-flash",
tools=[
"google_search",
"code_execution",
{
"function_declarations": [...]
}
]
)Best Practices
- Use for current info - Grounding is best for time-sensitive queries
- Check citations - Verify important claims against sources
- Combine tools - Use with code execution for data analysis
- Dynamic mode - Let model decide when to search for efficiency
Limitations
- Search quota limits may apply
- Some queries may not trigger search
- Results depend on Google Search availability
Related
- Generate Content - Basic generation
- Function Calling - Custom tools
- Code Execution - Run Python code
How is this guide?
Code Execution
Gemini Code Execution API - Let Gemini 2.0 write and run Python code. Solve math, data analysis, and complex problems with AI-generated executable code.
Embed Content POST
Gemini Embeddings API - Generate text-embedding vectors with Google's embedding models. Build semantic search, RAG, and similarity matching applications.