Route a refund request
Open in playground →Some steps only apply to some requests. With when, a field runs only if earlier answers call for it: a manager signs off on large refunds, and a return label goes out only for damaged or wrong items.
Challenges
- A manager approves refunds of $500 or more:
"when": {"amount": {"gte": 500}}. The test runs in code on the typedamount, so this $38.50 request skipsmanager_approvalwithout asking the model. - A prepaid return label only makes sense for damaged or wrong items:
"when": {"reason": ["damaged", "wrong_item"]}. The jug arrived cracked, soreturn_labelruns. - A field a
whenskips sends no request and has no key inresult;skippedlists it. The fields awhennames are dependencies, sodepends_onneed not list them.
import os
from typellm import TypeLLMClient
client = TypeLLMClient(api_key=os.environ["TYPELLM_API_KEY"])
response = client.generate(
context="Refund request for order #4471, a $38.50 blender jug. It arrived cracked and leaks when filled. I'd like my money back. — Dana",
questions={
"amount": {
"instructions": "Order amount in dollars.",
"type": "number"
},
"reason": {
"instructions": "Why does the customer want a refund?",
"type": "string",
"enum": [
"damaged",
"late",
"changed_mind",
"wrong_item"
]
},
"manager_approval": {
"instructions": "Refunds of $500 or more need a manager. Should this one be approved?",
"type": "boolean",
"when": {
"amount": {
"gte": 500
}
}
},
"return_label": {
"instructions": "Should we email a prepaid return label?",
"type": "boolean",
"when": {
"reason": [
"damaged",
"wrong_item"
]
}
},
"reply": {
"instructions": "A one-sentence reply to the customer.",
"type": "string",
"depends_on": [
"reason"
]
}
},
)
print(response)Generation(
result={
'amount': 38.5,
'reason': 'damaged',
'return_label': True,
'reply': 'We are sorry to hear that your blender jug arrived damaged and are processing your refund for order #4471.',
},
thinking={},
usage=Usage(input_tokens=212, thinking_tokens=0),
skipped=[
'manager_approval',
],
)