Skip to content

Commit fc4633d

Browse files
committed
Save session on each turn instead of just at the end
1 parent dcf9cf7 commit fc4633d

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

src/agents/run.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ async def run(
400400
current_agent = starting_agent
401401
should_run_agent_start_hooks = True
402402

403+
# save the original input to the session if enabled
404+
await self._save_result_to_session(session, original_input, [])
405+
403406
try:
404407
while True:
405408
all_tools = await AgentRunner._get_all_tools(current_agent, context_wrapper)
@@ -497,9 +500,7 @@ async def run(
497500
output_guardrail_results=output_guardrail_results,
498501
context_wrapper=context_wrapper,
499502
)
500-
501-
# Save the conversation to session if enabled
502-
await self._save_result_to_session(session, input, result)
503+
await self._save_result_to_session(session, [], turn_result.new_step_items)
503504

504505
return result
505506
elif isinstance(turn_result.next_step, NextStepHandoff):
@@ -508,7 +509,7 @@ async def run(
508509
current_span = None
509510
should_run_agent_start_hooks = True
510511
elif isinstance(turn_result.next_step, NextStepRunAgain):
511-
pass
512+
await self._save_result_to_session(session, [], turn_result.new_step_items)
512513
else:
513514
raise AgentsException(
514515
f"Unknown next step type: {type(turn_result.next_step)}"
@@ -739,6 +740,8 @@ async def _start_streaming(
739740
# Update the streamed result with the prepared input
740741
streamed_result.input = prepared_input
741742

743+
await AgentRunner._save_result_to_session(session, starting_input, [])
744+
742745
while True:
743746
if streamed_result.is_complete:
744747
break
@@ -841,24 +844,11 @@ async def _start_streaming(
841844
streamed_result.is_complete = True
842845

843846
# Save the conversation to session if enabled
844-
# Create a temporary RunResult for session saving
845-
temp_result = RunResult(
846-
input=streamed_result.input,
847-
new_items=streamed_result.new_items,
848-
raw_responses=streamed_result.raw_responses,
849-
final_output=streamed_result.final_output,
850-
_last_agent=current_agent,
851-
input_guardrail_results=streamed_result.input_guardrail_results,
852-
output_guardrail_results=streamed_result.output_guardrail_results,
853-
context_wrapper=context_wrapper,
854-
)
855-
await AgentRunner._save_result_to_session(
856-
session, starting_input, temp_result
857-
)
847+
await AgentRunner._save_result_to_session(session, [], turn_result.new_step_items)
858848

859849
streamed_result._event_queue.put_nowait(QueueCompleteSentinel())
860850
elif isinstance(turn_result.next_step, NextStepRunAgain):
861-
pass
851+
await AgentRunner._save_result_to_session(session, [], turn_result.new_step_items)
862852
except AgentsException as exc:
863853
streamed_result.is_complete = True
864854
streamed_result._event_queue.put_nowait(QueueCompleteSentinel())
@@ -1379,7 +1369,7 @@ async def _save_result_to_session(
13791369
cls,
13801370
session: Session | None,
13811371
original_input: str | list[TResponseInputItem],
1382-
result: RunResult,
1372+
new_items: list[RunItem],
13831373
) -> None:
13841374
"""Save the conversation turn to session."""
13851375
if session is None:
@@ -1389,7 +1379,7 @@ async def _save_result_to_session(
13891379
input_list = ItemHelpers.input_to_new_input_list(original_input)
13901380

13911381
# Convert new items to input format
1392-
new_items_as_input = [item.to_input_item() for item in result.new_items]
1382+
new_items_as_input = [item.to_input_item() for item in new_items]
13931383

13941384
# Save all items from this turn
13951385
items_to_save = input_list + new_items_as_input

0 commit comments

Comments
 (0)