Tools · Free preview

Structured Output

Turn free text into typed fields

A model returns one blob of free text, but your code needs typed fields it can act on — so you parse that text into a structured object, coercing each value (like a year) into the right type.

Turn free text into typed fields

You asked the model for a person's details and it answered: "Name: Ada Lovelace | Born: 1815 | Field: mathematics". That's a perfectly good sentence for a human — and useless to your code. You can't write person.born + 1 or save person.field to a database, because right now you have one opaque string, not fields. Everything downstream — a tool call, a DB row, an if — needs typed values it can address by name.

That gap is what structured output closes: you take the free text the model produced and parse it into an object with real fields. And parsing isn't just slicing strings — it's type coercion too. The year arrives as the text "1815", and "1815" + 1 is "18151", not 1816. So you wrap it in Number(...) to get an actual number your math and comparisons can trust.

The shape here is regular, so the parse is mechanical. Split the reply on " | " and you get three "Key: Value" chunks. Split each chunk on ": " and you get the label and its value: ["Name", "Ada Lovelace"], ["Born", "1815"], ["Field", "mathematics"]. Drop each into an object keyed by the lowercased label, coerce born to a Number, and you've turned a sentence into { name, born, field } — addressable, typed, ready to act on.

Below, the print loop is already wired, but the fields start blank (name: "", born: NaN, parsed 0 fields). Do the parse: pull name, born (as a Number), and field out of reply, and set count to how many fields you found. Done means it prints name: Ada Lovelace, born: 1815, field: mathematics, then parsed 3 fields.

A model gives you text; your code needs fields. Parsing — with the right type coercion — is the bridge between the two.

In the full academy, you write and run this — live, graded:

// A model replied with everything you need — but as ONE line of free text.
// Your code can't do "person.born + 1"; it needs typed fields. Parse them out.
const reply = "Name: Ada Lovelace | Born: 1815 | Field: mathematics";

// TODO: parse the reply into these three fields.
//   - name  : the text after "Name: "
//   - born  : the year after "Born: ", coerced to a Number
//   - field : the text after "Field: "
// Hint: split on " | " to get chunks, then split each chunk on ": ".
let name = "";
let born = NaN;
let field = "";
let count = 0;

// Print loop is wired — you just need to fill the fields + count above.
console.log(`name: ${name}`);
console.log(`born: ${born}`);
console.log(`field: ${field}`);
console.log(`parsed ${count} fields`);

🔒 Live code execution, real agent runs, mastery tracking and verifiable credentials unlock with the full academy.

This is 1 of 50 lessons.

The full academy: write real code, watch real agents run, and earn verifiable credentials — across 8 tracks, in a 3D campus.

Unlock the full academy — $100 →

14-day refund · 🔒 Stripe-secured checkout · lifetime access

More free lessons: An LLM Is a Function  ·  The Agent Loop  ·  Define a Tool  ·  Give an Agent a Tool  ·  Durable State

← The Agent Marketplace