> This page is for Server SDKs.

> Fetch clean Markdown by appending `.md` to any page URL under https://signalwire.com/docs or requesting it with the HTTP header `Accept: text/markdown`. The root index at https://signalwire.com/docs/llms.txt lists the available documentation indexes.

# ToolRefusal

> An exception a tool handler raises to decline a call with an outcome it knows.

[define-tool]: /docs/server-sdks/reference/python/agents/agent-base/define-tool

[add-tool-middleware]: /docs/server-sdks/reference/python/agents/agent-base/add-tool-middleware

[functionresult]: /docs/server-sdks/reference/python/agents/function-result

Raise `ToolRefusal` from a tool handler, or from tool
[middleware][add-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()`][define-tool].

```python
from signalwire import ToolRefusal
```

## Parameters

**`tool_result`** `str` — required

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

---

**`tool_prompt`** `str | None` — default: None

What the model should do next.

---

## Methods

#### [result](/docs/server-sdks/reference/python/agents/tool-refusal/result)

Return the FunctionResult the model gets.

## Example

```python {5-6}
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:

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