@@ -892,6 +892,153 @@ class StorySummary(pydantic.BaseModel):
892892 ]
893893
894894
895+ class _Summary (pydantic .BaseModel ):
896+ title : str
897+
898+
899+ def _thought_hop () -> types .GenerateContentResponse :
900+ return types .GenerateContentResponse (
901+ candidates = [
902+ types .Candidate (
903+ content = types .Content (
904+ role = 'model' ,
905+ parts = [types .Part (text = 'thinking' , thought = True )],
906+ ),
907+ finish_reason = types .FinishReason .CONTINUATION ,
908+ continuation_token = b'tok' ,
909+ )
910+ ]
911+ )
912+
913+
914+ def _json_hop (parsed ) -> types .GenerateContentResponse :
915+ return types .GenerateContentResponse (
916+ candidates = [
917+ types .Candidate (
918+ content = types .Content (
919+ role = 'model' , parts = [types .Part (text = '{"title": "x"}' )]
920+ ),
921+ finish_reason = types .FinishReason .STOP ,
922+ )
923+ ],
924+ parsed = parsed ,
925+ )
926+
927+
928+ @pytest .mark .parametrize (
929+ 'config, hop_parsed, expected' ,
930+ [
931+ (
932+ {'response_json_schema' : {'type' : 'object' }},
933+ {'title' : 'x' },
934+ {'title' : 'x' },
935+ ),
936+ (
937+ types .GenerateContentConfig (response_schema = _Summary ),
938+ _Summary (title = 'x' ),
939+ _Summary (title = 'x' ),
940+ ),
941+ (
942+ {'response_schema' : _Summary },
943+ _Summary (title = 'x' ),
944+ _Summary (title = 'x' ),
945+ ),
946+ ],
947+ )
948+ def test_merge_continuation_responses_reparses_when_hop_has_parsed (
949+ config , hop_parsed , expected
950+ ):
951+ """Merging hops that already carry `parsed` reparses instead of raising."""
952+ merged = _extra_utils .merge_continuation_responses (
953+ [_thought_hop (), _json_hop (hop_parsed )], config = config
954+ )
955+
956+ assert merged .parsed == expected
957+ assert merged .text == '{"title": "x"}'
958+
959+
960+ def test_merge_continuation_responses_replaces_stale_hop_parsed ():
961+ """`merged.parsed` comes from the merged text, not from a hop's `parsed`."""
962+ hop1 = types .GenerateContentResponse (
963+ candidates = [
964+ types .Candidate (
965+ content = types .Content (
966+ role = 'model' , parts = [types .Part (text = '{"title": "Long ' )]
967+ ),
968+ finish_reason = types .FinishReason .CONTINUATION ,
969+ continuation_token = b'tok' ,
970+ )
971+ ],
972+ parsed = {'title' : 'stale' },
973+ )
974+ hop2 = types .GenerateContentResponse (
975+ candidates = [
976+ types .Candidate (
977+ content = types .Content (
978+ role = 'model' , parts = [types .Part (text = 'Odyssey"}' )]
979+ ),
980+ finish_reason = types .FinishReason .STOP ,
981+ )
982+ ],
983+ )
984+
985+ merged = _extra_utils .merge_continuation_responses (
986+ [hop1 , hop2 ], config = {'response_json_schema' : {'type' : 'object' }}
987+ )
988+
989+ assert merged .parsed == {'title' : 'Long Odyssey' }
990+
991+
992+ def test_models_generate_content_continuation_with_parsed_hop (
993+ mock_api_client ,
994+ ):
995+ """generate_content merges hops when the final hop already has `parsed`."""
996+ models_module = models .Models (mock_api_client )
997+
998+ with mock .patch .object (
999+ models .Models ,
1000+ '_generate_content' ,
1001+ side_effect = [_thought_hop (), _json_hop (_Summary (title = 'x' ))],
1002+ ):
1003+ response = models_module .generate_content (
1004+ model = 'gemini-2.5-pro' ,
1005+ contents = 'Give me JSON' ,
1006+ config = types .GenerateContentConfig (
1007+ automatic_continuation = True ,
1008+ response_schema = _Summary ,
1009+ ),
1010+ )
1011+
1012+ assert response .parsed == _Summary (title = 'x' )
1013+ assert response .text == '{"title": "x"}'
1014+
1015+
1016+ @pytest .mark .asyncio
1017+ async def test_async_models_generate_content_continuation_with_parsed_hop (
1018+ mock_api_client ,
1019+ ):
1020+ """AsyncModels.generate_content merges hops when a hop has `parsed`."""
1021+ models_module = models .AsyncModels (mock_api_client )
1022+
1023+ with mock .patch .object (
1024+ models .AsyncModels ,
1025+ '_generate_content' ,
1026+ new_callable = mock .AsyncMock ,
1027+ side_effect = [_thought_hop (), _json_hop (_Summary (title = 'x' ))],
1028+ ):
1029+ response = await models_module .generate_content (
1030+ model = 'gemini-2.5-pro' ,
1031+ contents = 'Give me JSON' ,
1032+ config = types .GenerateContentConfig (
1033+ automatic_continuation = True ,
1034+ response_schema = _Summary ,
1035+ ),
1036+ )
1037+
1038+ assert response .parsed == _Summary (title = 'x' )
1039+ assert response .text == '{"title": "x"}'
1040+
1041+
8951042def test_merge_continuation_responses_shifts_grounding_support_chunk_and_part_indices ():
8961043 """Merging responses across hops shifts GroundingSupport chunk and part indices."""
8971044 hop1 = types .GenerateContentResponse (
0 commit comments