Building with LLMs · Chapter 11 of 24
JSON output
By the end of this chapter you can ask a model for JSON, turn its reply into Python data, cope with the values it leaves empty, and read JSON that arrives wrapped in a code fence with a stray comma. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Reliable output
- 4 tasks
- Python
- In the all labs plan
The first 2 chapters of Building with LLMs are free. This one comes with the all labs plan.
Why you need this
Lata's labels from chapter 9 were one word each: easy to use. Now she wants more from every booking message: where from, where to, how many seats, the customer's name. A sentence like "Krish wants two seats from Bengaluru to Hyderabad" is pleasant to read and useless to a program, which needs seats as the number 2, not somewhere inside a sentence.
JSON is the answer: text that Python turns into a dictionary in one call. Models write JSON well, because they have read a lot of it. They also write it badly in small, maddening ways, and your code has to know the difference.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
json_mode.py: JSON mode
Ask lab-chat, with JSON mode on (a response format of type json_object), to turn Diya's message (in the starter) into JSON: the system message says to extract the booking and reply in JSON with the keys from, to, seats, name and phone; the message is the user message.
Turn the reply into a dictionary and print five lines, one per key in that order: the key, a colon, a space and its value, for example:
from: Pune
- Task 2
parse_many.py: many replies, some empty
For each of the first 12 messages in /workspace/data/messages.jsonl, ask lab-chat in JSON mode for the keys from, to and seats (your instruction in the system message, the customer's text as the user message).
Print one line per message: its id, the from city, an arrow, the to city, a comma and the seat count, for example M200 Pune -> Goa, 2 seat(s). Where a value is null, print a question mark instead: M201 ? -> ?, ? seat(s)
- Task 3
sloppy.py: when the JSON is sloppy
Write a function clean_json(text) that returns the dictionary in a reply wrapped in a Markdown code fence (a first line of three backticks, maybe followed by json, and a last line of three backticks) with a trailing comma before a closing brace or bracket. Plain valid JSON must still work, and text that is not JSON must still raise an error.
When the file is run, ask lab-chat-sloppy in JSON mode for the keys from, to and seats of Diya's message, and print from clean_json of the reply one line such as Pune -> Goa, 2 seats
Put the call to the model under if __name__ == "__main__":
- Task 4 · interview question
Interview: fix_json.py, read what JSON mode sends back
fix_json.py should print, for its three messages, the from city, an arrow, the to city, a colon and the seat count, for example Pune -> Goa: 2 seat(s). A message that does not say how many seats is 1 seat.
It has three mistakes. Run it, read each error, fix the line it points at, and run it again until it prints three lines. Keep the messages and JSON mode.
What you will learn
- JSON mode: the setting that makes a reply valid JSON.
- Parsing a reply with json.loads, and what null becomes.
- What JSON mode does not promise: your keys, your types.
- What breaks: code fences and trailing commas, and how to read them.
More in Reliable output
Build "JSON output" yourself
SafarLine, an intercity bus company, drowns in WhatsApp messages. You build its first AI assistant. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Building with LLMs are free. This one comes with the all labs plan.