@@ -342,15 +342,20 @@ def send_message(
342342 ):
343343 break
344344
345+ logger .info (f"AFC remote call { i } is done." )
346+ remaining_remote_calls_afc -= 1
347+ if remaining_remote_calls_afc == 0 :
348+ # No request is left to send a result with, so the functions are not
349+ # called at all. Breaking here leaves the turn to be recorded once,
350+ # below, as the user's message followed by the model's function call.
351+ logger .info ("Reached max remote calls for automatic function calling." )
352+ break
353+
345354 func_response_parts = _extra_utils .get_function_response_parts (
346355 response , function_map
347356 )
348357 if not func_response_parts :
349358 break
350- logger .info (f"AFC remote call { i } is done." )
351- remaining_remote_calls_afc -= 1
352- if remaining_remote_calls_afc == 0 :
353- logger .info ("Reached max remote calls for automatic function calling." )
354359 func_call_content = response .candidates [0 ].content
355360 func_response_content = types .Content (
356361 role = "user" , parts = func_response_parts
@@ -377,7 +382,6 @@ def send_message(
377382 )
378383 return response
379384
380-
381385 def send_message_stream (
382386 self ,
383387 message : Union [list [PartUnionDict ], PartUnionDict ],
@@ -489,6 +493,15 @@ def send_message_stream(
489493 contents = contents_to_model , # type: ignore[arg-type]
490494 config = parsed_config ,
491495 )
496+ remaining_remote_calls_afc -= 1
497+ # No request is left to send a result with, so the functions are not
498+ # called at all. The chunks are still yielded, and the turn is recorded
499+ # once below, ending on the model's unanswered function call.
500+ is_last_remote_call_afc = remaining_remote_calls_afc == 0
501+ if is_last_remote_call_afc :
502+ logger .info (
503+ "Reached max remote calls for automatic function calling."
504+ )
492505
493506 model_output = []
494507 finish_reason = None
@@ -501,7 +514,8 @@ def send_message_stream(
501514 is_valid = False
502515
503516 if (
504- function_map
517+ not is_last_remote_call_afc
518+ and function_map
505519 and chunk .candidates
506520 and chunk .candidates [0 ].content
507521 and chunk .candidates [0 ].content .parts
@@ -518,15 +532,12 @@ def send_message_stream(
518532 finish_reason = chunk .candidates [0 ].finish_reason
519533 yield chunk
520534
535+ if is_last_remote_call_afc :
536+ break
521537 if not function_map or not func_response_parts :
522538 break
523539
524540 logger .info (f"AFC remote call { i } is done." )
525- remaining_remote_calls_afc -= 1
526- if remaining_remote_calls_afc == 0 :
527- logger .info (
528- "Reached max remote calls for automatic function calling."
529- )
530541
531542 if chunk and chunk .candidates and chunk .candidates [0 ].content :
532543 func_response_content = types .Content (
@@ -802,6 +813,17 @@ async def send_message(
802813 ):
803814 break
804815
816+ logger .info (f"AFC remote call { i } is done." )
817+ remaining_remote_calls_afc -= 1
818+ if remaining_remote_calls_afc == 0 :
819+ # No request is left to send a result with, so the functions are not
820+ # called at all. Breaking here leaves the turn to be recorded once,
821+ # below, as the user's message followed by the model's function call.
822+ logger .info (
823+ "Reached max remote calls for automatic function calling."
824+ )
825+ break
826+
805827 func_response_parts = (
806828 await _extra_utils .get_function_response_parts_async (
807829 response , function_map
@@ -810,13 +832,6 @@ async def send_message(
810832 if not func_response_parts :
811833 break
812834
813- logger .info (f"AFC remote call { i } is done." )
814- remaining_remote_calls_afc -= 1
815- if remaining_remote_calls_afc == 0 :
816- logger .info (
817- "Reached max remote calls for automatic function calling."
818- )
819-
820835 func_call_content = response .candidates [0 ].content
821836 func_response_content = types .Content (
822837 role = "user" , parts = func_response_parts
@@ -845,7 +860,6 @@ async def send_message(
845860 )
846861 return response
847862
848-
849863 async def send_message_stream (
850864 self ,
851865 message : Union [list [PartUnionDict ], PartUnionDict ],
@@ -1034,6 +1048,15 @@ async def async_generator(): # type: ignore[no-untyped-def]
10341048 contents = contents_to_model , # type: ignore[arg-type]
10351049 config = final_parsed_config ,
10361050 )
1051+ remaining_remote_calls_afc -= 1
1052+ # No request is left to send a result with, so the functions are not
1053+ # called at all. The chunks are still yielded, and the turn is
1054+ # recorded once below, ending on the model's unanswered function call.
1055+ is_last_remote_call_afc = remaining_remote_calls_afc == 0
1056+ if is_last_remote_call_afc :
1057+ logger .info (
1058+ "Reached max remote calls for automatic function calling."
1059+ )
10371060
10381061 model_output = []
10391062 finish_reason = None
@@ -1046,7 +1069,8 @@ async def async_generator(): # type: ignore[no-untyped-def]
10461069 is_valid = False
10471070
10481071 if (
1049- function_map
1072+ not is_last_remote_call_afc
1073+ and function_map
10501074 and chunk .candidates
10511075 and chunk .candidates [0 ].content
10521076 and chunk .candidates [0 ].content .parts
@@ -1065,15 +1089,12 @@ async def async_generator(): # type: ignore[no-untyped-def]
10651089 finish_reason = chunk .candidates [0 ].finish_reason
10661090 yield chunk
10671091
1092+ if is_last_remote_call_afc :
1093+ break
10681094 if not function_map or not func_response_parts :
10691095 break
10701096
10711097 logger .info (f"AFC remote call { i } is done." )
1072- remaining_remote_calls_afc -= 1
1073- if remaining_remote_calls_afc == 0 :
1074- logger .info (
1075- "Reached max remote calls for automatic function calling."
1076- )
10771098
10781099 func_response_content = types .Content (
10791100 role = "user" , parts = func_response_parts
0 commit comments