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.
Challenges
categorylists your categories plusnew.new_categoryruns only when the answer isnew:"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, namedprofessional_fees. A flight, "Flight from Boston to Chicago for the client meeting, $312.", came backtravel, andnew_categorywas 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)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),
)