Agents

FunctionResult

View as MarkdownOpen in Claude

FunctionResult is the return type for all SWAIG tool functions. It wraps a response (a prompt the AI reads, not text it speaks verbatim) and an ordered list of actions (transfers, SMS, data updates, context switches, and more). Every method returns self, so you can chain calls into a single fluent expression.

Returned from functions defined with the @tool() decorator or define_tool() on AgentBase.

FunctionResult builds the response payload for a SWAIG function. See the SWML SWAIG functions reference for the full response format specification.

Parameters

response
str | dict[str, Any] | NoneDefaults to None

A prompt injected into the model’s context after the function executes. The model reads it and decides what to say, so write it as an instruction in the second person: "Tell the caller their order shipped Tuesday." rather than "Your order shipped Tuesday.". A first-person script usually still works because the model tends to repeat it, but it is interpreted, not spoken, and can drift or be merged with other context.

post_process
boolDefaults to False

Let the AI take one more turn before executing actions. Set it whenever the caller must hear something before an action that ends or suspends the AI’s turn: hold pauses speech detection, connect and transfer replace the leg, and hangup ends the call.

tool_result
str | NoneDefaults to None

Factual outcome of the call, such as "status: on hold". When given, the response takes the structured form described under set_tool_response().

tool_prompt
str | NoneDefaults to None

Instruction for what the model should say next, in the second person. Pairs with tool_result.

Properties

response
str | dict[str, Any]Defaults to ''

The prompt injected into the model’s context. Either a plain string or a {"tool_result": ..., "tool_prompt": ...} object built by set_tool_response().

action
list[dict[str, Any]]Defaults to []

Ordered list of action objects to execute. Actions run sequentially in the order they were added.

post_process
boolDefaults to False

When True, the AI responds to the prompt and takes one more conversational turn with the user before executing actions. When False (default), actions execute immediately after the response.

Example

from signalwire import AgentBase
from signalwire import FunctionResult
agent = AgentBase(name="my-agent", route="/agent")
agent.set_prompt_text("You are a helpful assistant.")
@agent.tool(name="transfer_to_billing", description="Transfer the caller to billing")
def transfer_to_billing(args, raw_data):
return (
FunctionResult(
"Tell the caller you are transferring them to billing and ask if they need anything else first.",
post_process=True
)
.update_global_data({"transferred": True})
.send_sms(
to_number="+15551234567",
from_number="+15559876543",
body="You are being transferred to billing."
)
.connect("+15551234567", final=True)
)
agent.serve()

Fluent Chaining Pattern

Every method on FunctionResult returns self, so you build complex responses in a single expression. Actions execute in the order they are added.

Terminal actions like connect(final=True) and hangup() end the call flow. Place them last in the chain so that preceding actions (data updates, SMS, etc.) have a chance to execute.

from signalwire import AgentBase
from signalwire import FunctionResult
agent = AgentBase(name="my-agent", route="/agent")
agent.set_prompt_text("You are a helpful assistant.")
@agent.tool(name="transfer_call", description="Transfer the call")
def transfer_call(args, raw_data):
# Data update + SMS execute before the terminal transfer
return (
FunctionResult("Tell the caller you are transferring them now.")
.update_global_data({"transferred": True})
.send_sms(
to_number="+15551234567",
from_number="+15559876543",
body="Your call is being transferred."
)
.connect("+15551234567", final=True) # terminal — goes last
)
agent.serve()

Methods

Core

Call Control

Speech

Media

Data

Context Navigation

Events

Functions

Hints

Settings

SMS

Payment

SIP

Rooms and Conferences

SWML and RPC