Building with LLMs (TypeScript) · Chapter 13 of 24

Extraction

By the end of this chapter you can turn a messy WhatsApp message into a typed TypeScript record, keep the model from guessing what the customer never said, give it the one fact it cannot know (today's date), and measure how often the record is right. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Reliable output
  • 4 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of Building with LLMs (TypeScript) are free. This one comes with the all labs plan.

Why you need this

Half of SafarLine's WhatsApp queue is people who want seats: "Mumbai se Goa jana hai 3 Nov, 3 log hai", "Book 2 tickets Kolhapur to Pune for 22/10 please". Lata reads each one, works out the route, the date and the number of seats, and types them into the booking screen. Zoya wants the model to fill that screen, so Lata only checks it.

The booking screen needs fields, not prose: a from city, a to city, a date, a number. Chapter 12 showed you how to get JSON in the shape of a zod schema. That makes the reply parse. It does not make it right: the model can fill a field the customer never mentioned, or turn "kal" into the wrong day, and the JSON still parses perfectly.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    booking-record.ts: a message becomes a typed record

    Lata types every booking request into a screen by hand. In booking-record.ts, export a zod schema BookingRequest with six fields: fromCity (text), toCity (text), travelDate (an ISO date, z.iso.date()), seats (a whole number), name (text) and phone (text). Every field may be missing, so each must be nullable. Export its type too: type BookingRequest = z.infer<typeof BookingRequest>.

    Then export extract(text: string): Promise<BookingRequest>, which sends lab-chat a system message asking it to extract the booking request, the customer's text as the user message, and BookingRequest as the structured output schema, and returns the parsed record.

    The check calls extract on a message with a route, a date, seats and a name, and on one that gives only two cities: everything the message does not say must come back as null.

  2. Task 2

    today.ts: what day is kal?

    A customer writes "kal" and the record says 2024. In today.ts, export extract(text: string, today: string): Promise<BookingRequest>, the same extraction as task 1 (import the schema from ./booking-record.ts), but the request also tells the model today's date, taken from the today argument, a day written as YYYY-MM-DD.

    The check calls it with messages that say kal, parso and "5 Nov", and with todays it chooses, such as 2026-12-30: the travelDate must be the right day after the today it was given.

  3. Task 3

    follow-up.ts: ask, do not guess

    In follow-up.ts, export followUp(request: BookingRequest): string | null, which takes a booking record (fromCity, toCity, travelDate, seats, name and phone; missing ones are null) and returns, checking in this order:

    1. if any of fromCity, toCity, travelDate or seats is null: "Please tell us: " followed by the missing ones in that order, named from city, to city, travel date and seats, separated by a comma and a space, and a full stop, for example: Please tell us: travel date, seats.

    2. if routes.json in /workspace/data has no route from fromCity to toCity: Sorry, SafarLine has no bus from Goa to Nashik. (with the record's cities)

    3. if seats is more than 6: A booking can hold at most 6 seats. For 7 or more, write to groups@safarline.example.

    4. otherwise null. Never ask for the name or the phone.

  4. Task 4 · interview question

    Interview: field-scores.ts, does the extractor work?

    Lata wants proof before she trusts the screen. In field-scores.ts, extract each of the 60 booking requests in /workspace/data/messages.jsonl (the lines whose intent is booking_request) with your extractor from task 2, telling it that today is 2026-10-20, the day Lata exported them.

    Compare each record with the message's true fields (under "fields": from, to, date, seats, name, phone; a key is left out when the message does not say it, and then the record is right if it has null).

    Print one line per field, in the order fromCity, toCity, travelDate, seats, name, phone: the field, a colon, a space and right/total, for example seats: 41/60 (a made-up number).

    Be ready to explain what travelDate would score without today's date, and why only that field changes.

What you will learn

  • Extraction: one job, text in and fields out, with a zod schema as the form.
  • Nulls, not guesses: why every field the customer may leave out must be .nullable(), and what the compiler then makes you do.
  • Today's date: why "kal" and "5 Nov" need it, and where it goes.
  • What comes after: code that asks for what is missing, and a measurement against labelled messages.

More in Reliable output

Build "Extraction" yourself

SafarLine, an intercity bus company, drowns in WhatsApp messages. You build its first AI assistant, in TypeScript. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Building with LLMs (TypeScript) are free. This one comes with the all labs plan.