fix(gemini): preserve every grounding web source (#48745)

This commit is contained in:
Zabir Raihan 2026-10-29 06:39:15 +08:00 • committed by GitHub
parent cf1006a368
commit 7d79824391
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 35 additions and 4 deletions

View file

@ -2743,10 +2743,10 @@ class VertexGeminiConfig(VertexAIBaseConfig, BaseConfig):
chunk_indices = support.get("groundingChunkIndices", [])
if start_index is not None and end_index is not None and chunk_indices:
# Use the first chunk's URL for the annotation
first_chunk_idx = chunk_indices[0]
if first_chunk_idx in chunk_to_uri_map:
uri_info = chunk_to_uri_map[first_chunk_idx]
for chunk_index in chunk_indices:
if chunk_index not in chunk_to_uri_map:
continue
uri_info = chunk_to_uri_map[chunk_index]
url_citation: ChatCompletionAnnotationURLCitation = {
"start_index": start_index,

View file

@ -10655,3 +10655,34 @@ async def test_gemini_sends_a_boolean_for_a_gateway_default_written_as_text_or_n
sent: Final = await _sent_thinking_config("gemini-3.8-flash", {"reasoning_effort": "high"})
assert sent["includeThoughts"] is expected
@pytest.mark.parametrize("chunk_indices", [[0, 1], [2, 1, 0]])
def test_grounding_support_preserves_every_web_source(chunk_indices: list[int]) -> None:
metadata: Final = {
"groundingChunks": [
{"web": {"uri": "https://example.com/a", "title": "A"}},
{"web": {"uri": "https://example.com/b", "title": "B"}},
{"retrievedContext": {"uri": "gs://document"}},
],
"groundingSupports": [
{"segment": {"startIndex": 0, "endIndex": 5}, "groundingChunkIndices": chunk_indices},
{"segment": {"startIndex": 6, "endIndex": 11}, "groundingChunkIndices": [1]},
],
}
annotations: Final = VertexGeminiConfig._convert_grounding_metadata_to_annotations([metadata], "First claim")
expected_sources: Final = [(index, 0, 5) for index in chunk_indices if index in (0, 1)] + [(1, 6, 11)]
assert annotations == [
{
"type": "url_citation",
"url_citation": {
"start_index": start,
"end_index": end,
"url": metadata["groundingChunks"][index]["web"]["uri"],
"title": metadata["groundingChunks"][index]["web"]["title"],
},
}
for index, start, end in expected_sources
]