Skip to content

Commit 237d672

Browse files
Add functionality to update agent during realtime session (#1366)
Adding support to update current agent during a Realtime Session much like the TS/JS method: https://openai.github.io/openai-agents-js/openai/agents-realtime/classes/realtimesession/#updateagent
1 parent 907533a commit 237d672

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/agents/realtime/session.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ async def interrupt(self) -> None:
180180
"""Interrupt the model."""
181181
await self._model.send_event(RealtimeModelSendInterrupt())
182182

183+
async def update_agent(self, agent: RealtimeAgent) -> None:
184+
"""Update the active agent for this session and apply its settings to the model."""
185+
self._current_agent = agent
186+
187+
updated_settings = await self._get_updated_model_settings_from_agent(
188+
starting_settings=None,
189+
agent=self._current_agent,
190+
)
191+
192+
await self._model.send_event(
193+
RealtimeModelSendSessionUpdate(session_settings=updated_settings)
194+
)
195+
183196
async def on_event(self, event: RealtimeModelEvent) -> None:
184197
await self._put_event(RealtimeRawModelEvent(data=event, info=self._event_info))
185198

tests/realtime/test_session.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
RealtimeModelTurnEndedEvent,
4646
RealtimeModelTurnStartedEvent,
4747
)
48+
from agents.realtime.model_inputs import RealtimeModelSendSessionUpdate
4849
from agents.realtime.session import RealtimeSession
4950
from agents.tool import FunctionTool
5051
from agents.tool_context import ToolContext
@@ -1488,3 +1489,24 @@ async def mock_get_handoffs(cls, agent, context_wrapper):
14881489
assert model_settings["voice"] == "model_config_only_voice"
14891490
assert model_settings["tool_choice"] == "required"
14901491
assert model_settings["output_audio_format"] == "g711_ulaw"
1492+
1493+
1494+
class TestUpdateAgentFunctionality:
1495+
"""Tests for update agent functionality in RealtimeSession"""
1496+
1497+
@pytest.mark.asyncio
1498+
async def test_update_agent_creates_handoff_and_session_update_event(self, mock_model):
1499+
first_agent = RealtimeAgent(name="first", instructions="first", tools=[], handoffs=[])
1500+
second_agent = RealtimeAgent(name="second", instructions="second", tools=[], handoffs=[])
1501+
1502+
session = RealtimeSession(mock_model, first_agent, None)
1503+
1504+
await session.update_agent(second_agent)
1505+
1506+
# Should have sent session update
1507+
session_update_event = mock_model.sent_events[0]
1508+
assert isinstance(session_update_event, RealtimeModelSendSessionUpdate)
1509+
assert session_update_event.session_settings["instructions"] == "second"
1510+
1511+
# Check that the current agent and session settings are updated
1512+
assert session._current_agent == second_agent

0 commit comments

Comments
 (0)