Gemini API: Getting started with information grounding for Gemini models

In this notebook you will learn how to use information grounding with Gemini models.

Information grounding is the process of connecting these models to specific, verifiable information sources to enhance the accuracy, relevance, and factual correctness of their responses. While LLMs are trained on vast amounts of data, this knowledge can be general, outdated, or lack specific context for particular tasks or domains. Grounding helps to bridge this gap by providing the LLM with access to curated, up-to-date information.

Here you will experiment with:

Setup

Install the Google GenAI SDK

Install the Google GenAI SDK from npm.

$ npm install @google/genai

Setup your API key

You can create your API key using Google AI Studio with a single click.

Remember to treat your API key like a password. Don’t accidentally save it in a notebook or source file you later commit to GitHub. In this notebook we will be storing the API key in a .env file. You can also set it as an environment variable or use a secret manager.

Here’s how to set it up in a .env file:

$ touch .env
$ echo "GEMINI_API_KEY=<YOUR_API_KEY>" >> .env
Tip

Another option is to set the API key as an environment variable. You can do this in your terminal with the following command:

$ export GEMINI_API_KEY="<YOUR_API_KEY>"

Load the API key

To load the API key from the .env file, we will use the dotenv package. This package loads environment variables from a .env file into process.env.

$ npm install dotenv

Then, we can load the API key in our code:

const dotenv = require("dotenv") as typeof import("dotenv");

dotenv.config({
  path: "../.env",
});

const GEMINI_API_KEY = process.env.GEMINI_API_KEY ?? "";
if (!GEMINI_API_KEY) {
  throw new Error("GEMINI_API_KEY is not set in the environment variables");
}
console.log("GEMINI_API_KEY is set in the environment variables");
GEMINI_API_KEY is set in the environment variables
Note

In our particular case the .env is is one directory up from the notebook, hence we need to use ../ to go up one directory. If the .env file is in the same directory as the notebook, you can omit it altogether.

│
├── .env
└── quickstarts
    └── Grounding.ipynb

Initialize SDK Client

With the new SDK, now you only need to initialize a client with you API key (or OAuth if using Vertex AI). The model is now set in each call.

const google = require("@google/genai") as typeof import("@google/genai");

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

Select a model

Now select the model you want to use in this guide, either by selecting one in the list or writing it down. Keep in mind that some models, like the 2.5 ones are thinking models and thus take slightly more time to respond (cf. thinking notebook for more details and in particular learn how to switch the thiking off).

const tslab = require("tslab") as typeof import("tslab");

const MODEL_ID = "gemini-2.5-flash-preview-05-20";

Use Google Search grounding

Google Search grounding is particularly useful for queries that require current information or external knowledge. Using Google Search, Gemini can access nearly real-time information and better responses.

const search_grounding = await ai.models.generateContent({
  model: MODEL_ID,
  contents: "What was the latest Indian Premier League match and who won?",
  config: {
    tools: [{ googleSearch: {} }],
  },
});

tslab.display.markdown(search_grounding.text ?? "");

console.log(
  "Search Query:",
  JSON.stringify(search_grounding.candidates?.[0]?.groundingMetadata?.webSearchQueries, null, 2)
);
console.log(
  "Search Pages:",
  JSON.stringify(search_grounding.candidates?.[0]?.groundingMetadata?.groundingChunks, null, 2)
);

tslab.display.html(search_grounding.candidates?.[0]?.groundingMetadata?.searchEntryPoint?.renderedContent ?? "");

The latest Indian Premier League (IPL) match was the IPL 2025 Final, played on June 3, 2025, in Ahmedabad.

Royal Challengers Bengaluru (RCB) won the match against Punjab Kings (PBKS) by 6 runs, securing their maiden IPL title. Virat Kohli was emotional after RCB’s historic win. Krunal Pandya was named the Man of the Match for his economical bowling performance.

