Ask for JSON, Not a Paragraph
structured JSON extraction
Part of: Receipt Scanner
The model will happily describe a receipt in a chatty paragraph. That does you no good. You want a Python dict with merchant, date, and total that goes straight into a database. Getting there takes two things: a prompt that asks for JSON and nothing else, and parsing code that still works when the model ignores you and wraps its JSON in chatter anyway. Asking for the exact shape Spell out the keys and the format you want, right in the system prompt: Naming the keys and formats up front is the biggest lever you have. "Return the date" gets you five different date formats across five receipts. "Return the date as YYYY-MM-DD" gets you one. The model still adds chatter Even with a strict prompt, replies come back looking like "Sure, here's the data: {...} Let me know if you need more.", or with the JSON buried in a markdown code fence. A good prompt shrinks how often this happens. It won't stop it entirely. So your parser has to go looking for the JSON object inside the text instead of assuming the whole reply is JSON. Pulling the JSON out of the noise The trick is short: find the first { and the last } in the reply, slice out everything between them, and hand that slice to json.loads.
Challenge: Pull the JSON Out of the Noise