diff --git a/src/agents/run.py b/src/agents/run.py index 5b25df4f2..461a2407f 100644 --- a/src/agents/run.py +++ b/src/agents/run.py @@ -640,6 +640,7 @@ async def run( model_responses.append(turn_result.model_response) original_input = turn_result.original_input generated_items = turn_result.generated_items + context_wrapper.run_items = generated_items if server_conversation_tracker is not None: server_conversation_tracker.track_server_items(turn_result.model_response) @@ -1080,6 +1081,7 @@ async def _start_streaming( ] streamed_result.input = turn_result.original_input streamed_result.new_items = turn_result.generated_items + context_wrapper.run_items = turn_result.generated_items if server_conversation_tracker is not None: server_conversation_tracker.track_server_items(turn_result.model_response) diff --git a/src/agents/run_context.py b/src/agents/run_context.py index 579a215f2..490aa21ac 100644 --- a/src/agents/run_context.py +++ b/src/agents/run_context.py @@ -1,16 +1,21 @@ from dataclasses import dataclass, field -from typing import Any, Generic +from typing import Any, Generic, TYPE_CHECKING -from typing_extensions import TypeVar +from typing_extensions import TypeVar from .usage import Usage +if TYPE_CHECKING: + from .items import RunItem + TContext = TypeVar("TContext", default=Any) + @dataclass class RunContextWrapper(Generic[TContext]): - """This wraps the context object that you passed to `Runner.run()`. It also contains + """ + This wraps the context object that you passed to `Runner.run()`. It also contains information about the usage of the agent run so far. NOTE: Contexts are not passed to the LLM. They're a way to pass dependencies and data to code @@ -24,3 +29,6 @@ class RunContextWrapper(Generic[TContext]): """The usage of the agent run so far. For streamed responses, the usage will be stale until the last chunk of the stream is processed. """ + + run_items: list["RunItem"] = field(default_factory=list) + """The items generated by the agent so far."""