AI invoice reader project (TypeScript) · Chapter 3 of 12

The schema

By the end of this chapter you can describe an invoice as zod schemas (money as numbers, dates as ISO text, limits on every value, fields that must be there even when they are empty), read JSON text into an invoice with one line per problem when it does not fit, and build the strict JSON schema that chapter 4 sends the model. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: From PDF to data
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of AI invoice reader project (TypeScript) are free. This one comes with the all labs plan.

Why you need this

Chapter 2 gives you an invoice's text. From chapter 4 on, a model reads that text and writes back what it found, as JSON. Before you ask, you have to say exactly what you want back, and your code has to refuse anything else.

Vaishali's September mistakes show why each field needs a type, not just a name:

  • ₹48,600 typed as ₹46,800. Money has to be a number the code can add and compare, not text such as "Rs. 48,600".
  • A date typed month first. "08/10/2026" is 8 October to Vaishali and 10 August to a program from another country. ISO text, 2026-10-08, has only one meaning.
  • A GSTIN typo that cost a tax credit. When an invoice prints no GSTIN, the reader must say so (null), not leave the field out: a missing field could also mean "forgot to look".

JSON.parse checks none of this. It returns any, and TypeScript believes whatever you say about it: a quantity of 0, a total of "Rs. 36,595.12" and an extra key "verified": true all go straight in. TypeScript's types vanish when the code runs; a check has to run too.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    schema.ts: an invoice as zod schemas

    In src/extract/schema.ts give LineItem its five fields: description (a string of at least 1 character), quantity (a number more than 0), unit (a string or null), rate and amount (numbers, 0 or more). Give Invoice its ten: supplier_name (a string of at least 1 character), supplier_gstin (a string or null), invoice_number (a string of at least 1 character), invoice_date (an ISO date, z.iso.date()), line_items (an array of LineItem, at least 1), subtotal, cgst, sgst and igst (numbers or null), total (a number, 0 or more). Both objects are .strict(). Every field is required: one the invoice may not print is .nullable(), never .optional().

    The Check parses a good invoice and seventeen bad ones that must all be refused: a quantity of 0, a negative rate or amount, empty text, a missing or extra key, no lines, 30 February, 08/10/2026, "Rs. 36,595.12" and others.

  2. Task 2

    schema.ts: JSON text in, an Invoice or its problems out

    Write readInvoiceJson(text). JSON.parse the text; if that throws, throw new InvoiceJsonError([invoice: ${error.message}]). Then Invoice.safeParse the value: on success return its data; otherwise throw new InvoiceJsonError(problems), one string per issue of result.error.issues: "<path>: <message>", the path's parts joined with dots ("line_items.0.quantity"), or "invoice" when the path is empty.

    The Check reads good JSON, then a quantity of 0, JSON in a code fence, and a total of "lots" with an extra key, and compares your problems with its own.

  3. Task 3 · interview question

    Interview: schema.ts, the strict response format

    Write responseFormat(): return zodResponseFormat(Invoice, 'invoice') (from openai/helpers/zod): { type: "json_schema", json_schema: { name: "invoice", strict: true, schema } }, the schema written from your zod Invoice.

    The Check compares it with its own, then walks every object in the schema (the invoice and a line item): each must say additionalProperties false and list every property in required. Then GET /invoices/schema.

    Nikhil will ask: some vendors print no GSTIN. Why is supplier_gstin still in the schema's required list?

What you will learn

  • zod schemas: z.object, z.string, z.number, z.array, and safeParse, which checks a value when it comes in.
  • Limits: .min(1) for text and for arrays, .positive() and .nonnegative() for numbers, .strict() for objects.
  • Money and dates: numbers of rupees, and z.iso.date(), which also refuses 30 February.
  • Required, nullable, optional: three different things, and why this schema is never optional.
  • Reading JSON text: two kinds of failure, and zod's issues with their paths.
  • JSON schema and strict: what zodResponseFormat writes, and what a strict structured output demands.

More in From PDF to data

Build The schema yourself

Parda's venues get invoices from sound, printing and canteen vendors, typed and messy. You build the pipeline that reads them, checks every rupee, and sends the doubtful ones to a person. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of AI invoice reader project (TypeScript) are free. This one comes with the all labs plan.