Skip to main content

Search Grounding with Gemini

Search grounding is a powerful feature in the Gemini API that allows you to enhance your model's responses by providing relevant search results as context.

See Live Demo built using these code snippets

import {
DynamicRetrievalMode,
GoogleGenerativeAI,
} from "@google/generative-ai";

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const model = genAI.getGenerativeModel(
{
model: "models/gemini-2.0-flash",
}
);

const result = await model.generateContent({
contents: [
{
role: "user",
parts: [{ text: searchQuery }],
},
],
tools: [
{
google_search: {},
},
],
});

console.log(result.response.candidates[0].groundingMetadata);