Skip to content

Python SDK: add ToolError exception to surface tool failures to the LLM#1404

Open
idryzhov wants to merge 1 commit into
github:mainfrom
idryzhov:python-add-tool-error
Open

Python SDK: add ToolError exception to surface tool failures to the LLM#1404
idryzhov wants to merge 1 commit into
github:mainfrom
idryzhov:python-add-tool-error

Conversation

@idryzhov
Copy link
Copy Markdown

@idryzhov idryzhov commented May 23, 2026

Tool handlers can now raise ToolError("message") to return a structured failure result whose message is intentionally forwarded to the LLM as text_result_for_llm.

This simplifies handler code: a tool that wants to signal a recoverable failure to the LLM can simply raise ToolError("...") instead of having to construct and return a full ToolResult(result_type="failure", ...) manually. The raise-and-return paths now look symmetric for success and failure.

Other exception types continue to be caught and hidden behind the generic Invoking this tool produced an error message for security.

Copilot AI review requested due to automatic review settings May 23, 2026 21:21
@idryzhov idryzhov requested a review from a team as a code owner May 23, 2026 21:21
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a first-class way for tool handlers to intentionally return an LLM-visible failure message via a new ToolError exception.

Changes:

  • Introduces ToolError exception type to represent intentional, user-facing tool failures.
  • Updates tool invocation wrapper to catch ToolError and surface its message verbatim to the LLM.
  • Adds a unit test asserting ToolError propagation behavior and _from_exception semantics.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
python/copilot/tools.py Adds ToolError and special-case handling to return an LLM-visible failure ToolResult.
python/copilot/init.py Exports ToolError as part of the public package API.
python/test_tools.py Adds coverage ensuring ToolError is surfaced to the LLM as intended.

Comment thread python/copilot/tools.py Outdated
Comment thread python/copilot/tools.py
Tool handlers can now raise ToolError("message") to return a structured
failure result whose message is intentionally forwarded to the LLM as
text_result_for_llm.

This simplifies handler code: a tool that wants to signal a recoverable
failure to the LLM can simply 'raise ToolError("...")' instead of having
to construct and return a full ToolResult(result_type="failure", ...)
manually. The raise-and-return paths now look symmetric for success and
failure.

Other exception types continue to be caught and hidden behind the generic
'Invoking this tool produced an error' message for security.
@idryzhov idryzhov force-pushed the python-add-tool-error branch from 6f21043 to e801a04 Compare May 23, 2026 21:32
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.

2 participants