@@ -453,6 +453,100 @@ async def mock_coro():
453453 mock_client .aio .models .generate_content .assert_called_once ()
454454
455455
456+ @pytest .mark .asyncio
457+ async def test_generate_content_async_multiple_candidates_logs_error (
458+ gemini_llm , llm_request , generate_content_response , caplog
459+ ):
460+ generate_content_response .candidates = [
461+ types .Candidate (
462+ content = Content (
463+ role = "model" , parts = [Part .from_text (text = "First candidate" )]
464+ ),
465+ finish_reason = types .FinishReason .STOP ,
466+ ),
467+ types .Candidate (
468+ content = Content (
469+ role = "model" , parts = [Part .from_text (text = "Second candidate" )]
470+ ),
471+ finish_reason = types .FinishReason .STOP ,
472+ ),
473+ ]
474+
475+ with mock .patch .object (gemini_llm , "api_client" ) as mock_client :
476+
477+ async def mock_coro ():
478+ return generate_content_response
479+
480+ mock_client .aio .models .generate_content .return_value = mock_coro ()
481+
482+ with caplog .at_level (logging .ERROR ):
483+ responses = [
484+ response
485+ async for response in gemini_llm .generate_content_async (
486+ llm_request , stream = False
487+ )
488+ ]
489+
490+ mock_client .aio .models .generate_content .assert_called_once ()
491+ assert len (responses ) == 1
492+ assert responses [0 ].content .parts [0 ].text == "First candidate"
493+ errors = [
494+ record
495+ for record in caplog .records
496+ if "Multiple candidates found in response" in record .getMessage ()
497+ ]
498+ assert len (errors ) == 1
499+ assert errors [0 ].name == "google_adk.google.adk.models.google_llm"
500+
501+
502+ @pytest .mark .asyncio
503+ async def test_generate_content_async_stream_multiple_candidates_logs_error (
504+ gemini_llm , llm_request , caplog
505+ ):
506+ with mock .patch .object (gemini_llm , "api_client" ) as mock_client :
507+ mock_responses = [
508+ types .GenerateContentResponse (
509+ candidates = [
510+ types .Candidate (
511+ content = Content (
512+ role = "model" , parts = [Part .from_text (text = first )]
513+ ),
514+ finish_reason = None ,
515+ ),
516+ types .Candidate (
517+ content = Content (
518+ role = "model" , parts = [Part .from_text (text = second )]
519+ ),
520+ finish_reason = None ,
521+ ),
522+ ]
523+ )
524+ for first , second in (("Hello" , "World" ), ("Hello2" , "World2" ))
525+ ]
526+
527+ async def mock_coro ():
528+ return MockAsyncIterator (mock_responses )
529+
530+ mock_client .aio .models .generate_content_stream .return_value = mock_coro ()
531+
532+ with caplog .at_level (logging .ERROR ):
533+ _ = [
534+ response
535+ async for response in gemini_llm .generate_content_async (
536+ llm_request , stream = True
537+ )
538+ ]
539+
540+ errors = [
541+ record
542+ for record in caplog .records
543+ if "Multiple candidates found in streaming response"
544+ in record .getMessage ()
545+ ]
546+ assert len (errors ) == 1
547+ assert errors [0 ].name == "google_adk.google.adk.models.google_llm"
548+
549+
456550@pytest .mark .asyncio
457551async def test_generate_content_async_stream (gemini_llm , llm_request ):
458552 with mock .patch .object (gemini_llm , "api_client" ) as mock_client :
@@ -3043,3 +3137,182 @@ async def __aexit__(self, *args):
30433137 # The log is still emitted and still useful.
30443138 assert "gemini-2.5-flash" in caplog .text
30453139 assert "Modality.AUDIO" in caplog .text
3140+
3141+
3142+ @pytest .mark .asyncio
3143+ async def test_generate_content_async_stream_secondary_candidate_chunk (
3144+ gemini_llm , llm_request , caplog
3145+ ):
3146+ """Test a candidate streamed in its own chunk is detected and skipped."""
3147+ with mock .patch .object (gemini_llm , "api_client" ) as mock_client :
3148+ mock_responses = [
3149+ types .GenerateContentResponse (
3150+ candidates = [
3151+ types .Candidate (
3152+ index = 0 ,
3153+ content = Content (
3154+ role = "model" , parts = [Part .from_text (text = "Hello" )]
3155+ ),
3156+ finish_reason = None ,
3157+ )
3158+ ]
3159+ ),
3160+ types .GenerateContentResponse (
3161+ candidates = [
3162+ types .Candidate (
3163+ index = 1 ,
3164+ content = Content (
3165+ role = "model" , parts = [Part .from_text (text = "Other" )]
3166+ ),
3167+ finish_reason = None ,
3168+ )
3169+ ]
3170+ ),
3171+ types .GenerateContentResponse (
3172+ candidates = [
3173+ types .Candidate (
3174+ index = 0 ,
3175+ content = Content (
3176+ role = "model" , parts = [Part .from_text (text = " world" )]
3177+ ),
3178+ finish_reason = types .FinishReason .STOP ,
3179+ )
3180+ ]
3181+ ),
3182+ ]
3183+
3184+ async def mock_coro ():
3185+ return MockAsyncIterator (mock_responses )
3186+
3187+ mock_client .aio .models .generate_content_stream .return_value = mock_coro ()
3188+
3189+ with caplog .at_level (logging .ERROR ):
3190+ responses = [
3191+ response
3192+ async for response in gemini_llm .generate_content_async (
3193+ llm_request , stream = True
3194+ )
3195+ ]
3196+
3197+ assert responses [- 1 ].content .parts [0 ].text == "Hello world"
3198+ errors = [
3199+ record
3200+ for record in caplog .records
3201+ if "Multiple candidates found in streaming response"
3202+ in record .getMessage ()
3203+ ]
3204+ assert len (errors ) == 1
3205+
3206+
3207+ @pytest .mark .asyncio
3208+ async def test_generate_content_async_stream_secondary_candidate_chunk_is_skipped (
3209+ gemini_llm , llm_request
3210+ ):
3211+ """Test a chunk holding only secondary candidates yields no partial response."""
3212+ with mock .patch .object (gemini_llm , "api_client" ) as mock_client :
3213+ mock_responses = [
3214+ types .GenerateContentResponse (
3215+ candidates = [
3216+ types .Candidate (
3217+ index = 0 ,
3218+ content = Content (
3219+ role = "model" , parts = [Part .from_text (text = "Hello" )]
3220+ ),
3221+ finish_reason = None ,
3222+ )
3223+ ]
3224+ ),
3225+ types .GenerateContentResponse (
3226+ candidates = [
3227+ types .Candidate (
3228+ index = 1 ,
3229+ content = Content (
3230+ role = "model" , parts = [Part .from_text (text = "Other" )]
3231+ ),
3232+ finish_reason = None ,
3233+ )
3234+ ]
3235+ ),
3236+ types .GenerateContentResponse (
3237+ candidates = [
3238+ types .Candidate (
3239+ index = 0 ,
3240+ content = Content (
3241+ role = "model" , parts = [Part .from_text (text = " world" )]
3242+ ),
3243+ finish_reason = types .FinishReason .STOP ,
3244+ )
3245+ ]
3246+ ),
3247+ ]
3248+
3249+ async def mock_coro ():
3250+ return MockAsyncIterator (mock_responses )
3251+
3252+ mock_client .aio .models .generate_content_stream .return_value = mock_coro ()
3253+
3254+ responses = [
3255+ response
3256+ async for response in gemini_llm .generate_content_async (
3257+ llm_request , stream = True
3258+ )
3259+ ]
3260+
3261+ assert len (responses ) == 3
3262+ assert [r .content .parts [0 ].text for r in responses ] == [
3263+ "Hello" ,
3264+ " world" ,
3265+ "Hello world" ,
3266+ ]
3267+
3268+
3269+ @pytest .mark .asyncio
3270+ async def test_generate_content_async_stream_secondary_candidate_chunk_preserves_usage_metadata (
3271+ gemini_llm , llm_request
3272+ ):
3273+ """Test that usage metadata on a chunk holding only secondary candidates is preserved."""
3274+ with mock .patch .object (gemini_llm , "api_client" ) as mock_client :
3275+ mock_responses = [
3276+ types .GenerateContentResponse (
3277+ candidates = [
3278+ types .Candidate (
3279+ index = 0 ,
3280+ content = Content (
3281+ role = "model" , parts = [Part .from_text (text = "Hello" )]
3282+ ),
3283+ finish_reason = None ,
3284+ )
3285+ ]
3286+ ),
3287+ types .GenerateContentResponse (
3288+ candidates = [
3289+ types .Candidate (
3290+ index = 1 ,
3291+ content = Content (
3292+ role = "model" , parts = [Part .from_text (text = "Other" )]
3293+ ),
3294+ finish_reason = types .FinishReason .STOP ,
3295+ )
3296+ ],
3297+ usage_metadata = types .GenerateContentResponseUsageMetadata (
3298+ prompt_token_count = 10 ,
3299+ candidates_token_count = 5 ,
3300+ total_token_count = 15 ,
3301+ ),
3302+ ),
3303+ ]
3304+
3305+ async def mock_coro ():
3306+ return MockAsyncIterator (mock_responses )
3307+
3308+ mock_client .aio .models .generate_content_stream .return_value = mock_coro ()
3309+
3310+ responses = [
3311+ response
3312+ async for response in gemini_llm .generate_content_async (
3313+ llm_request , stream = True
3314+ )
3315+ ]
3316+
3317+ assert responses [- 1 ].usage_metadata is not None
3318+ assert responses [- 1 ].usage_metadata .total_token_count == 15
0 commit comments