Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/agents/realtime/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing_extensions import assert_never

from ..agent import Agent
from ..exceptions import ModelBehaviorError, UserError
from ..exceptions import UserError
from ..handoffs import Handoff
from ..logger import logger
from ..run_context import RunContextWrapper, TContext
Expand Down Expand Up @@ -493,7 +493,12 @@ async def _handle_tool_call(
)
)
else:
raise ModelBehaviorError(f"Tool {event.name} not found")
await self._put_event(
RealtimeError(
info=self._event_info,
error={"message": f"Tool {event.name} not found"},
)
)

@classmethod
def _get_new_history(
Expand Down
17 changes: 9 additions & 8 deletions tests/realtime/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,11 +1067,7 @@ async def test_handoff_tool_handling(self, mock_model):

@pytest.mark.asyncio
async def test_unknown_tool_handling(self, mock_model, mock_agent, mock_function_tool):
"""Test that unknown tools raise an error"""
import pytest

from agents.exceptions import ModelBehaviorError

"""Test that unknown tools emit a RealtimeError event"""
# Set up agent to return different tool than what's called
mock_function_tool.name = "known_tool"
mock_agent.get_all_tools.return_value = [mock_function_tool]
Expand All @@ -1083,9 +1079,14 @@ async def test_unknown_tool_handling(self, mock_model, mock_agent, mock_function
name="unknown_tool", call_id="call_unknown", arguments="{}"
)

# Should raise an error for unknown tool
with pytest.raises(ModelBehaviorError, match="Tool unknown_tool not found"):
await session._handle_tool_call(tool_call_event)
# Should emit a RealtimeError event for unknown tool
await session._handle_tool_call(tool_call_event)

# Should have emitted a RealtimeError event
assert session._event_queue.qsize() >= 1
error_event = await session._event_queue.get()
assert isinstance(error_event, RealtimeError)
assert "Tool unknown_tool not found" in error_event.error.get("message", "")

# Should not have called any tools
mock_function_tool.on_invoke_tool.assert_not_called()
Expand Down