diff --git a/examples/basic/previous_response_id.py b/examples/basic/previous_response_id.py new file mode 100644 index 00000000..b00bf3aa --- /dev/null +++ b/examples/basic/previous_response_id.py @@ -0,0 +1,66 @@ +import asyncio + +from agents import Agent, Runner + +"""This demonstrates usage of the `previous_response_id` parameter to continue a conversation. +The second run passes the previous response ID to the model, which allows it to continue the +conversation without re-sending the previous messages. + +Notes: +1. This only applies to the OpenAI Responses API. Other models will ignore this parameter. +2. Responses are only stored for 30 days as of this writing, so in production you should +store the response ID along with an expiration date; if the response is no longer valid, +you'll need to re-send the previous conversation history. +""" + + +async def main(): + agent = Agent( + name="Assistant", + instructions="You are a helpful assistant. be VERY concise.", + ) + + result = await Runner.run(agent, "What is the largest country in South America?") + print(result.final_output) + # Brazil + + result = await Runner.run( + agent, + "What is the capital of that country?", + previous_response_id=result.last_response_id, + ) + print(result.final_output) + # Brasilia + + +async def main_stream(): + agent = Agent( + name="Assistant", + instructions="You are a helpful assistant. be VERY concise.", + ) + + result = Runner.run_streamed(agent, "What is the largest country in South America?") + + async for event in result.stream_events(): + if event.type == "raw_response_event" and event.data.type == "response.output_text.delta": + print(event.data.delta, end="", flush=True) + + print() + + result = Runner.run_streamed( + agent, + "What is the capital of that country?", + previous_response_id=result.last_response_id, + ) + + async for event in result.stream_events(): + if event.type == "raw_response_event" and event.data.type == "response.output_text.delta": + print(event.data.delta, end="", flush=True) + + +if __name__ == "__main__": + is_stream = input("Run in stream mode? (y/n): ") + if is_stream == "y": + asyncio.run(main_stream()) + else: + asyncio.run(main()) diff --git a/pyproject.toml b/pyproject.toml index c3e46a66..1eefadd9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "openai-agents" -version = "0.0.10" +version = "0.0.11" description = "OpenAI Agents SDK" readme = "README.md" requires-python = ">=3.9" diff --git a/src/agents/models/openai_chatcompletions.py b/src/agents/models/openai_chatcompletions.py index 712a7998..a4f93cb7 100644 --- a/src/agents/models/openai_chatcompletions.py +++ b/src/agents/models/openai_chatcompletions.py @@ -528,7 +528,9 @@ async def _fetch_response( reasoning_effort = model_settings.reasoning.effort if model_settings.reasoning else None store = _Converter.get_store_param(self._get_client(), model_settings) - stream_options = _Converter.get_stream_options_param(self._get_client(), model_settings) + stream_options = _Converter.get_stream_options_param( + self._get_client(), model_settings, stream=stream + ) ret = await self._get_client().chat.completions.create( model=self.model, @@ -591,8 +593,11 @@ def get_store_param(cls, client: AsyncOpenAI, model_settings: ModelSettings) -> @classmethod def get_stream_options_param( - cls, client: AsyncOpenAI, model_settings: ModelSettings + cls, client: AsyncOpenAI, model_settings: ModelSettings, stream: bool ) -> dict[str, bool] | None: + if not stream: + return None + default_include_usage = True if cls.is_openai(client) else None include_usage = ( model_settings.include_usage diff --git a/tests/fake_model.py b/tests/fake_model.py index 203479d0..52d3a3b2 100644 --- a/tests/fake_model.py +++ b/tests/fake_model.py @@ -63,6 +63,7 @@ async def get_response( "model_settings": model_settings, "tools": tools, "output_schema": output_schema, + "previous_response_id": previous_response_id, } with generation_span(disabled=not self.tracing_enabled) as span: @@ -98,6 +99,14 @@ async def stream_response( *, previous_response_id: str | None, ) -> AsyncIterator[TResponseStreamEvent]: + self.last_turn_args = { + "system_instructions": system_instructions, + "input": input, + "model_settings": model_settings, + "tools": tools, + "output_schema": output_schema, + "previous_response_id": previous_response_id, + } with generation_span(disabled=not self.tracing_enabled) as span: output = self.get_next_output() if isinstance(output, Exception): diff --git a/tests/test_agent_runner.py b/tests/test_agent_runner.py index 4f277656..14a278a9 100644 --- a/tests/test_agent_runner.py +++ b/tests/test_agent_runner.py @@ -662,3 +662,86 @@ async def test_model_settings_override(): # temperature is overridden by Runner.run, but max_tokens is not assert model.last_turn_args["model_settings"].temperature == 0.5 assert model.last_turn_args["model_settings"].max_tokens == 1000 + + +@pytest.mark.asyncio +async def test_previous_response_id_passed_between_runs(): + """Test that previous_response_id is passed to the model on subsequent runs.""" + model = FakeModel() + model.set_next_output([get_text_message("done")]) + agent = Agent(name="test", model=model) + + assert model.last_turn_args.get("previous_response_id") is None + await Runner.run(agent, input="test", previous_response_id="resp-non-streamed-test") + assert model.last_turn_args.get("previous_response_id") == "resp-non-streamed-test" + + +@pytest.mark.asyncio +async def test_multi_turn_previous_response_id_passed_between_runs(): + """Test that previous_response_id is passed to the model on subsequent runs.""" + + model = FakeModel() + agent = Agent( + name="test", + model=model, + tools=[get_function_tool("foo", "tool_result")], + ) + + model.add_multiple_turn_outputs( + [ + # First turn: a message and tool call + [get_text_message("a_message"), get_function_tool_call("foo", json.dumps({"a": "b"}))], + # Second turn: text message + [get_text_message("done")], + ] + ) + + assert model.last_turn_args.get("previous_response_id") is None + await Runner.run(agent, input="test", previous_response_id="resp-test-123") + assert model.last_turn_args.get("previous_response_id") == "resp-test-123" + + +@pytest.mark.asyncio +async def test_previous_response_id_passed_between_runs_streamed(): + """Test that previous_response_id is passed to the model on subsequent streamed runs.""" + model = FakeModel() + model.set_next_output([get_text_message("done")]) + agent = Agent( + name="test", + model=model, + ) + + assert model.last_turn_args.get("previous_response_id") is None + result = Runner.run_streamed(agent, input="test", previous_response_id="resp-stream-test") + async for _ in result.stream_events(): + pass + + assert model.last_turn_args.get("previous_response_id") == "resp-stream-test" + + +@pytest.mark.asyncio +async def test_previous_response_id_passed_between_runs_streamed_multi_turn(): + """Test that previous_response_id is passed to the model on subsequent streamed runs.""" + + model = FakeModel() + agent = Agent( + name="test", + model=model, + tools=[get_function_tool("foo", "tool_result")], + ) + + model.add_multiple_turn_outputs( + [ + # First turn: a message and tool call + [get_text_message("a_message"), get_function_tool_call("foo", json.dumps({"a": "b"}))], + # Second turn: text message + [get_text_message("done")], + ] + ) + + assert model.last_turn_args.get("previous_response_id") is None + result = Runner.run_streamed(agent, input="test", previous_response_id="resp-stream-test") + async for _ in result.stream_events(): + pass + + assert model.last_turn_args.get("previous_response_id") == "resp-stream-test" diff --git a/uv.lock b/uv.lock index 1212f790..d4b82546 100644 --- a/uv.lock +++ b/uv.lock @@ -1108,7 +1108,7 @@ wheels = [ [[package]] name = "openai-agents" -version = "0.0.10" +version = "0.0.11" source = { editable = "." } dependencies = [ { name = "griffe" },