Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/agents/run_internal/run_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,12 @@ async def _save_stream_items_without_count(
if streamed_result._state is not None:
streamed_result._state._current_step = NextStepRunAgain()

await _save_stream_items_with_count(
turn_session_items,
turn_result.model_response.response_id,
store_setting,
)

if streamed_result._cancel_mode == "after_turn": # type: ignore[comparison-overlap]
streamed_result.is_complete = True
streamed_result._event_queue.put_nowait(QueueCompleteSentinel())
Expand Down
36 changes: 36 additions & 0 deletions tests/test_agent_runner_streamed.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,42 @@ async def test_tool_call_runs():
)


@pytest.mark.asyncio
async def test_streamed_run_again_persists_tool_items_to_session():
model = FakeModel()
call_id = "call-session-run-again"
agent = Agent(
name="test",
model=model,
tools=[get_function_tool("foo", "tool_result")],
)
session = SimpleListSession()

model.add_multiple_turn_outputs(
[
[get_function_tool_call("foo", json.dumps({"a": "b"}), call_id=call_id)],
[get_text_message("done")],
]
)

result = Runner.run_streamed(agent, input="user_message", session=session)
await consume_stream(result)

saved_items = await session.get_items()
assert any(
isinstance(item, dict)
and item.get("type") == "function_call"
and item.get("call_id") == call_id
for item in saved_items
)
assert any(
isinstance(item, dict)
and item.get("type") == "function_call_output"
and item.get("call_id") == call_id
for item in saved_items
)


@pytest.mark.asyncio
async def test_handoffs():
model = FakeModel()
Expand Down