Skip to content

Commit c35c0af

Browse files
fix: #2204 Convert tool execution exception to runtime error (#2243)
1 parent 0994833 commit c35c0af

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/agents/realtime/session.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from typing_extensions import assert_never
99

1010
from ..agent import Agent
11-
from ..exceptions import ModelBehaviorError, UserError
11+
from ..exceptions import UserError
1212
from ..handoffs import Handoff
1313
from ..logger import logger
1414
from ..run_context import RunContextWrapper, TContext
@@ -493,7 +493,12 @@ async def _handle_tool_call(
493493
)
494494
)
495495
else:
496-
raise ModelBehaviorError(f"Tool {event.name} not found")
496+
await self._put_event(
497+
RealtimeError(
498+
info=self._event_info,
499+
error={"message": f"Tool {event.name} not found"},
500+
)
501+
)
497502

498503
@classmethod
499504
def _get_new_history(

tests/realtime/test_session.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,11 +1067,7 @@ async def test_handoff_tool_handling(self, mock_model):
10671067

10681068
@pytest.mark.asyncio
10691069
async def test_unknown_tool_handling(self, mock_model, mock_agent, mock_function_tool):
1070-
"""Test that unknown tools raise an error"""
1071-
import pytest
1072-
1073-
from agents.exceptions import ModelBehaviorError
1074-
1070+
"""Test that unknown tools emit a RealtimeError event"""
10751071
# Set up agent to return different tool than what's called
10761072
mock_function_tool.name = "known_tool"
10771073
mock_agent.get_all_tools.return_value = [mock_function_tool]
@@ -1083,9 +1079,14 @@ async def test_unknown_tool_handling(self, mock_model, mock_agent, mock_function
10831079
name="unknown_tool", call_id="call_unknown", arguments="{}"
10841080
)
10851081

1086-
# Should raise an error for unknown tool
1087-
with pytest.raises(ModelBehaviorError, match="Tool unknown_tool not found"):
1088-
await session._handle_tool_call(tool_call_event)
1082+
# Should emit a RealtimeError event for unknown tool
1083+
await session._handle_tool_call(tool_call_event)
1084+
1085+
# Should have emitted a RealtimeError event
1086+
assert session._event_queue.qsize() >= 1
1087+
error_event = await session._event_queue.get()
1088+
assert isinstance(error_event, RealtimeError)
1089+
assert "Tool unknown_tool not found" in error_event.error.get("message", "")
10891090

10901091
# Should not have called any tools
10911092
mock_function_tool.on_invoke_tool.assert_not_called()

0 commit comments

Comments
 (0)