Python helpers

llm_classify_category

Have an LLM sort a message or conversation into one of the categories you define.

llm_classify_category(prompt: str, input, options: list, fallback=None) -> str

Sends a prompt and input to an LLM and returns one of the category strings you provide. Use it for routing decisions where the answer isn't yes or no. For example, a condition provider can sort a product complaint into categories such as a medical concern or a physical defect.


Parameters

NameTypeDescription
promptstrThe classification instruction (e.g., "What department should handle this?"). 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
optionslistList of possible category strings: 2 to 50 options, each at most 200 characters. The LLM picks exactly one
fallbackany/NoneOptional value to return if the API call fails. If not provided, exceptions are raised

Returns

str: one of the strings from the options list.

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 and keep the option list within 50 so a fallback doesn't silently replace every answer.
  • If fallback is not provided (default), exceptions propagate.
  • The returned value is always one of the strings in options.

Examples

Route a product complaint

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

    category = llm_classify_category(
        "Based on the customer's description, classify the product issue.",
        messages,
        ["medical_concern", "physical_defect", "not_working", "general_dissatisfaction"],
    )

    return {
        "conditions": {
            "isMedicalConcern": category == "medical_concern",
            "isPhysicalDefect": category == "physical_defect",
            "isNotWorking": category == "not_working",
        },
        "data": {"issueCategory": category},
    }

Classify message sentiment

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

    sentiment = llm_classify_category(
        "What is the sentiment of this message?",
        last_message,
        ["positive", "neutral", "negative"],
        fallback="neutral",
    )

    add_conversation_tag(context, f"sentiment:{sentiment}")
    return {"sentiment": sentiment}

Classify a shipping situation from tracking data

def evaluate_conditions(context):
    tracking_data = fetch_tracking(context["args"]["trackingNumber"])

    situation = llm_classify_category(
        "Based on the tracking checkpoints, classify the shipping situation.",
        tracking_data,
        [
            "moving_normally",
            "delayed_but_moving",
            "stalled",
            "lost",
            "customs_hold",
            "delivered",
            "returned_to_sender",
        ],
        fallback="stalled",
    )

    return {
        "conditions": {
            "isMoving": situation in ("moving_normally", "delayed_but_moving"),
            "isStalled": situation == "stalled",
            "isLost": situation == "lost",
            "isDelivered": situation == "delivered",
            "isReturned": situation == "returned_to_sender",
        },
        "data": {"situation": situation, "tracking": tracking_data},
    }

On this page