define_tool
define_tool
Programmatically define a SWAIG function (tool) that the AI can invoke during a
conversation. This is the imperative alternative to the
@tool decorator.
Tool definitions map to SWML SWAIG function entries. See the SWML SWAIG functions reference for the full specification.
For most cases, the @tool decorator
is simpler and supports automatic parameter inference from type hints. Use
define_tool() when you need dynamic tool registration or when the tool definition
comes from external configuration.
Parameters
name
Function name. Must be unique within the agent. The AI uses this name to invoke the function.
description
Human-readable description of what the function does. The AI reads this to decide when to call the function.
parameters
JSON Schema describing the function’s parameters. The AI generates arguments conforming to this schema.
handler
Python function to call when the AI invokes this tool. Receives (args: dict, raw_data: dict)
and should return a FunctionResult.
secure
Whether to require token validation on tool calls. Recommended for production.
fillers
Language-specific filler phrases spoken while the function executes.
Format: {"en-US": ["Looking that up...", "One moment..."]}.
webhook_url
External URL to forward the tool call to instead of executing locally.
required
List of required parameter names from the JSON Schema.
is_typed_handler
Set to True if the handler uses type-hinted parameters instead of the standard
(args, raw_data) signature.
**swaig_fields
Additional SWAIG fields to include in the function definition
(e.g., wait_file, meta_data).
Returns
AgentBase — Returns self for method chaining.