Intro to Url Context
Source notebook
Repo path:
gemini/url-context/intro_url_context.ipynb· Open on GitHub · intro
Shows how to use Gemini URL context to summarize, compare, and analyze web pages with optional Google Search grounding.
Summary
This notebook teaches how to configure the Google Gen AI SDK for Vertex AI and call Gemini 2.5 Flash with the URL context tool. It demonstrates single-URL summarization, multi-URL comparison, inspecting URL retrieval metadata, and combining URL context with Google Search grounding in v1beta1. The workflow covers authentication, client setup, tool configuration, generate_content calls, and source metadata inspection.
Key code patterns
Vertex AI GenAI client
client = genai.Client(
enterprise=True,
project=PROJECT_ID,
location="global",
)Creates a Google Gen AI SDK client connected to Vertex AI using a Google Cloud project.
URL context tool
url_context_tool = Tool(url_context=UrlContext)
response = client.models.generate_content(
model=MODEL_ID,
contents="Summarize this document: https://blog.google/...",
config=GenerateContentConfig(tools=[url_context_tool]),
)Enables Gemini to retrieve and use the content of URLs included in the prompt.
Inspect URL metadata
print(response.candidates[0].url_context_metadata)Verifies which URLs were retrieved and their retrieval status.
URL context plus Google Search
client_v1beta1 = genai.Client(
enterprise=True,
project=PROJECT_ID,
location=LOCATION,
http_options=HttpOptions(api_version="v1beta1"),
)
tools = [Tool(url_context=UrlContext), Tool(google_search=GoogleSearch())]Combines URL context with Google Search grounding using the experimental v1beta1 API.
Models & APIs used
- Models: gemini-2.5-flash
- APIs / services: Vertex AI, Gemini API, Grounding with Google Search
- SDKs / libraries:
google-genai
When to use this
Use this pattern when Gemini should answer from one or more specific web pages and optionally search for related current context.
Gotchas & caveats
- Requires Google Cloud project authentication or Vertex AI API key Express Mode.
- The notebook uses LOCATION = “global”.
- Multiple URL context requests currently support a maximum of 20 URLs per request.
- Combining URL context with Google Search is experimental and only available in API version v1beta1.
- The SDK uses beta API endpoints by default, but the notebook explicitly sets api_version=“v1beta1” for the combined tool example.
Best practices
- Install or upgrade google-genai before running the notebook.
- Authenticate Colab users with auth.authenticate_user().
- Use a Google Cloud Project for authentication in the tutorial workflow.
- Inspect url_context_metadata to debug retrieved sources and verify information sources.
- Inspect grounding_metadata when using Google Search grounding.
- Use URL context for extracting data, comparing information, synthesizing sources, answering questions from pages, and content analysis.
Related
- Concepts: Function Calling & Tools · RAG & Grounding · Getting Started
- Entities: Vertex AI · Google GenAI SDK · Grounding · Gemini
- Area: Gemini Notebooks
- Best practices: Function Calling & Tools - Best Practices · RAG & Grounding - Best Practices · Getting Started - Best Practices