From b0f54e0d2b530b3d107475c40bde8f01310aac5f Mon Sep 17 00:00:00 2001 From: Yufeng He <40085740+he-yufeng@users.noreply.github.com> Date: Sun, 7 Jun 2026 15:18:56 +0800 Subject: [PATCH] fix: accept grounding chunk metadata --- google/genai/tests/types/test_types.py | 24 ++++++++++++++++++++++++ google/genai/types.py | 7 +++++++ 2 files changed, 31 insertions(+) diff --git a/google/genai/tests/types/test_types.py b/google/genai/tests/types/test_types.py index 099ff2a2d..2608dfab0 100644 --- a/google/genai/tests/types/test_types.py +++ b/google/genai/tests/types/test_types.py @@ -155,6 +155,30 @@ def test_factory_method_from_code_execution_result_part(): assert isinstance(my_part, SubPart) +def test_generate_content_response_accepts_grounding_chunk_metadata(): + response = types.GenerateContentResponse.model_validate({ + 'candidates': [{ + 'content': {'parts': [{'text': 'ok'}], 'role': 'model'}, + 'groundingMetadata': { + 'groundingChunks': [{ + 'chunkMetadata': {'query': 'dummy_query'}, + 'web': { + 'title': 'youtube.com', + 'uri': ( + 'https://vertexaisearch.cloud.google.com/' + 'grounding-api-redirect/id' + ), + }, + }] + }, + }] + }) + + chunk = response.candidates[0].grounding_metadata.grounding_chunks[0] + assert chunk.chunk_metadata == {'query': 'dummy_query'} + assert chunk.web.title == 'youtube.com' + + def test_factory_method_from_mcp_call_tool_function_response_on_error(): if not _is_mcp_imported: return diff --git a/google/genai/types.py b/google/genai/types.py index f99fbd070..fee27f8d0 100644 --- a/google/genai/types.py +++ b/google/genai/types.py @@ -7146,6 +7146,10 @@ class GroundingChunk(_common.BaseModel): the source of the information. """ + chunk_metadata: Optional[dict[str, Any]] = Field( + default=None, + description="""Metadata for this grounding chunk.""", + ) image: Optional[GroundingChunkImage] = Field( default=None, description="""A grounding chunk from an image search result. See the `Image` message for details.""", @@ -7176,6 +7180,9 @@ class GroundingChunkDict(TypedDict, total=False): the source of the information. """ + chunk_metadata: Optional[dict[str, Any]] + """Metadata for this grounding chunk.""" + image: Optional[GroundingChunkImageDict] """A grounding chunk from an image search result. See the `Image` message for details."""