Python helpers

hand_off_conversation

Hand the conversation to a human agent from your own action logic, or transfer a live phone call.

hand_off_conversation(context: dict, reason: str = None, phone_destination: str = None) -> dict

Hands the conversation to a human agent. The bot stops responding and a human takes over. It can only be called from bot actions. Anywhere else it raises RuntimeError.

Call this from inside a custom Python action when you want to hand a conversation to a human based on your own logic, for example after an API call reveals a complex case. This is the programmatic equivalent of the built-in transferConversation action. Use it when the handoff decision depends on logic inside your action rather than the workflow instructions.

For the wider picture, see Human escalation. It covers what a handoff triggers, how conversations are routed to teams, and common custom escalation patterns.


Parameters

NameTypeDescription
contextdictThe context object passed to your action
reasonstr or NoneOptional reason for the handoff (max 500 characters)
phone_destinationstr or NoneOptional phone number to forward a live phone call to (E.164 format, e.g. +15551234567). Silently ignored outside the phone channel.

Returns

dict: {"success": bool, "message": str}

Example

def execute_action(context):
    order = fetch_order(context["args"]["orderId"])

    if order.get("hasFraudFlag"):
        hand_off_conversation(context, "Order flagged for fraud review")
        return {"handedOff": True, "reason": "fraud_flag"}

    return order

On this page