Skip to navigation
Agents

ToolRefusal

View as MarkdownOpen in Claude

Raise ToolRefusal from a tool handler, or from tool middleware, to decline the call with an outcome you know, such as an item out of stock or an account that isn’t found. The model gets tool_result, which says what is true now, and tool_prompt, which says what to do next.

Any other exception a handler raises is logged, not sent: the model is told the function failed before it finished, so whether it took effect is unknown. See the handler parameter of define_tool().

from signalwire import ToolRefusal

Parameters

tool_result
strRequired

What is true now: that nothing was done, and why. It’s also the exception’s message.

tool_prompt
str | NoneDefaults to None

What the model should do next.

Methods

Example

from signalwire import FunctionResult, ToolRefusal
def place_order(args, raw_data):
if args.get("item") == "tea":
raise ToolRefusal("place_order was not run: tea is out of stock.",
"Tell the caller, and offer coffee.")
return FunctionResult("Order placed.")

The model gets:

{"response": {"tool_result": "place_order was not run: tea is out of stock.",
"tool_prompt": "Tell the caller, and offer coffee."}}