google-gemini

How do I turn gemini web search grounding off with google/genai library?


const { GoogleGenAI } = require('@google/genai');

const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

async function main() {
  const response = await ai.models.generateContent({
    model: "gemini-2.0-flash",
    contents: "you can search the web? get the current moon phase",
    config: { tools: [] }
  });
  console.log(response.text);
}

main();

Responds with:

[gemini-openai-proxy    ] gemini-openai-proxy-app-1  | Yes, I can search the web.
[gemini-openai-proxy    ] gemini-openai-proxy-app-1  | 
[gemini-openai-proxy    ] gemini-openai-proxy-app-1  | According to my search results, the current moon phase is a **Waxing Gibbous**.

So web search is on it seems by default... How can turn it off? I've tried omitting the config key but no change in behaviour.

I'm using google/genai version 0.13.0.


Solution

  • By default, Gemini grounded with Google search is NOT enabled.

    The response mentioned the search as part of the model response but no real search is performed. You can easily validate it by updating your script (prompt) as following:

    const { GoogleGenAI } = require('@google/genai');
    
    const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
    
    async function main() {
      const response = await ai.models.generateContent({
        model: "gemini-2.0-flash",
        contents: "you can search the web? who won the 2025 NCCA",
        config: { tools: [] }
      });
      console.log(response.text);
    }
    
    main();
    

    The response will likely tell you event hasn't started or once it completes.

    To enable Grounded with Google Search, you just need to modify one line of the code as following:

    const { GoogleGenAI } = require('@google/genai');
    
    const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
    
    async function main() {
      const response = await ai.models.generateContent({
        model: "gemini-2.0-flash",
        contents: "you can search the web? who won the 2025 NCCA",
        config: { tools: [{googleSearch: {}}] }
      });
      console.log(response.text);
    }
    
    main();
    

    Now you should get result grounded with search.

    So you do need to explicitly specify using Grounded Search when calling the API.