Gemini can pull real-time information from Google Search to ground its responses, making them more current and accurate than models that rely solely on their training data.

Let’s see how this works with a concrete example. Imagine we want to know the current price of a specific stock.

import google.generativeai as genai

# Configure your API key
genai.configure(api_key="YOUR_API_KEY")

# Initialize the model
model = genai.GenerativeModel('gemini-1.5-flash')

# Define the prompt
prompt = "What is the current stock price of Apple (AAPL)?"

# Generate the response, grounding it with search results
response = model.generate_content(prompt,
    generation_config=genai.GenerationConfig(
        # You can set other generation parameters here if needed
    ),
    # This is the key part: enable search grounding
    tools=[genai.Tool(function_declarations=[
        genai.FunctionDeclaration(
            name="google_search",
            description="Performs a Google Search and returns the top search results.",
            parameters=genai.StructuredSchema(
                properties={
                    "query": genai.Schema(type=genai.Schema.STRING)
                },
                required=["query"]
            )
        )
    ])]
)

# Print the response
print(response.text)

When you run this, Gemini doesn’t just guess or recall information from its training data. It recognizes that the prompt requires up-to-date information and uses the google_search tool to query Google. The search results are then fed back to Gemini, allowing it to synthesize an answer based on the latest available information. The output might look something like this:

"As of my last search, the stock price for Apple (AAPL) was approximately $180.50. Please note that stock prices fluctuate rapidly, so this is a snapshot in time."

The core problem this feature solves is the "knowledge cut-off" inherent in large language models. LLMs are trained on massive datasets, but this data is static. Once training is complete, the model has no inherent knowledge of events or information that emerged after that point. This makes them unreliable for questions about current events, breaking news, rapidly changing data (like stock prices, weather, or sports scores), or any topic where recency is critical. By integrating real-time search, Gemini bridges this gap, allowing it to access and process information as it becomes available on the web.

Internally, when google_search is invoked, Gemini constructs a search query based on your prompt. It then sends this query to a Google Search API. The API returns a set of relevant search results, including snippets, titles, and URLs. Gemini then analyzes these results, extracting the most pertinent information to formulate an answer. This process isn’t just about pulling text; it involves understanding the context of the search results and integrating them coherently into a natural language response. You control the type of information Gemini can access by defining the tools it has available. In this case, google_search is the primary tool for real-time web data.

The magic lies in how Gemini interprets the search results. It’s not simply copying and pasting. The model is designed to understand the nuances of web content, identify authoritative sources, and synthesize information from multiple results if necessary. For instance, if one result gives a stock price and another provides a trend, Gemini can combine these to give a more informed answer. The generation_config allows you to fine-tune aspects like temperature (creativity vs. determinism) and top_k/top_p for sampling, which can influence how Gemini uses the grounded information, though for factual grounding, lower temperatures are often preferred.

When Gemini uses a tool like google_search, it doesn’t just execute it and move on. It actually evaluates the output of the tool in the context of your original prompt. If the search results are ambiguous or don’t directly answer the question, Gemini might refine its search query or ask clarifying questions. This iterative process, though often happening instantaneously from the user’s perspective, is crucial for delivering accurate, grounded responses. It’s a form of "reasoning over information" where the external search result becomes a temporary piece of knowledge that Gemini can reason with.

The real power comes when you combine this with other tools. Imagine asking Gemini to "Summarize the latest developments in AI research and suggest a research paper on a novel approach, providing its arXiv link." Gemini could use google_search to find recent AI news and papers, then potentially use another tool (if defined) to fetch details from arXiv, and finally synthesize it all. This layered approach allows for increasingly complex and dynamic information retrieval and processing.

The next step in leveraging real-time data is understanding how to handle conflicting information from search results. When multiple sources present contradictory facts, Gemini needs a strategy to prioritize or flag these discrepancies.

Want structured learning?

Take the full Gemini-api course →