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.