Search Query: [
  "latest Indian Premier League match",
  "who won the latest IPL match",
  "IPL 2025 latest match"
]
Search Pages: [
  {
    "web": {
      "uri": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQHt5kdqdKChCOa4Pr_VPq-gkhoamwHE_iJ7o5RjLXIZzeI8TlkXQAR1XQHWyRNevZPTZteSGYR_ktkeCXuaJNMzEtiY1vDOiYAqqwRHmf-0khw3lJECXDBYSMcf6EWuAY1ClUwt7u3TE77DH8qSQYstjGe7dmNu8vUX",
      "title": "indiatimes.com"
    }
  },
  {
    "web": {
      "uri": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQFCh5VcEGh9Ppqbg8BuGhf2X6LdqlJu9t5zENDQxiarqynpQBAcWuAJgCxLJxkhc223LqJDgX5VVrqUD-9LLks_RJT8OUwTLUnpLH2Ik8doMHLfo54eI5AlWXNiWQHqMXCbmJRBbhBIu1sUuyuS4O7h9SR4VQvO4aun6b--Z2Tci0mGK_1VAJA_Nq-3_FNih6pXFq3Fv1Q8aBCc9ej_pB6g9M4f6EcWKKO2hs-oaKzR3BefFnnWrKYSri7iel8au4UEcPvgkCPBjdLPz-yFkM1apJvTvbUduUXfhGCvDuRDwfFEsoyT_7IdM9YhaXo=",
      "title": "hindustantimes.com"
    }
  },
  {
    "web": {
      "uri": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQGvvHqeHxsXknsFovCD7wo_veKNQ6iYuWG9DskR4DJ6snVn3D8SnMM87g4L18UaMAO7r2Awtm38AOyarUmOmJl0-8WEOKoRokRnclqUow5wXIXYiEkppq8QXHQvmdGUmGsnqtNBD8ihXgEZsTKjADNFvAbcHAi1KAqreLNSwRUrZJeZZuyh8Q4cmP-sQr641mN93E-U8-brpV0sioq8BTxjGjqOBygrtiVK50ii1_T331e-zV0C43LEuseCLnGK2EhcgDVwr3sTBWYBBGPpayIMUHF5YiKLvginlkqaVsljdJg2PRtarWaZM2tmNMWVe5tAw7arRDyU4l0J_0s=",
      "title": "economictimes.com"
    }
  },
  {
    "web": {
      "uri": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQHmjQGwwY0-0xsbJwE6Iiq0YeANNMylO8O7QqED6QR0DjWMPWKCVJ7BbFlCtjpnNixJqtuNHhaZnxX7Dm9wpvxATPJ4VcZHG3_bR5gvgWIcN815qb90gvYjQdvgZWfp9O974BgMbTMZPZTuX-Bh",
      "title": "hindustantimes.com"
    }
  },
  {
    "web": {
      "uri": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQEUFnrVepN1H1fgVoziCx8PvUEv8DNW5sY_yi6nfJZmYkgXZZ8grAr3f-Xem__PJGjLLj-tWs7H3RPDym3dLiwculxhndrCJiMmLR05FAqHAkOeKWP4rTTOtfInazSk0kZCgrRS3w==",
      "title": "thehindu.com"
    }
  },
  {
    "web": {
      "uri": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQEM79AM748uZRyPEo299owu6dh5IWJvZ9zN2r9Hsvqze0jlz3qWdydYu77QE_pTR68fleN_TvnWelgL3bay37V8im1EmRsYKQ6waMkotgEma4D_BoRwMSU2G6tHyfuwdc6kVTA0OMdbp-0tyQz25UI9r2Trrl8upNhY5Ghlc1N8sUdlS36fvFv9pzMvAXnlMCU=",
      "title": "jagranjosh.com"
    }
  },
  {
    "web": {
      "uri": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQGXhGLM_mklCri2YB0fS1v_xpfcTItWFFBMRSCIf6MYzMwhs8ebT9nDdMq1PL8pa0OwKcL50rcD5qZ-N2ShSQsI14HQ8XifZy5lQPpLOKwACpHWkkDQPQge0EXAzO2vWzsEwWkpbW6Xd0UqeO8tiMoUZR0J8jg8NHUiatz14S2rdYoiX_szYkRCNRw0e_0=",
      "title": "olympics.com"
    }
  },
  {
    "web": {
      "uri": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQGyyx6UMCzkXbOfQYKW6vf99hvJrupL4nB-46oHtG-n79CcqG7bP6Yw0OCF5hjcwmc5TJG2YzBR7Cr2v9KPApOFm_Cl8AbFTwzxy-A3K6mfixgF6mkGYOeubi-HGVcf_IhSvECdhleOapVK",
      "title": "careerpower.in"
    }
  },
  {
    "web": {
      "uri": "https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQEomdtJaCbrT3KBtlXb1bzxqBz3dQpRn34LgUhKW-wM2Ds5fzsQK5PPOhIYD8DM7KQXCbnj8QZUE9C1fSTjmfHGI0ZujDFAgBiQ1q1woWKKIbKIkvtb-oofk69NW7oldahhbYOuR6fG77IRf2XPdcBS-8eDkZy5blZUeQf3S4L17qrH_p_juMQ=",
      "title": "iplt20.com"
    }
  }
]

