Examples

Sort an email, then ask what that kind needs

Open in playground →

One answer picks which question comes next. The email is classed as an invoice, a meeting request or a job application, and only the follow-up for that kind runs: an amount, a date or a role.

  • when
  • Enum
  • Typed numbers
  • Text

Challenges

  • kind is an enum of three. Each follow-up has a when on one of them, so exactly one runs: amount_due (a number) for an invoice, meeting_date (a string) for a meeting request, role (an enum) for a job application.
  • This email is an invoice, so amount_due comes back as the number 1240, and meeting_date and role are skipped: no requests are sent for them, and skipped lists them.
  • The tests run in code on the typed kind. The fields a when names are dependencies, so none of the follow-ups lists depends_on.
import os
from typellm import TypeLLMClient

client = TypeLLMClient(api_key=os.environ["TYPELLM_API_KEY"])
response = client.generate(
    context="Subject: Invoice INV-2291\n\nHi, please find attached invoice INV-2291 from Acme Hosting for September. Amount due: $1,240.00 by October 15. Thanks, Acme Billing",
    questions={
      "kind": {
        "instructions": "What kind of email is this?",
        "type": "string",
        "enum": [
          "invoice",
          "meeting_request",
          "job_application"
        ]
      },
      "amount_due": {
        "instructions": "Amount due in dollars.",
        "type": "number",
        "when": {
          "kind": "invoice"
        }
      },
      "meeting_date": {
        "instructions": "Proposed meeting date, as YYYY-MM-DD.",
        "type": "string",
        "when": {
          "kind": "meeting_request"
        }
      },
      "role": {
        "instructions": "Which role is the candidate applying for?",
        "type": "string",
        "enum": [
          "engineering",
          "design",
          "sales"
        ],
        "when": {
          "kind": "job_application"
        }
      }
    },
)
print(response)

Time 0.42 s · Cost $0.0000097

Generation(
    result={
        'kind': 'invoice',
        'amount_due': 1240,
    },
    thinking={},
    usage=Usage(input_tokens=195, thinking_tokens=0),
    skipped=[
        'meeting_date',
        'role',
    ],
)