View as Markdown

JSON output

Ask for JSON when your code, not a person, reads the reply. Set response_format to {"type": "json_object"} and describe the fields you want in the prompt.

python
import json
from openai import OpenAI

client = OpenAI(base_url="https://api.continuumlabs.ai/v1", api_key="cl-your-key")

reply = client.chat.completions.create(
    model="glm-5.2",
    temperature=0,
    response_format={"type": "json_object"},
    messages=[
        {"role": "system", "content": 'Extract the invoice. Reply with JSON only: {"supplier": str, "abn": str or null, "total_inc_gst": str}'},
        {"role": "user", "content": "Ironbark Plumbing Pty Ltd, ABN 90 551 661 605. Total (inc GST) $2,856.70"},
    ],
)
invoice = json.loads(reply.choices[0].message.content)

Always check the result in code

JSON mode makes the reply parse as JSON. It does not make it correct. Check it before you use it:

  • Required fields are present and have the right type.
  • Allowed values only, for anything that should come from a fixed list.
  • Copied figures really appear in the source. Models occasionally invent or alter numbers.
  • Arithmetic is redone in code. For Australian data, the cookbook's checks library tests ABNs, GST and more.

If a check fails, send the problems back and ask for corrected JSON. One retry fixes most errors. If it still fails, flag the item for a person rather than guessing.

The bookkeeper recipe shows the whole pattern on supplier invoices.

Tips

  • Use temperature: 0 for extraction.
  • Show the exact field names and types in the system prompt.
  • Ask the model to copy numbers exactly as written, so your code can check them against the source.