Skip to content

fix: send error output to model when realtime tool call raises exception#3437

Closed
rmotgi1227 wants to merge 1 commit into
openai:mainfrom
rmotgi1227:fix/realtime-tool-exception-sends-output
Closed

fix: send error output to model when realtime tool call raises exception#3437
rmotgi1227 wants to merge 1 commit into
openai:mainfrom
rmotgi1227:fix/realtime-tool-exception-sends-output

Conversation

@rmotgi1227
Copy link
Copy Markdown

Summary

Closes #3356.

When a known tool raised an exception (ToolTimeoutError with timeout_behavior="raise_exception", or any user-raised error), the exception propagated out of _handle_tool_call, got logged by _on_tool_call_task_done, but no RealtimeModelSendToolOutput was ever sent. The model waited for a function result that never arrived — effectively hanging the session for ~30 seconds until the server-side timeout fired.

The same class of problem was already fixed for the unknown-tool branch in #3287, which ensures the model always gets a reply. The known-tool exception branch was the remaining gap.

Fix: Wrap invoke_function_tool in a try/except inside _handle_tool_call. On failure, format an error string via default_tool_error_function and send it as a tool output with start_response=True so the model can recover and continue. The exception is not re-raised — the task completes normally and _stored_exception is not set, keeping the session alive.

Before/after for the three affected paths:

Path Before After
Known tool success sends tool output ✓ unchanged ✓
Known tool exception/timeout no output — model hangs sends error output, session continues ✓
Unknown tool sends error output (fixed by #3287) ✓ unchanged ✓

Test plan

  • Updated test_function_tool_timeout_raise_exception_propagates — no longer expects ToolTimeoutError to propagate; now verifies a model-visible error output is sent
  • Updated test_function_call_event_timeout_raise_exception_enqueues_error_stored_exception is now None (session stays alive); model receives tool output instead of hanging
  • Updated test_function_tool_exception_handling — exception is now caught; model receives error output
uv run pytest tests/realtime/test_session.py -v   # 76 passed
uv run pytest tests/realtime/ -v                  # 244 passed
make lint
make typecheck

Issue number

Closes #3356.

Checks

  • I've added new tests (if relevant)
  • I've added/updated the relevant documentation (no doc changes needed)
  • I've run make lint and make format
  • I've made sure tests pass

Closes openai#3356.

When a known tool raised an exception (e.g. ToolTimeoutError with
timeout_behavior="raise_exception", or any user-raised error), the
exception propagated out of _handle_tool_call, got logged by
_on_tool_call_task_done, but NO RealtimeModelSendToolOutput was ever
sent. The model would then wait for a function result that never arrived
— effectively hanging the session for ~30 seconds until the server-side
timeout fired.

Wrap invoke_function_tool in a try/except. On failure, format an error
string via default_tool_error_function and send it as tool output with
start_response=True so the model can recover and continue. The exception
is not re-raised, so the task completes normally and _stored_exception
is not set (the session stays alive).

The pattern mirrors the existing unknown-tool branch (fixed in openai#3287):
both cases now guarantee the model always receives a tool output.

Update three tests that asserted the old "exception propagates, no
output sent" behavior to reflect the new correct contract.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@rmotgi1227
Copy link
Copy Markdown
Author

Closing in favor of existing PRs #3393 and #3435 that address the same issue. Apologies for the duplicate.

@rmotgi1227 rmotgi1227 closed this May 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Realtime known tool failures (exception/timeout) do not send model-visible output

1 participant