You can see that running the same prompt without search grounding gives you outdated information:

const without_search_grounding = await ai.models.generateContent({
  model: MODEL_ID,
  contents: "What was the latest Indian Premier League match and who won?",
});
tslab.display.markdown(without_search_grounding.text ?? "");

The latest Indian Premier League (IPL) match was the Final of the 2024 season, played on May 26, 2024.

It was between:

  • Kolkata Knight Riders (KKR)
  • Sunrisers Hyderabad (SRH)

Kolkata Knight Riders (KKR) won the match by 8 wickets, securing their third IPL title.

Grounding information using URL context

The URL Context tool empowers Gemini models to directly access and process content from specific web page URLs you provide within your API requests. This is incredibly interesting because it allows your applications to dynamically interact with live web information without needing you to manually pre-process and feed that content to the model.

URL Context is effective because it allows the models to base its responses and analysis directly on the content of the designated web pages. Instead of relying solely on its general training data or broad web searches (which are also valuable grounding tools), URL Context anchors the model’s understanding to the specific information present at those URLs.

const url_context_response = await ai.models.generateContent({
  model: MODEL_ID,
  contents: [
    `
    based on https://ai.google.dev/gemini-api/docs/models, what are the key
    differences between Gemini 1.5, Gemini 2.0 and Gemini 2.5 models?
    Create a markdown table comparing the differences.
    `,
  ],
  config: {
    tools: [{ urlContext: {} }],
  },
});
tslab.display.markdown(url_context_response.text ?? "");

The Gemini API offers various models optimized for different use cases, with Gemini 1.5, Gemini 2.0, and Gemini 2.5 representing different generations and capabilities. The key differences between the main variants are summarized in the table below.

Feature Gemini 1.5 Pro Gemini 1.5 Flash Gemini 2.0 Flash Gemini 2.5 Pro (Preview) Gemini 2.5 Flash (Preview)
Primary Use Case / Optimization Mid-size multimodal model optimized for a wide range of reasoning tasks; excels at processing large amounts of data. Fast and versatile multimodal model for scaling across diverse tasks. Next-generation features and improved capabilities, superior speed, native tool use, built for agentic experiences. Most powerful thinking model with maximum response accuracy and state-of-the-art performance; best for complex coding, reasoning, and multimodal understanding. Best model in terms of price-performance, offering well-rounded capabilities; ideal for low-latency, high-volume tasks requiring thinking.
Input Modalities Audio, images, video, text Audio, images, video, text Audio, images, video, text Audio, images, video, text Audio, images, video, text
Output Modalities Text Text Text Text Text
Input Token Limit 2,097,152 (2M) 1,048,576 (1M) 1,048,576 (1M) 1,048,576 (1M) 1,048,576 (1M)
Key Capabilities System instructions, JSON mode, JSON schema, adjustable safety settings, caching, function calling, code execution. System instructions, JSON mode, JSON schema, adjustable safety settings, caching, tuning, function calling, code execution. Structured outputs, caching, function calling, code execution, search grounding, Live API. Thinking is experimental. Structured outputs, caching, function calling, code execution, search grounding, thinking. Adaptive thinking, cost efficiency, structured outputs, caching, function calling, code execution, search grounding, thinking.
Status Latest Stable Latest Stable Latest Stable (also Experimental and Stable versions) Preview Preview

