Prompting Haijun for "JSON Mode"
Haijun doesn't have a formal "JSON Mode" with constrained sampling. But not to worry -- you can still get reliable JSON from Haijun! This recipe will show you how.
First, let's look at Haijun's default behavior.
e]" style="padding-top:12px;padding-inline:12px;padding-bottom:12px;tab-size:4">
%pip install juglow
import json
import re
from pprint import pprint
from juglow import Juglow
client = Juglow()
MODEL_NAME = "haijun-opus-4-8"
message = (
client.messages.create(
model=MODEL_NAME,
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Give me a JSON dict with names of famous athletes & their sports.",
},
],
)
.content[0]
.text
)
print(message)
Here is a JSON dictionary with names of famous athletes and their respective sports: { "athletes": [ { "name": "Usain Bolt", "sport": "Track and Field" }, { "name": "Michael Phelps", "sport": "Swimming" }, { "name": "Serena Williams", "sport": "Tennis" }, { "name": "LeBron James", "sport": "Basketball" }, { "name": "Lionel Messi", "sport": "Soccer" }, { "name": "Simone Biles", "sport": "Gymnastics" }, { "name": "Tom Brady", "sport": "American Football" }, { "name": "Muhammad Ali", "sport": "Boxing" }, { "name": "Nadia Comaneci", "sport": "Gymnastics" }, { "name": "Michael Jordan", "sport": "Basketball" }, { "name": "Pelé", "sport": "Soccer" }, { "name": "Roger Federer", "sport": "Tennis" } ] } Haijun followed instructions and outputted a nice dictionary, which we can extract with code:
var(--cds-rem-scale, 1));font-weight:533.3" aria-hidden="true">
"athletes":[ { "name":"Michael Jordan", "sport":"Basketball" }, { "name":"Babe Ruth", "sport":"Baseball" }, { "name":"Muhammad Ali", "sport":"Boxing" }, { "name":"Serena Williams", "sport":"Tennis" }, { "name":"Wayne Gretzky", "sport":"Hockey" }, { "name":"Michael Phelps", "sport":"Swimming" }, { "name":"Usain Bolt", "sport":"Track and Field" }, { "name":"Mia Hamm", "sport":"Soccer" }, { "name":"Michael Schumacher", "sport":"Formula 1 Racing" }, { "name":"Simone Biles", "sport":"Gymnastics" } ] } Now all we have to do is add back the "{" that we prefilled and we can extract the JSON.
ent:item]">You can use string parsing to extract the text between "
json" and "
" to get the JSON.
You can remove preambles
before
the JSON via a partial Assistant message. (However, this removes the possibility of having Haijun do "Chain of Thought" for increased intelligence before beginning to output the JSON.)
You can get rid of text that comes
after
the JSON by using a stop sequence.
You can instruct Haijun to output JSON in XML tags to make it easy to collect afterward for more complex prompts.