SWAIG Functions
What You’ll Learn
This chapter covers everything about SWAIG functions:
- Defining Functions - Creating functions the AI can call
- Parameters - Accepting arguments from the AI
- Results & Actions - Returning data and triggering actions
- DataMap - Serverless API integration without webhooks
- Native Functions - Built-in SignalWire functions
How SWAIG Functions Work

Quick Start Example
Here’s a complete agent with a SWAIG function:
Python
TypeScript
Function Types
Chapter Contents
When to Use SWAIG Functions
Key Concepts
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.
Basic Function Definition
define_tool() Across Languages
The define_tool() method is available in all SDK languages. The parameters JSON Schema is identical everywhere; only the calling syntax differs:
The define_tool() Method
Required Parameters:
Optional Parameters:
Handler Function Signature
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:
Accessing Call Data
Multiple Functions
Register as many functions as your agent needs:
Function Fillers
Add per-function filler phrases for when the function is executing:
The @tool Decorator
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:
define_tool() vs @tool: Choosing an Approach
Both methods register SWAIG functions with the same result. Choose based on your coding style and requirements.
Comparison
When to Use define_tool()
Conditional function registration:
Dynamic functions from configuration:
Handlers defined outside the class:
When to Use @tool Decorator
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-Inferred Functions
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:
- Parameters without defaults and not
Optionalare marked as required - The function’s docstring summary becomes the tool description
- Google-style
Args:docstring sections provide per-parameter descriptions - A
raw_dataparameter is recognized and passed through (not included in schema) - If no type hints are present, the SDK falls back to the traditional
(args, raw_data)convention
Accessing raw_data with typed functions:
When to use type inference vs explicit parameters:
Mixing Both Approaches
You can use both in the same agent:
Registering Pre-Built SWAIG Functions
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():
External Webhook Functions
Route function calls to an external webhook:
Function Security
By default, functions require token validation. Disable for testing:
Writing Good Descriptions
The description helps the AI decide when to use your function:
Testing Functions
Use swaig-test to test your functions:
Complete Example
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.