find and pick pair here is one you can point at your own documents, and three
worked cases show it in use: the address a sender wants their receipt sent to, a phone
number as +14155550177, and an invoice total as 1315.50 USD flagged as a charge.
TypeSafe picks one of the options you hand it, so the candidates have to be found
first. A regex finds them, TypeSafe picks one, and code copies the pick, in three steps:
- A regex finds the candidate values in the text. Tune it to over-find.
- TypeSafe picks which candidate the question is asking for, and reads off any attribute the code needs downstream (currency, country, whether an amount is a credit or a charge).
- The code copies the picked value and normalizes it.

Setup
TYPESAFE_API_KEY.
Helpers
find runs a regex tuned to over-find and dedupes the matches. pick is a
Choice question whose options are the spans find returns, so its answer is one of
those spans copied exactly, or none when no candidate fits. classify is a
Choice question over a fixed set of labels, used here for the currency and the
country.
is_true is a Noul, used here to ask whether an amount is a credit.
Every call is cached to json_cache.json, so re-rendering makes no API calls.
Email: pick the right address by role
Four addresses in the headers. The body asks for the receipt to go to a personal address instead of theTo: billing alias, so the answer depends on reading the body.
Two questions here: which address gets the receipt, and which one sent the message.
receipt is the personal Gmail address on the Reply-To: line, which is what the body
asks for; sender is the one on the From line. Both are copies of regex matches,
lowercased in code.
Phone: pick the mobile, normalize to E.164
Three numbers, none of them carrying a country code. TypeSafe picks the mobile and reads the country from the text;phonenumbers combines those two answers into E.164,
the international format that starts with a + and the country code.
phonenumbers formats the picked
number as +14155550177.
Money: pick the amount, classify the currency, flag credit vs charge
An invoice with four amounts on it. TypeSafe picks the total due and the credit, reads the currency, and flags each picked amount as a charge or a credit. The code copies each picked string and parses it into aDecimal.
Noul answers 0.01 on the total and 0.99 on the credit, so the code knows the
sign of each Decimal it parses.
to_decimalassumes the comma groups thousands and the dot is the decimal point. That holds for$1,315.50; in€1.315,50it is the other way round. Ask aNoulquestion which convention the document uses, and branch on it in code.
Open it in the TypeSafe playground
A share link that opens the email thread in the browser, with the receipt question on it and the four addresses the regex found among its options.Two limits
- A
Choicequestion allows at most 255 options. With more candidates than that, narrow in two stages: pick the section first, then the span inside it. - Finding the candidates is the part that takes work. Emails, phone numbers and amounts have regexes that cover them; a name does not, so its candidates have to come from a roster you already have, or from a named-entity recognizer or an LLM that proposes them. TypeSafe then picks the one the question asks for.

