Examples

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.

  • when
  • Typed numbers
  • String enum

Challenges

  • A manager approves refunds of $500 or more: "when": {"amount": {"gte": 500}}. The test runs in code on the typed amount, so this $38.50 request skips manager_approval without asking the model.
  • A prepaid return label only makes sense for damaged or wrong items: "when": {"reason": ["damaged", "wrong_item"]}. The jug arrived cracked, so return_label runs.
  • A field a when skips sends no request and has no key in result; skipped lists it. The fields a when names are dependencies, so depends_on need 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)

Time 0.92 s · Cost $0.000011

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',
    ],
)