This chapter covers everything about SWAIG functions:

Here’s a complete agent with a SWAIG function:
Handler Functions: Python code that runs on your server when the AI decides to call a function. You have full access to databases, APIs, and any Python library.
FunctionResult: The return type for all SWAIG functions. Contains the response text the AI will speak and optional actions to execute.
Parameters: JSON Schema definitions that tell the AI what arguments your function accepts. The AI will extract these from the conversation.
Actions: Side effects like call transfers, SMS sending, or context changes that execute after the function completes.
DataMap: A way to define functions that call REST APIs without running any code on your server - the API calls happen directly on SignalWire’s infrastructure.
Let’s start by learning how to define functions.
The define_tool() method is available in all SDK languages. The parameters JSON Schema is identical everywhere; only the calling syntax differs:
Required Parameters:
Optional Parameters:
All handlers receive two arguments: the parsed function arguments and the full request data. The handler returns a FunctionResult with response text and optional actions.
The handler signature across languages:
Register as many functions as your agent needs:
Add per-function filler phrases for when the function is executing:
The @tool decorator is a Python-specific convenience. Other languages achieve the same result using define_tool() / defineTool() as shown in the examples above.
Alternative syntax using decorators:
Both methods register SWAIG functions with the same result. Choose based on your coding style and requirements.
Conditional function registration:
Dynamic functions from configuration:
Handlers defined outside the class:
Static, self-documenting functions:
The decorator keeps the function metadata with the implementation, making it easier to see what a function does at a glance.
Type inference from function signatures is a Python-specific feature. Other languages require explicit JSON Schema parameter definitions via define_tool() / defineTool().
When using the @tool decorator (or define_tool()) without explicit parameters, the SDK automatically infers the JSON Schema from Python type hints. This eliminates boilerplate for straightforward functions.
How type inference works:
Rules:
Optional are marked as requiredArgs: docstring sections provide per-parameter descriptionsraw_data parameter is recognized and passed through (not included in schema)(args, raw_data) conventionAccessing raw_data with typed functions:
When to use type inference vs explicit parameters:
You can use both in the same agent:
The register_swaig_function() method registers a function definition object directly (used with DataMap and external definitions). This is a lower-level method than define_tool():
Route function calls to an external webhook:
By default, functions require token validation. Disable for testing:
The description helps the AI decide when to use your function:
Use swaig-test to test your functions:
The following example is shown in Python. All concepts demonstrated (multiple define_tool() calls, fillers, handlers) work identically in other SDK languages using the syntax shown in the Quick Start and define_tool() tables above.