Welcome to Structured Output From Language Models! A language model returns text, but your code needs data. In this course, you’ll turn a model’s answers into data and check every answer before your code relies on it. Anything your code can’t check goes to a person instead of a guess.
What You’ll Build
The course follows a neighborhood vet clinic whose online booking form has one free-text box, so owners write whatever they like. There’s a beagle that’s limping, a corn snake the clinic doesn’t treat, a request for a visit yesterday, and a note that says only “help”.
You’ll build a script that turns each of the twelve notes in data/notes.json into an appointment record. Each record that passes its checks is ready for the clinic to book. The script sends the notes it flags, and any that aren’t bookings, to a person at the front desk.
Here’s a first taste from one real run of that script. Yours will differ in wording and cost. One owner’s note goes in, wrapped here for display:
Hi, our beagle Biscuit has been limping on his back left leg since
Saturday. He still eats and plays but won't put weight on it after walks.
Any morning next week is fine, Tuesday would be best. Thanks, Dana
And an appointment record comes out, with the species, the problem, and the day. The long problem value is wrapped for display:
{
"kind": "booking",
"species": "dog",
"petName": "Biscuit",
"problem": "Limping on his left hind leg since Saturday and won't
bear weight on it after walks. Still eating and playing.",
"urgency": "soon",
"preferredDate": "2026-12-08",
"preferredTime": "morning"
}
Across all twelve notes, nine were ready to book, and three went to a person. The whole run cost about three cents:
12 notes, capable: 3.062 cents
to book: 9
front desk: 3
#6 the clinic doesn't treat corn snake
#7 date 2026-11-30 is before today
#8 not a booking
saved intake-2026-12-01.json
The course uses two model roles, a cheaper model and a costlier one, and capable in the first line is the costlier.
The checks prove that a record has the right shape and follows the clinic’s rules, not that it says what the owner meant. The date above, for example, is the model’s reading of “Tuesday would be best”. Along the way, you’ll see what each check can catch and what it can’t.
What You’ll Learn
By the end of this course, you’ll be able to write a prompt that turns messy text into data, get the answer as JSON that matches a schema, check it with Zod, and retry or fall back when it’s wrong. It all runs through askForJson() in the model module, for OpenAI or Anthropic.
Step by step, you’ll:
- Write a prompt that turns a messy note into data, and test it on every note
- Add
askForJson()to your model module, so each request asks the provider for JSON that matches a schema - Check each record with Zod, a library for checking data, then retry once with the error or hand off to a person
- Put it all together in a project that builds the clinic’s whole intake
Before You Start
This is the second of five courses on building AI features in JavaScript. It builds on a small model/ module whose ask() function sends a request to your provider and returns the reply with its token usage. If you’re starting here, you don’t need to have built it: the course’s code download has the starting project with model/ ready.
The course assumes you’ve called a model from your code before, and that you’ve read the token usage and cost a call reports. If that’s new to you, then read through ask() in the starting project’s model/ folder before the first lesson, because the course’s code builds on it.
You should already know how to use fetch(), async and await, errors and retries, cancellation with AbortController, JSON, and Node scripts with npm.
If you’d like a refresher on ES modules and reading JSON, the Modules and JSON course covers both.
You’ll need:
- Node 26, a terminal, and any editor. The course’s runs used Node v26.14.0.
- A key for one model provider, OpenAI or Anthropic, for the lessons and the project that call a real model. The starting project’s
.env.exampleshows where the key goes, and Calling a Language Model From JavaScript walks through getting one and setting a spending limit.
The real calls are cheap. One note costs a fraction of a cent, and the run of all twelve notes above cost about three cents. A low monthly spending limit with your provider, such as five dollars, leaves plenty of room to rerun things.
How the Course Works
- Text lessons carry most of the course. Where code differs between providers, a switch shows both versions, so open the one for your provider.
- Four video lessons show the intake in action.
- Exercises run in your browser, on a stand-in model, so they need no key.
- A project puts the intake together, a mentor scenario has you explain your code and change it, and a quiz checks what you’ve learned.
- Downloads include the starting project, the finished code, and the course slides.
The next lesson, Separate Instructions From Data, starts with the prompt that reads one owner’s note.