cookbook · Extractor · Accounts payable
Read an invoice into your AP system
Supplier invoices arrive as PDFs and emails in every layout there is, and someone retypes them into the accounts system. This recipe sends the invoice text through the OpenAI-compatible chat endpoint with a JSON schema, so you can keep the OpenAI SDK you already use, and gets back one object: vendor, dates as dates, every line item with numbers as numbers, the totals, and the purchase order number, which this invoice does not quote, so it comes back null instead of invented.
Request
POST /v1/chat/completions, documented in the Extractor reference.
curl https://meragpt.com/v1/chat/completions \
-H "Authorization: Bearer $MERAGPT_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "extractor-1",
"messages": [
{
"role": "user",
"content": "INVOICE\nNorthwind Office Supply Ltd.\n14 Canal Street, Manchester M1 3HE\nVAT reg. GB 284 1937 06\n\nInvoice no: NW-2026-04417\nInvoice date: 12 November 2026\nPayment terms: 30 days net\nDue date: 12 December 2026\n\nBill to: Halden Architects, 3 Rope Walk, Bristol BS1 6ZJ\n\nItem Qty Unit price Amount\nA4 copier paper, 80gsm (box of 5) 6 £24.50 £147.00\nToner cartridge HP 58A 2 £89.99 £179.98\nDesk organiser, bamboo 4 £18.25 £73.00\n\nSubtotal £399.98\nVAT 20% £80.00\nTotal due £479.98\n\nPlease pay by bank transfer to Northwind Office Supply Ltd, sort code 20-45-77, quoting the invoice number."
}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "invoice",
"schema": {
"type": "object",
"properties": {
"vendor": {
"type": "string"
},
"invoice_number": {
"type": "string"
},
"invoice_date": {
"type": "string",
"format": "date"
},
"due_date": {
"type": "string",
"format": "date",
"description": "When payment is due."
},
"currency": {
"type": "string",
"enum": [
"GBP",
"EUR",
"USD"
]
},
"line_items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"unit_price": {
"type": "number"
},
"amount": {
"type": "number"
}
}
}
},
"subtotal": {
"type": "number"
},
"tax": {
"type": "number"
},
"total": {
"type": "number"
},
"purchase_order_number": {
"type": "string",
"description": "The buyer'\''s PO number, if the invoice quotes one."
}
},
"required": [
"vendor",
"invoice_number",
"total"
]
}
}
}
}'What came back
The real output from running this recipe, unedited.
{
"vendor": "Northwind Office Supply Ltd.",
"invoice_number": "NW-2026-04417",
"invoice_date": "2026-11-12",
"due_date": "2026-12-12",
"currency": "GBP",
"line_items": [
{
"description": "A4 copier paper, 80gsm (box of 5)",
"quantity": 6,
"unit_price": 24.5,
"amount": 147
},
{
"description": "Toner cartridge HP 58A",
"quantity": 2,
"unit_price": 89.99,
"amount": 179.98
},
{
"description": "Desk organiser, bamboo",
"quantity": 4,
"unit_price": 18.25,
"amount": 73
}
],
"subtotal": 399.98,
"tax": 80,
"total": 479.98,
"purchase_order_number": null
}Python
import json, os
from openai import OpenAI
client = OpenAI(base_url="https://meragpt.com/v1", api_key=os.environ["MERAGPT_API_KEY"])
invoice_text = open("invoice.txt").read()
schema = {
"type": "object",
"properties": {
"vendor": {
"type": "string"
},
"invoice_number": {
"type": "string"
},
"invoice_date": {
"type": "string",
"format": "date"
},
"due_date": {
"type": "string",
"format": "date",
"description": "When payment is due."
},
"currency": {
"type": "string",
"enum": [
"GBP",
"EUR",
"USD"
]
},
"line_items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"unit_price": {
"type": "number"
},
"amount": {
"type": "number"
}
}
}
},
"subtotal": {
"type": "number"
},
"tax": {
"type": "number"
},
"total": {
"type": "number"
},
"purchase_order_number": {
"type": "string",
"description": "The buyer's PO number, if the invoice quotes one."
}
},
"required": [
"vendor",
"invoice_number",
"total"
]
}
r = client.chat.completions.create(
model="extractor-1",
messages=[{"role": "user", "content": invoice_text}],
response_format={"type": "json_schema", "json_schema": {"name": "invoice", "schema": schema}},
)
inv = json.loads(r.choices[0].message.content)
# the totals must add up before anything is posted
lines = round(sum(i["amount"] for i in inv["line_items"]), 2)
assert lines == inv["subtotal"], (lines, inv["subtotal"])
assert round(inv["subtotal"] + inv["tax"], 2) == inv["total"]
if inv["purchase_order_number"] is None:
print("no PO quoted: route for approval")
print(inv["vendor"], inv["total"], inv["currency"], "due", inv["due_date"])
TypeScript
import OpenAI from "openai";
import { readFileSync } from "node:fs";
const client = new OpenAI({ baseURL: "https://meragpt.com/v1", apiKey: process.env.MERAGPT_API_KEY });
const schema = {
"type": "object",
"properties": {
"vendor": {
"type": "string"
},
"invoice_number": {
"type": "string"
},
"invoice_date": {
"type": "string",
"format": "date"
},
"due_date": {
"type": "string",
"format": "date",
"description": "When payment is due."
},
"currency": {
"type": "string",
"enum": [
"GBP",
"EUR",
"USD"
]
},
"line_items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"unit_price": {
"type": "number"
},
"amount": {
"type": "number"
}
}
}
},
"subtotal": {
"type": "number"
},
"tax": {
"type": "number"
},
"total": {
"type": "number"
},
"purchase_order_number": {
"type": "string",
"description": "The buyer's PO number, if the invoice quotes one."
}
},
"required": [
"vendor",
"invoice_number",
"total"
]
};
const r = await client.chat.completions.create({
model: "extractor-1",
messages: [{ role: "user", content: readFileSync("invoice.txt", "utf8") }],
response_format: { type: "json_schema", json_schema: { name: "invoice", schema } },
});
const inv = JSON.parse(r.choices[0].message.content!);
const lines = Math.round(inv.line_items.reduce((s: number, i: any) => s + i.amount, 0) * 100) / 100;
if (lines !== inv.subtotal) throw new Error(`line items ${lines} != subtotal ${inv.subtotal}`);
if (inv.purchase_order_number === null) console.log("no PO quoted: route for approval");
When to trust it
Every field in the example came back right, including the null PO number. Before posting to the ledger, check that the line amounts add up to the subtotal and that subtotal plus tax equals the total: three lines of code that catch a misread digit. Give the currency as an enum, as here; asked for an open string, the model is freer to answer with something like a country code.
Try it without code in the playground, or see more Extractor recipes: pull the key terms out of a contract, turn a support email into a ticket, get action items out of meeting notes, turn a receipt photo into an expense entry, extract memories from a conversation.