Examples

Discover a new category

Open in playground →

Sort an expense into your categories, with one more option: new. Only when none fits does a second field name the new category, in the same call.

  • Enum
  • when
  • Text

Challenges

  • category lists your categories plus new. new_category runs only when the answer is new: "when": {"category": "new"}, and it sees that answer, so it names what the expense actually is.
  • Bar association dues are not travel, meals, software or office supplies, so this one comes back new, named professional_fees. A flight, "Flight from Boston to Chicago for the client meeting, $312.", came back travel, and new_category was skipped.
import os
from typellm import TypeLLMClient

client = TypeLLMClient(api_key=os.environ["TYPELLM_API_KEY"])
response = client.generate(
    context="Expense: Annual dues for the state bar association, $450, paid by Dana Lee.",
    questions={
      "category": {
        "type": "string",
        "enum": [
          "travel",
          "meals",
          "software",
          "office_supplies",
          "new"
        ],
        "instructions": "Which category does this expense belong to? Choose new if none of them fits.",
        "return_probabilities": True
      },
      "new_category": {
        "type": "string",
        "instructions": "A short snake_case name for the new category.",
        "when": {
          "category": "new"
        }
      }
    },
)
print(response)

Time 0.42 s · Cost $0.0000056

Generation(
    result={
        'category': {
            'value': 'new',
            'probabilities': {
                'travel': 0.001,
                'meals': 0.001,
                'software': 0.001,
                'office_supplies': 0.001,
                'new': 0.996,
            },
            'confidence': 0.995,
        },
        'new_category': 'professional_fees',
    },
    thinking={},
    usage=Usage(input_tokens=111, thinking_tokens=0),
)