Skip to main content
Read a date’s parts off the text with TypeSafe, then resolve them to a date in code. In this cookbook you will build extract_date(document, role). Give it a document and a phrase naming the date you want - “the deadline to return the form” - and it hands back a date with a confidence. It flags a low-confidence read, and one whose parts do not add up to a date at all, including a date the document never states. The date can be spelled out (“August 14, 2027”) or written relative to today (“tomorrow”, “next Thursday”). TypeSafe answers Choices about the date in one call: what kind of date it is, and which month, day, year, or weekday the text names. Code turns those answers into a date. The model reads what the text says and never does the calendar math. The cells below run that function over four short documents, print each date with its confidence, and split the results into the ones code accepts and the ones a person should look at. Overview diagram TypeSafe reads how the date is written and which parts the text names. Code turns those answers into a date - counting from today when the date is relative - and either accepts it or sends it to review.

Setup

then set TYPESAFE_API_KEY.

The questions

Seven Choices go out in one call. mode says how the date is written: absolute for a date that names a month, relative for one written relative to today, and none when the document does not state the date at all. The other six read the pieces. An absolute date needs month, day, and year. A relative one needs day_anchor: today, tomorrow, the day after, or a named weekday. When it names a weekday, weekday and week_offset say which one and which week. Code reads only the pieces mode calls for. year lists one option per year from 1900 to 2050, plus two escapes. none means the text states no year and code fills one in. out_of_range means the text states a year outside the list, and code flags that instead of guessing. If a list that long bothers you, pull the year-like numbers out of the text first and offer the model only those.

Resolve it in code

read_parts makes the call. assemble turns the answers into a date: it fills in the year when the text states none, and it works out which day a named weekday points at. Both of those count from TODAY, which is pinned so relative dates come out the same on every run. assemble also reports the lowest confidence among the parts it used, so a weak answer on any one part can send the whole date to review. “next Thursday” can mean two different days, so code decides which. A weekday with no qualifier means the next one on or after today. next means the following calendar week, and current means this week.

Run it

Six questions across four short documents: two dates from a contract that states its years, a form deadline written without a year, a survey that closes “today”, a review set for “next Thursday”, and a date the form never mentions. All of them resolve against TODAY = 2026-07-30, a Thursday.
The contract states both of its years, so those came off the text. The form states no year, so code filled in 2026: it takes the current year and moves to the next one only when the date is already more than a month past. “today” and “next Thursday” went through the same function as the spelled-out dates. The kickoff call is the one the form never mentions. There is a date in that form, just not this one, and the note absolute date incomplete means mode came back absolute with no month to go with it. The date came back empty, the confidence reads 0.46, and the row is flagged for a person.

Confidence to route on

Every answer comes back with a calibrated confidence, and a date’s confidence is the lowest one among the parts that went into it. A date under REVIEW_BELOW = 0.60 goes to a person, and so does a date code could not assemble at all. The rest go straight through.

Open it in the TypeSafe playground

The link below carries the “next Thursday” message and the same questions the code sends. Open it to see the answers and their confidences, and to change the wording without writing any code.
Open this document + questions in the TypeSafe playground →