As a reference, you can see how the answer would be without the URL context, using the official models documentation as reference:

const without_url_context_response = await ai.models.generateContent({
  model: MODEL_ID,
  contents: [
    `
        what are the key differences between Gemini 1.5, Gemini 2.0 and Gemini 2.5
        models? Create a markdown table comparing the differences.
        `,
  ],
});
tslab.display.markdown(without_url_context_response.text ?? "");

It seems there might be a slight misunderstanding regarding the versioning of Gemini models. As of my last update, Google has not publicly released models named “Gemini 2.0” or “Gemini 2.5.”

The publicly announced and available Gemini models follow this progression:

  • Gemini 1.0 (Pro, Ultra, Nano): The initial general release of the Gemini family.
  • Gemini 1.5 Pro: A significant upgrade focusing on a massive context window and Mixture-of-Experts (MoE) architecture.
  • Gemini 1.5 Flash: A faster, more cost-effective version of 1.5 Pro, optimized for high-volume, lower-latency tasks.

Therefore, I will provide a comparison between Gemini 1.0 (representing the initial family), Gemini 1.5 Pro, and Gemini 1.5 Flash, as these are the relevant and distinct models in the Gemini lineup today.

Here’s a breakdown of their key differences:


Comparison of Gemini Models (1.0 vs. 1.5 Pro vs. 1.5 Flash)

Feature Gemini 1.0 (Pro/Ultra/Nano) Gemini 1.5 Pro Gemini 1.5 Flash
Release/Announced December 2023 (Pro/Ultra); Early 2024 (Nano) February 2024 (Limited preview); May 2024 (Broader availability) May 2024 (Announced alongside 1.5 Pro’s broader release)
Primary Focus Foundational multimodal capabilities, general-purpose reasoning Massive context window, advanced long-context understanding Speed & efficiency, optimized for high-volume, low-latency tasks
Architecture Highly optimized transformer architecture Mixture-of-Experts (MoE) Mixture-of-Experts (MoE) (optimized for speed)
Context Window Up to 32K tokens (for Pro) 1 Million tokens (default), expandable to 2 Million tokens 1 Million tokens (default), expandable to 2 Million tokens
Performance Strong general reasoning, coding, multimodal understanding Exceptional long-document analysis, complex codebases, video analysis High throughput, good for simpler tasks, less “deep” reasoning than 1.5 Pro
Cost/Efficiency Balanced Higher cost per token due to advanced capabilities Significantly more cost-effective and faster inference
Modality Multimodal (text, image, audio, video input/output) Multimodal (text, image, audio, video input/output) Multimodal (text, image, audio, video input/output)
Ideal Use Cases Chatbots, content generation, general AI applications Summarizing entire books/videos, analyzing large datasets, complex troubleshooting, long-form code analysis High-volume API calls, real-time chatbots, dynamic content updates, RAG without deep reasoning, quick summarization
Key Differentiator First publicly available, versatile Gemini family Unprecedented long-context processing for multimodal data Blazing speed and cost-efficiency for large-scale applications

In summary:

  • Gemini 1.0 established the baseline with strong general-purpose multimodal capabilities.
  • Gemini 1.5 Pro represents a monumental leap in the context window, allowing it to process and understand vast amounts of information (like entire novels, lengthy codebases, or hours of video) in a single prompt. Its MoE architecture contributes to this capability.
  • Gemini 1.5 Flash takes the MoE architecture from 1.5 Pro and optimizes it for speed and cost-efficiency, making it ideal for applications requiring high throughput where the deepest reasoning of 1.5 Pro isn’t strictly necessary. It retains the same large context window as 1.5 Pro.

It’s possible that “Gemini 2.0” or “Gemini 2.5” refers to future internal development versions that have not yet been announced publicly. Google frequently iterates and develops models, and future major versions will undoubtedly bring even more advanced capabilities.

As you can see, using the model knowledge only, it does not know about the new Gemini 2.5 models family.

Next steps

Also check the other Gemini capabilities that you can find in the Gemini quickstarts.