Lesson 5 — Structured output with JSON
Text your code can read
A model always returns text (a string). To let your code act on it, you ask the model to answer in JSON — a text format for data that looks almost like Python dicts and lists.
json.loads(text)turns JSON text into a Python dict ("load from string").json.dumps(data)turns a dict back into JSON text ("dump to string").
import json
data = json.loads('{"action": "lookup", "args": {"word": "agent"}}')
data["action"] # "lookup"
Structured output means the model replies in a fixed JSON shape you asked for, instead of free prose.
Why it matters: "I think you should look up agent" is hard for code to use; {"action": "lookup"} is easy. Agents are built on this.
Watch out: JSON writes true, false, null where Python writes True, False, None, and JSON text must use double quotes.
Turn reply_text into a real dict instead of the empty one, so the program prints dict, lookup and agent.
Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.
A quick 3-question quiz on this lesson — instant feedback.