Loading video player…

Structured Output From Language Models (Overview)

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:

Language: Text
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:

Language: Program Output
{
  "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:

Language: Program Output
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.example shows 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.

Download

Course Slides (.pdf)

689.1 KB
Download

Sample Code (.zip)

37.6 KB

00:00 Welcome to Structured Output From Language Models. A model answers in text, but your code needs data. This course turns those answers into data your code can check.

00:13 Here’s a first taste of the script you’ll build for a vet clinic. An owner’s note goes in, and a record comes out. It has the species, the problem, and the day.

00:25 Of twelve notes, nine are ready to book, and three go to a person. The run cost about three cents. Your wording and cost will differ a little.

00:35 The clinic’s booking form has one free-text box, so owners write anything: a snake the clinic doesn’t treat, a visit for yesterday, or one word, help. Records that pass the checks are ready to book, and the code sends the rest to a person instead of guessing.

00:55 You’ll write a prompt that turns a messy note into data. Then you’ll add a function that asks the model for JSON matching a schema. You’ll check each record with Zod, a checking library, then retry once or hand off to a person.

01:10 The project builds the clinic’s whole intake.

01:15 This course is the second of five on building AI features. It builds on a small model module that sends requests to a model and returns the reply. If you’re starting here, the code download has it.

01:28 You should already know what’s on this list. You’ll need Node 26 and an OpenAI or Anthropic key.

01:36 Most lessons are text, with a switch that shows your provider’s code, and four are videos. Exercises run in your browser and need no key. The course closes with a project, a mentor scenario where you explain your code, and a quiz.

01:52 Next up is Separate Instructions From Data.

Become a Member to join the conversation.