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.
I have similar issues. For my case, grounding_metadata was never empty. But grouding_chunks and grounding_supports (attributes of grouding_metadata) are returned as None most of the time. I haven’t tested it thoroughly, but I got proper lengthy outcome at first time. And it returns None afterwards, even I try different prompts.
grouding_chunks and grounding_supports is critical for my use case, as I can find the exact url, which information Gemini got from the url and confidence score from there.
I second this. been having the same issue with groundingSupports and groundingChunks not return in either cases that the output consists only of a code block or some text and then code block.
When I got text only output then there are groundingSupports and groundingChunks
I am having a similar behavior, grounding_chunks are returned empty but grounding_supports do have data that links to non existing chunks. Here is an example:
"grounding_metadata": {
"grounding_chunks": null,
"grounding_supports": [
{
"confidence_scores": null,
"grounding_chunk_indices": [
0
],
"segment": {
"end_index": 1994,
"part_index": null,
"start_index": 1901,
"text": "Steinway has a \"All-Steinway School\" program, indicating a significant focus on this segment."
}
},
{
"confidence_scores": null,
"grounding_chunk_indices": [
1
],
"segment": {
"end_index": 4802,
"part_index": null,
"start_index": 4674,
"text": "* The global musical instruments market size was estimated at approximately USD 17.5 billion in 2023 and is projected to grow."
}
},```
I am using gemini-2.5-flash
Seeing the same issue as above (grounding_chunks being empty but grounding_support.grounding_chunk_indices pointing to an index) and it makes relying on Gemini grounding responses very difficult.
Anecdotally, this seems to occur more frequently when in a multi-turn conversation, I imagine because the model thinks it has already responded with this grounding_chunk in the past and doesnt want to repeat itself, but if there are many messages in the conversation it is impossible to identify exactly which chunk this message particular response is referring to.