Structured outputs
Structured outputs turn free-form chat into data your code can trust — invoices, forms, and tickets parsed as JSON every time.
In this guide
- Design a schema before you write the prompt
- Request
json_objectmode for strict JSON replies - Validate responses and repair them automatically
- Extract Nepali invoice fields end to end
Start with the schema, not the prompt
Decide the exact keys, types, and required fields first. Small, flat schemas with explicit formats (YYYY-MM-DD, NPR numbers) fail far less often than clever nested ones.
For the invoice case below, the contract is fixed: merchant as string, date as ISO date, total_npr as number, and line_items as an array of name plus price objects.
Paste this schema into your prompt description so the model sees field names verbatim. Keep key names snake_case English even when values are Nepali — it simplifies downstream code.
Ask for JSON object mode
Set response_format to json_object so the model returns parseable JSON instead of chatty prose. State the schema in the system message and give one short example of the shape you expect.
Expected shape for the call above:
See request and response fields in the Chat Completions reference.
Validate, then repair
Never trust raw model JSON. Parse it, validate types and totals, and on failure send the error back for a one-shot repair call. This validate-then-repair loop fixes most malformed replies without human review.
Limit repairs to one retry, then route to a human queue. Log both attempts so you can tighten the prompt or schema.
Extract Nepali invoice text
Real receipts mix Nepali and English: "भाटभटेनी, मिति २०८२-१२-०२, दाल रु ३५०". Normalize first — ask the model to transliterate merchant names, convert Bikram Sambat dates to ISO when possible, and strip currency symbols into plain total_npr numbers.
Use sage-2-4-actus for messy handwriting-OCR text and sage-2-5-celer for clean, high-volume POS lines. When sourcing text from scans, pair this guide with Process documents.
Best practices
- Keep
temperatureat0.2or below for extraction tasks. - Demand JSON-only replies — no markdown fences or commentary.
- Validate enums, dates, and NPR arithmetic in code, not in prose.
- Version your schemas (
invoice_v1) so prompts and parsers evolve together. - Sample and review 5% of outputs weekly to catch drift.
