Skip to content

Asking for JSON

Read · 1 of 3

Make the model fill in a form

An agent's code has to act on what the model says. "I think this ticket is fairly urgent" is hard to act on; {"urgent": true} is easy.

So you ask for JSON with exact field names, show an example, and parse it with json.loads. Now the model's answer is data your program can branch on.

Read this lesson as text

Text is for people. Programs need fields.

Make the model fill in a form

An agent's code has to act on what the model says. "I think this ticket is fairly urgent" is hard to act on; {"urgent": true} is easy.

So you ask for JSON with exact field names, show an example, and parse it with json.loads. Now the model's answer is data your program can branch on.

From text to fields

import json

reply = '{"label": "bug", "priority": 2, "tags": ["login", "mobile"]}'
data = json.loads(reply)
print(data["label"], data["priority"] + 1, len(data["tags"]))

What does this print?

  • bug 3 2
  • bug 21 2
  • label 3 2
Show the answer

bug 3 2 json.loads turns the text into a dict. priority is a real number now (2, plus 1 is 3), and tags is a list of two strings.

Exercise: Read the triage

A model triaged a support ticket and replied with one line of JSON: title (text), tags (a list of text) and urgent (true or false).

Print title | tag1, tag2 | URGENT (or normal when it isn't urgent). With no tags, print - in the middle.