Skip to content

Commit 50d0ffe

Browse files
authored
fix: #1661 Preserve realtime session voice settings when updating agents (#1684)
## Summary Resolves #1661 - merge run and initial realtime session settings when constructing a session - reuse the merged base when recomputing settings so the active voice is preserved on handoffs or agent updates - add a regression test that ensures the initial voice and output audio format remain intact when refreshing session settings ## Testing - make format - make lint - make mypy *(fails: optional dependencies numpy, sqlalchemy, and litellm are not installed in the test environment)* - make tests *(fails: optional dependencies numpy and litellm are not installed in the test environment)*
1 parent dd0b65c commit 50d0ffe

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/agents/realtime/session.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ def __init__(
9595
self._history: list[RealtimeItem] = []
9696
self._model_config = model_config or {}
9797
self._run_config = run_config or {}
98+
initial_model_settings = self._model_config.get("initial_model_settings")
99+
run_config_settings = self._run_config.get("model_settings")
100+
self._base_model_settings: RealtimeSessionModelSettings = {
101+
**(run_config_settings or {}),
102+
**(initial_model_settings or {}),
103+
}
98104
self._event_queue: asyncio.Queue[RealtimeSessionEvent] = asyncio.Queue()
99105
self._closed = False
100106
self._stored_exception: Exception | None = None
@@ -619,9 +625,8 @@ async def _get_updated_model_settings_from_agent(
619625
starting_settings: RealtimeSessionModelSettings | None,
620626
agent: RealtimeAgent,
621627
) -> RealtimeSessionModelSettings:
622-
# Start with run config model settings as base
623-
run_config_settings = self._run_config.get("model_settings", {})
624-
updated_settings: RealtimeSessionModelSettings = run_config_settings.copy()
628+
# Start with the merged base settings from run and model configuration.
629+
updated_settings = self._base_model_settings.copy()
625630

626631
instructions, tools, handoffs = await asyncio.gather(
627632
agent.get_system_prompt(self._context_wrapper),

tests/realtime/test_session.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,47 @@ async def mock_get_handoffs(cls, agent, context_wrapper):
16061606
assert model_settings["tool_choice"] == "required"
16071607
assert model_settings["output_audio_format"] == "g711_ulaw"
16081608

1609+
@pytest.mark.asyncio
1610+
async def test_model_settings_preserve_initial_settings_on_updates(self):
1611+
"""Initial model settings should persist when we recompute settings for updates."""
1612+
1613+
agent = RealtimeAgent(name="test_agent", instructions="test")
1614+
agent.handoffs = []
1615+
agent.get_system_prompt = AsyncMock(return_value="test_prompt") # type: ignore
1616+
agent.get_all_tools = AsyncMock(return_value=[]) # type: ignore
1617+
1618+
mock_model = Mock(spec=RealtimeModel)
1619+
1620+
initial_settings: RealtimeSessionModelSettings = {
1621+
"voice": "initial_voice",
1622+
"output_audio_format": "pcm16",
1623+
}
1624+
1625+
session = RealtimeSession(
1626+
model=mock_model,
1627+
agent=agent,
1628+
context=None,
1629+
model_config={"initial_model_settings": initial_settings},
1630+
run_config={},
1631+
)
1632+
1633+
async def mock_get_handoffs(cls, agent, context_wrapper):
1634+
return []
1635+
1636+
with pytest.MonkeyPatch().context() as m:
1637+
m.setattr(
1638+
"agents.realtime.session.RealtimeSession._get_handoffs",
1639+
mock_get_handoffs,
1640+
)
1641+
1642+
model_settings = await session._get_updated_model_settings_from_agent(
1643+
starting_settings=None,
1644+
agent=agent,
1645+
)
1646+
1647+
assert model_settings["voice"] == "initial_voice"
1648+
assert model_settings["output_audio_format"] == "pcm16"
1649+
16091650

16101651
class TestUpdateAgentFunctionality:
16111652
"""Tests for update agent functionality in RealtimeSession"""

0 commit comments

Comments
 (0)