Running the grounding examples, grounding metadata that is returned is often empty, even when the model clearly did perform grounding (i.e. it gives correct recent information). It seems completely random, and happens in ~20% of the calls.
I cannot replicate this with the examples given in the docs anymore. Here is my code:
prompt = "When is the next total solar eclipse in the United States?"
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
model = "gemini-2.5-pro-preview-03-25"
contents = [
types.Content(
role="user",
parts=[types.Part.from_text(text=prompt)],
),
]
tools = [types.Tool(google_search=types.GoogleSearch())]
generate_content_config = types.GenerateContentConfig(
tools=tools,
response_mime_type="text/plain",
)
response = client.models.generate_content(
model=model,
contents=contents,
config=generate_content_config,
)
print(response.candidates[0].grounding_metadata)
However, as soon as I instruct the model to output as JSON (not using structured output, just instructing it to do so), e.g. with this prompt:
When is the next total solar eclipse in the United States? Output as json with field 'date' in YYYY-MM-DD format.
then it fails to include sources in the response, in cca. 60% of the calls (randomly). I tested across various different prompt complexities, and it seems to always be around 60%, as soon as JSON output is mentioned in the prompt.
Without asking for json output, did you get the issue??
I am seeing with json output prompt, grounding_chunks are not present but search results are there with this line of code “display(HTML(response.candidates[0].grounding_metadata.search_entry_point.rendered_content))”
My guess is this might happen bcz multitool is not supported. Let me check on this.
Yes, search queries are always returned. However, as far as I can tell, whenever output consists only of a code block, there aren’t any grounding supports or chunks returned. When I instruct the model not to output the JSON (or XML) inside a code block, then it seems to work. However, model often ignores those instructions, so it’s very hard to steer around this issue.