@@ -943,11 +943,13 @@ async def _run_single_turn_streamed(
943
943
input_items = input ,
944
944
system_instructions = system_prompt ,
945
945
)
946
-
946
+
947
947
# Call hook just before the model is invoked, with the correct system_prompt.
948
948
if agent .hooks :
949
- await agent .hooks .on_llm_start (context_wrapper , agent , filtered .instructions , filtered .input )
950
-
949
+ await agent .hooks .on_llm_start (
950
+ context_wrapper , agent , filtered .instructions , filtered .input
951
+ )
952
+
951
953
# 1. Stream the output events
952
954
async for event in model .stream_response (
953
955
filtered .instructions ,
@@ -985,7 +987,7 @@ async def _run_single_turn_streamed(
985
987
streamed_result ._event_queue .put_nowait (RawResponsesStreamEvent (data = event ))
986
988
987
989
# Call hook just after the model response is finalized.
988
- if agent .hooks :
990
+ if agent .hooks and final_response is not None :
989
991
await agent .hooks .on_llm_end (context_wrapper , agent , final_response )
990
992
991
993
# 2. At this point, the streaming is complete for this turn of the agent loop.
@@ -1266,8 +1268,8 @@ async def _get_new_response(
1266
1268
await agent .hooks .on_llm_start (
1267
1269
context_wrapper ,
1268
1270
agent ,
1269
- filtered .instructions , # Use filtered instructions
1270
- filtered .input # Use filtered input
1271
+ filtered .instructions , # Use filtered instructions
1272
+ filtered .input , # Use filtered input
1271
1273
)
1272
1274
1273
1275
new_response = await model .get_response (
@@ -1285,11 +1287,7 @@ async def _get_new_response(
1285
1287
)
1286
1288
# If the agent has hooks, we need to call them after the LLM call
1287
1289
if agent .hooks :
1288
- await agent .hooks .on_llm_end (
1289
- context_wrapper ,
1290
- agent ,
1291
- new_response
1292
- )
1290
+ await agent .hooks .on_llm_end (context_wrapper , agent , new_response )
1293
1291
1294
1292
context_wrapper .usage .add (new_response .usage )
1295
1293
@@ -1404,4 +1402,4 @@ async def _save_result_to_session(
1404
1402
def _copy_str_or_list (input : str | list [TResponseInputItem ]) -> str | list [TResponseInputItem ]:
1405
1403
if isinstance (input , str ):
1406
1404
return input
1407
- return input .copy ()
1405
+ return input .copy ()
0 commit comments