@@ -930,6 +930,59 @@ def merge_safety_ratings(
930930 return [by_category [cat ] for cat in order ]
931931
932932
933+ def _shift_candidate_grounding_supports (
934+ prev : types .Candidate ,
935+ curr : types .Candidate ,
936+ ) -> types .Candidate :
937+ """Shifts curr's GroundingSupport chunk and part indices by prev's lengths."""
938+ curr_gm = curr .grounding_metadata
939+ if curr_gm is None or not curr_gm .grounding_supports :
940+ return curr
941+
942+ part_offset = (
943+ len (prev .content .parts )
944+ if prev .content is not None and prev .content .parts
945+ else 0
946+ )
947+ chunk_offset = (
948+ len (prev .grounding_metadata .grounding_chunks )
949+ if prev .grounding_metadata is not None
950+ and prev .grounding_metadata .grounding_chunks
951+ else 0
952+ )
953+ if part_offset == 0 and chunk_offset == 0 :
954+ return curr
955+
956+ shifted_supports : list [types .GroundingSupport ] = []
957+ for support in curr_gm .grounding_supports :
958+ support_updates : dict [str , Any ] = {}
959+ if chunk_offset > 0 and support .grounding_chunk_indices :
960+ support_updates ['grounding_chunk_indices' ] = [
961+ idx + chunk_offset for idx in support .grounding_chunk_indices
962+ ]
963+ if part_offset > 0 and support .segment is not None :
964+ prev_part_index = (
965+ support .segment .part_index
966+ if support .segment .part_index is not None
967+ else 0
968+ )
969+ support_updates ['segment' ] = support .segment .model_copy (
970+ update = {'part_index' : prev_part_index + part_offset }
971+ )
972+ if support_updates :
973+ shifted_supports .append (support .model_copy (update = support_updates ))
974+ else :
975+ shifted_supports .append (support )
976+
977+ return curr .model_copy (
978+ update = {
979+ 'grounding_metadata' : curr_gm .model_copy (
980+ update = {'grounding_supports' : shifted_supports }
981+ )
982+ }
983+ )
984+
985+
933986def merge_candidates (
934987 prev_candidates : list [types .Candidate ],
935988 curr_candidates : list [types .Candidate ],
@@ -953,6 +1006,8 @@ def merge_candidates(
9531006
9541007def merge_pydantic_models (prev : C , curr : C ) -> C :
9551008 """Recursively merges two Pydantic BaseModel instances across continuation hops."""
1009+ if isinstance (prev , types .Candidate ) and isinstance (curr , types .Candidate ):
1010+ curr = typing .cast (C , _shift_candidate_grounding_supports (prev , curr ))
9561011 merged_data : dict [str , Any ] = {}
9571012 for field_name in type (curr ).model_fields :
9581013 prev_val = getattr (prev , field_name , None )
0 commit comments