Skip to content

feat: add tool_call_error_function to AssistantAgent for custom error handling#7387

Open
majiayu000 wants to merge 1 commit intomicrosoft:mainfrom
majiayu000:fix/issue-5340-tool-call-error-handling
Open

feat: add tool_call_error_function to AssistantAgent for custom error handling#7387
majiayu000 wants to merge 1 commit intomicrosoft:mainfrom
majiayu000:fix/issue-5340-tool-call-error-handling

Conversation

@majiayu000
Copy link

Summary

  • Adds tool_call_error_function parameter to AssistantAgent that allows callers to control how tool execution errors are handled
  • When the callback returns a str, it is used as the error content (recoverable error)
  • When the callback returns None, the original exception is re-raised (fatal error)
  • Without the callback (default), behavior is unchanged — errors are stringified as before

Fixes #5340

Changes

  • StaticWorkbench / StaticStreamWorkbench: added raise_on_error parameter to control exception propagation
  • AssistantAgent.__init__: added tool_call_error_function parameter
  • AssistantAgent._execute_tool_call: wrapped workbench call in try/except, delegates to error function
  • AssistantAgent._execute_tool_calls: wrapped asyncio.gather in try/except to ensure stream sentinel (None) is sent on error, preventing consumer loop hangs
  • Added 3 tests covering: fatal error (re-raise), handled error (custom string), and default behavior (backward compatible)

Test plan

  • pytest tests/test_assistant_agent.py::TestToolCallErrorFunction — 3/3 pass
  • pyright type checking — 0 errors on both modified source files

… handling

Allow tool call exceptions to be handled by a user-provided callback
instead of being silently stringified. The callback receives the
exception and FunctionCall, and returns either a string (handled error)
or None (re-raise as fatal).

- Add raise_on_error parameter to StaticWorkbench/StaticStreamWorkbench
- Thread tool_call_error_function through AssistantAgent pipeline
- Wrap asyncio.gather in try/except to ensure stream sentinel on error
- Add tests for all error handling paths

Fixes microsoft#5340

Signed-off-by: majiayu000 <1835304752@qq.com>
@majiayu000 majiayu000 marked this pull request as ready for review March 13, 2026 12:04
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.

Allow tool call to be fatal instead of always passing info as tool execution result

1 participant