-
Notifications
You must be signed in to change notification settings - Fork 605
Open
Labels
priority: p3Desirable enhancement or fix. May not be included in next release.Desirable enhancement or fix. May not be included in next release.type: questionRequest for information or clarification. Not an issue.Request for information or clarification. Not an issue.
Description
What I am trying to do
I am trying to get the actual URL of the grounding chunk. However, all the URLs in the metadata are redirect links to vertexaisearch.cloud.google.com
. I would like to know if there is a way to obtain the result without performing another HTTP request to the vertexaisearch.cloud.google.com
and following the redirects.
I have read the docs and found that GroundingChunkWeb#domain exists however through testing, I have found that it just gives me None
.
My Code
# To run this code you need to install the following dependencies:
# pip install google-genai
import base64
import os
from google import genai
from google.genai import types
def generate():
client = genai.Client(
api_key=os.environ.get("GEMINI_API_KEY"),
)
model = "gemini-2.5-flash-preview-09-2025"
contents = [
types.Content(
role="user",
parts=[
types.Part.from_text(text="""Search the web for the latest news on AI advancements"""),
],
),
]
tools = [
types.Tool(url_context=types.UrlContext()),
types.Tool(googleSearch=types.GoogleSearch(
)),
]
generate_content_config = types.GenerateContentConfig(
thinking_config = types.ThinkingConfig(
thinking_budget=-1,
),
tools=tools,
)
res = client.models.generate_content(model=model, contents=contents, config=generate_content_config)
print(res.candidates[0].grounding_metadata.grounding_chunks)
print("="*50)
print(res.candidates[0].grounding_metadata.grounding_chunks[0].web.domain)
if __name__ == "__main__":
generate()
Output
[GroundingChunk(
web=GroundingChunkWeb(
title='openai.com',
uri='https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQE7lMEaHE1RkAGMA-shF_58De-kPLvfEhx8u1AwaNefWUQZqnQs3vfOJNh2z-d7XPKBOG9S2_1PI0Yhyrs4_9nik_YdfOxa97qkbfRolTRTK2xpYw=='
<... snip ...>
), GroundingChunk(
web=GroundingChunkWeb(
title='mit.edu',
uri='https://vertexaisearch.cloud.google.com/grounding-api-redirect/AUZIYQEBBzMGiOTYFvDK1niZ-hwEiAtnWHXC69jaHsCgq7Afq2sCOIBFstmNgrz-AyUkDjRNaDufa3QE0Y_VBs04TZ4anT2X6Z-_EGfzJHN7u5NtpeILeqjvpqWehCtpLDOqDKnV9ipqPt-_8t7yeDkz6GkGnfRRl2slpZOoOsn5yqckb8AsjFg8vLaaOL56'
)
)]
==================================================
None
Error Messages
None
Metadata
Metadata
Assignees
Labels
priority: p3Desirable enhancement or fix. May not be included in next release.Desirable enhancement or fix. May not be included in next release.type: questionRequest for information or clarification. Not an issue.Request for information or clarification. Not an issue.