@@ -1166,6 +1166,53 @@ def _get_latest_user_contents(
11661166 return latest_user_contents
11671167
11681168
1169+ async def _create_interactions (
1170+ api_client : Client ,
1171+ * ,
1172+ create_kwargs : dict [str , Any ],
1173+ stream : bool ,
1174+ ) -> AsyncGenerator [LlmResponse , None ]:
1175+ """Issue ``interactions.create`` and convert the response(s) to LlmResponses.
1176+
1177+ This is the shared transport + conversion loop. The caller assembles
1178+ ``create_kwargs`` (``model`` or ``agent``, ``input``, ``tools``, etc.); this
1179+ helper owns issuing the call and mapping the stream to ``LlmResponse``s.
1180+
1181+ Args:
1182+ api_client: The Google GenAI client.
1183+ create_kwargs: Keyword arguments passed verbatim to
1184+ ``api_client.aio.interactions.create`` (excluding ``stream``).
1185+ stream: Whether to stream the response.
1186+
1187+ Yields:
1188+ LlmResponse objects converted from interaction responses.
1189+ """
1190+ current_interaction_id : str | None = None
1191+
1192+ if stream :
1193+ responses = await api_client .aio .interactions .create (
1194+ ** create_kwargs , stream = True
1195+ )
1196+ aggregated_parts : list [types .Part ] = []
1197+ async for event in responses :
1198+ logger .debug (build_interactions_event_log (event ))
1199+ interaction_id = _extract_stream_interaction_id (event )
1200+ if interaction_id :
1201+ current_interaction_id = interaction_id
1202+ llm_response = convert_interaction_event_to_llm_response (
1203+ event , aggregated_parts , current_interaction_id
1204+ )
1205+ if llm_response :
1206+ yield llm_response
1207+ else :
1208+ interaction = await api_client .aio .interactions .create (
1209+ ** create_kwargs , stream = False
1210+ )
1211+ logger .info ('Interaction response received.' )
1212+ logger .debug (build_interactions_response_log (interaction ))
1213+ yield convert_interaction_to_llm_response (interaction )
1214+
1215+
11691216async def generate_content_via_interactions (
11701217 api_client : Client ,
11711218 llm_request : LlmRequest ,
@@ -1227,49 +1274,18 @@ async def generate_content_via_interactions(
12271274 )
12281275 )
12291276
1230- # Track the current interaction ID from responses
1231- current_interaction_id : str | None = None
1232-
1233- if stream :
1234- # Streaming mode
1235- responses = await api_client .aio .interactions .create (
1236- model = llm_request .model ,
1237- input = input_steps ,
1238- stream = True ,
1239- system_instruction = system_instruction ,
1240- tools = interaction_tools if interaction_tools else None ,
1241- generation_config = generation_config if generation_config else None ,
1242- previous_interaction_id = previous_interaction_id ,
1243- )
1244-
1245- aggregated_parts : list [types .Part ] = []
1246- async for event in responses :
1247- # Log the streaming event
1248- logger .debug (build_interactions_event_log (event ))
1249-
1250- interaction_id = _extract_stream_interaction_id (event )
1251- if interaction_id :
1252- current_interaction_id = interaction_id
1253- llm_response = convert_interaction_event_to_llm_response (
1254- event , aggregated_parts , current_interaction_id
1255- )
1256- if llm_response :
1257- yield llm_response
1258-
1259- else :
1260- # Non-streaming mode
1261- interaction = await api_client .aio .interactions .create (
1262- model = llm_request .model ,
1263- input = input_steps ,
1264- stream = False ,
1265- system_instruction = system_instruction ,
1266- tools = interaction_tools if interaction_tools else None ,
1267- generation_config = generation_config if generation_config else None ,
1268- previous_interaction_id = previous_interaction_id ,
1269- )
1270-
1271- # Log the response
1272- logger .info ('Interaction response received from the model.' )
1273- logger .debug (build_interactions_response_log (interaction ))
1274-
1275- yield convert_interaction_to_llm_response (interaction )
1277+ # Assemble the create() kwargs for the model path and delegate the
1278+ # transport + conversion loop to the shared helper.
1279+ create_kwargs : dict [str , Any ] = {
1280+ 'model' : llm_request .model ,
1281+ 'input' : input_steps ,
1282+ 'system_instruction' : system_instruction ,
1283+ 'tools' : interaction_tools if interaction_tools else None ,
1284+ 'generation_config' : generation_config if generation_config else None ,
1285+ 'previous_interaction_id' : previous_interaction_id ,
1286+ }
1287+
1288+ async for llm_response in _create_interactions (
1289+ api_client , create_kwargs = create_kwargs , stream = stream
1290+ ):
1291+ yield llm_response
0 commit comments