JSON output
Get a reply you can parse as JSON.
Ask for JSON in the prompt and pass "response_format": {"type": "json_object"}. Some models wrap the JSON in a Markdown code fence, so remove the fence markers before you parse it, as below.
Python:
import json, re
r = client.chat.completions.create(
model="ghost-x2",
response_format={"type": "json_object"},
messages=[{"role": "user", "content": "Give me JSON with keys a and b, both numbers."}],
)
text = re.sub(r"^```(?:json)?\s*|\s*```quot;, "", r.choices[0].message.content.strip())
data = json.loads(text)