to_dict

View as MarkdownOpen in Claude

Serialize to the JSON structure expected by SWAIG. Called automatically when the result is returned from a tool function — you rarely need to call this directly.

The output contains response (if set), action (if any actions were added), and post_process (only when True and actions are present). If neither response nor action is present, defaults to {"response": "Action completed."}.

Parameters

None.

Returns

dict[str, Any] — SWAIG-formatted response dictionary.

Example

1from signalwire import AgentBase
2from signalwire import FunctionResult
3
4agent = AgentBase(name="my-agent", route="/agent")
5agent.set_prompt_text("You are a helpful assistant.")
6
7@agent.tool(name="transfer", description="Transfer the call")
8def transfer(args, raw_data):
9 result = (
10 FunctionResult("Transferring you now.")
11 .connect("+15551234567")
12 )
13
14 # Inspect the serialized output
15 print(result.to_dict())
16 # {"response": "Transferring you now.", "action": [{"SWML": {...}, "transfer": "true"}]}
17 return result
18
19agent.serve()