llm_classify_category
Classify input into one of several categories using an LLM.
llm_classify_category(prompt: str, input, options: list, fallback=None) -> strSends a prompt and input to an LLM and returns one of the provided category strings. Useful for routing decisions where the answer isn't binary.
Parameters
| Name | Type | Description |
|---|---|---|
prompt | str | The classification instruction (e.g., "What department should handle this?") |
input | any | The text to classify. If not a string (e.g., a list of messages or dict), it is auto-converted to JSON |
options | list | List of possible category strings. Must contain at least 2 options. The LLM picks exactly one |
fallback | any/None | Optional value to return if the API call fails. If not provided, exceptions are raised |
Returns
str — One of the strings from the options list.
Error handling
- If
fallbackis provided, it is returned on any API error. - If
fallbackis 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},
}