Python helpers

llm_classify_binary

Ask an LLM a yes-or-no question about a message or conversation, and get a boolean back.

llm_classify_binary(prompt: str, input, fallback=None) -> bool

Sends a prompt and input to an LLM and returns True or False. It lets plain Python make judgment calls on conversation content: intent, sentiment, threats or specific patterns. For example, your code can check whether a customer is threatening a chargeback.


Parameters

NameTypeDescription
promptstrThe classification instruction (e.g., "Is the customer asking about returns?"). At most 2,000 characters
inputanyThe text to classify. If not a string (e.g., a list of messages or dict), it is auto-converted to JSON. At most 10,000 characters after conversion
fallbackany/NoneOptional value to return if the API call fails. If not provided, exceptions are raised

Returns

bool: True or False based on the LLM classification.

Answers are consistent: the same prompt and input normally return the same answer, so calling again or taking a majority vote does not make a result more reliable. To make a decision stricter or looser, reword the prompt.

Error handling

  • If fallback is provided, it is returned on any API error, including a request rejected for exceeding the limits above. Trim long conversations so a fallback doesn't silently replace every answer.
  • If fallback is not provided (default), exceptions propagate and your action fails.
  • Set a fallback when you want your action to continue even if classification fails.

Examples

Detect a chargeback threat

def execute_action(context):
    messages = context["conversation"]["messages"]

    is_threat = llm_classify_binary(
        "Has the customer explicitly threatened a chargeback, bank dispute, "
        "or legal action? Frustration alone does not count.",
        messages,
        fallback=False,
    )

    if is_threat:
        # Immediately process refund to avoid chargeback
        process_full_refund(context["args"]["orderId"])
        add_conversation_tag(context, "chargeback-threat")
        return {"action": "refunded", "reason": "chargeback_threat"}

    return {"action": "continue_normal_flow"}

Check if a message is about a specific topic

def execute_action(context):
    last_message = context["conversation"]["messages"][-1]["content"]

    is_return_request = llm_classify_binary(
        "Is the customer asking to return or exchange a product?",
        last_message,
    )

    return {"isReturnRequest": is_return_request}

Classify with a structured input

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

    is_suspicious = llm_classify_binary(
        "Does this order look suspicious? Check for mismatched "
        "shipping/billing addresses, unusually high quantities, "
        "or known fraud patterns.",
        order,  # dict is auto-converted to JSON
        fallback=False,
    )

    return {"suspicious": is_suspicious, "order": order}

On this page