|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import asyncio |
4 | | -import copy |
5 | 4 | import inspect |
6 | 5 | from dataclasses import dataclass, field |
7 | 6 | from typing import Any, Callable, Generic, cast |
@@ -387,7 +386,7 @@ async def run( |
387 | 386 | disabled=run_config.tracing_disabled, |
388 | 387 | ): |
389 | 388 | current_turn = 0 |
390 | | - original_input: str | list[TResponseInputItem] = copy.deepcopy(prepared_input) |
| 389 | + original_input: str | list[TResponseInputItem] = _copy_str_or_list(prepared_input) |
391 | 390 | generated_items: list[RunItem] = [] |
392 | 391 | model_responses: list[ModelResponse] = [] |
393 | 392 |
|
@@ -446,7 +445,7 @@ async def run( |
446 | 445 | starting_agent, |
447 | 446 | starting_agent.input_guardrails |
448 | 447 | + (run_config.input_guardrails or []), |
449 | | - copy.deepcopy(prepared_input), |
| 448 | + _copy_str_or_list(prepared_input), |
450 | 449 | context_wrapper, |
451 | 450 | ), |
452 | 451 | self._run_single_turn( |
@@ -594,7 +593,7 @@ def run_streamed( |
594 | 593 | ) |
595 | 594 |
|
596 | 595 | streamed_result = RunResultStreaming( |
597 | | - input=copy.deepcopy(input), |
| 596 | + input=_copy_str_or_list(input), |
598 | 597 | new_items=[], |
599 | 598 | current_agent=starting_agent, |
600 | 599 | raw_responses=[], |
@@ -647,7 +646,7 @@ async def _maybe_filter_model_input( |
647 | 646 |
|
648 | 647 | try: |
649 | 648 | model_input = ModelInputData( |
650 | | - input=copy.deepcopy(effective_input), |
| 649 | + input=effective_input.copy(), |
651 | 650 | instructions=effective_instructions, |
652 | 651 | ) |
653 | 652 | filter_payload: CallModelData[TContext] = CallModelData( |
@@ -786,7 +785,7 @@ async def _start_streaming( |
786 | 785 | cls._run_input_guardrails_with_queue( |
787 | 786 | starting_agent, |
788 | 787 | starting_agent.input_guardrails + (run_config.input_guardrails or []), |
789 | | - copy.deepcopy(ItemHelpers.input_to_new_input_list(prepared_input)), |
| 788 | + ItemHelpers.input_to_new_input_list(prepared_input), |
790 | 789 | context_wrapper, |
791 | 790 | streamed_result, |
792 | 791 | current_span, |
@@ -1376,3 +1375,9 @@ async def _save_result_to_session( |
1376 | 1375 |
|
1377 | 1376 |
|
1378 | 1377 | DEFAULT_AGENT_RUNNER = AgentRunner() |
| 1378 | + |
| 1379 | + |
| 1380 | +def _copy_str_or_list(input: str | list[TResponseInputItem]) -> str | list[TResponseInputItem]: |
| 1381 | + if isinstance(input, str): |
| 1382 | + return input |
| 1383 | + return input.copy() |
0 commit comments