Building with LLMs · Chapter 13 of 24

Extraction

By the end of this chapter you can turn a messy WhatsApp message into a typed Python 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
  • Python
  • In the all labs plan

The first 2 chapters of Building with LLMs 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 Pydantic model. 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.py: a message becomes a typed record

    Lata types every booking request into a screen by hand. In booking_record.py, write a Pydantic model BookingRequest with six fields: from_city (text), to_city (text), travel_date (a datetime.date), seats (a whole number), name (text) and phone (text). Every field may be missing, so each must allow None.

    Then write extract(text) that 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 BookingRequest.

    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 None.

  2. Task 2

    today.py: what day is kal?

    A customer writes "kal" and the record says 2024. In today.py, write extract(text, today), the same extraction as task 1 (you can import BookingRequest from booking_record.py), but the request also tells the model today's date, taken from the today argument, a datetime.date.

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

  3. Task 3

    follow_up.py: ask, do not guess

    In follow_up.py, write follow_up(request), which takes a booking record (with from_city, to_city, travel_date, seats, name and phone; missing ones are None) and returns, checking in this order:

    1. if any of from_city, to_city, travel_date or seats is None: "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 from_city to to_city: 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 None. Never ask for the name or the phone.

  4. Task 4 · interview question

    Interview: field_scores.py, does the extractor work?

    Lata wants proof before she trusts the screen. In field_scores.py, 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 None).

    Print one line per field, in the order from_city, to_city, travel_date, 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 travel_date 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 Pydantic model as the schema.
  • Nulls, not guesses: why every field the customer may leave out must be allowed to be None.
  • 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. 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 are free. This one comes with the all labs plan.