Skip to content

Commit 5339c35

Browse files
committed
Add input validation to WS connect method
1 parent 97f2a5c commit 5339c35

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/agents/realtime/openai_realtime.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,25 @@ async def connect(self, options: RealtimeModelConfig) -> None:
208208

209209
self._playback_tracker = options.get("playback_tracker", None)
210210

211-
self.model = model_settings.get("model_name", self.model)
211+
call_id = options.get("call_id")
212+
model_name = model_settings.get("model_name")
213+
if call_id and model_name:
214+
error_message = (
215+
"Cannot specify both `call_id` and `model_name` "
216+
"when attaching to an existing realtime call."
217+
)
218+
raise UserError(error_message)
219+
220+
if model_name:
221+
self.model = model_name
222+
212223
api_key = await get_api_key(options.get("api_key"))
213224

214225
if "tracing" in model_settings:
215226
self._tracing_config = model_settings["tracing"]
216227
else:
217228
self._tracing_config = "auto"
218229

219-
call_id = options.get("call_id")
220230
if call_id:
221231
url = options.get("url", f"wss://api.openai.com/v1/realtime?call_id={call_id}")
222232
else:

tests/realtime/test_openai_realtime.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ async def test_connect_missing_api_key_raises_error(self, model):
5353
with pytest.raises(UserError, match="API key is required"):
5454
await model.connect(config)
5555

56+
@pytest.mark.asyncio
57+
async def test_connect_with_call_id_and_model_raises_error(self, model):
58+
"""Test that specifying both call_id and model raises UserError."""
59+
config = {
60+
"api_key": "test-api-key-123",
61+
"call_id": "call-123",
62+
"initial_model_settings": {"model_name": "gpt-4o-realtime-preview"},
63+
}
64+
65+
with pytest.raises(UserError, match="Cannot specify both `call_id` and `model_name`"):
66+
await model.connect(config)
67+
5668
@pytest.mark.asyncio
5769
async def test_connect_with_string_api_key(self, model, mock_websocket):
5870
"""Test successful connection with string API key."""

0 commit comments

Comments
 (0)