PARAS MADAN
All writing

Latest in AI

Extract invoice fields with ScaleDown Extract-1 on Vercel AI Gateway

Call the AI SDK extraction API, preserve missing values and source evidence, and decide which invoice fields need human review.

The invoice number a parser found and the invoice number it guessed should never land in the same database column without a way to tell them apart. Vercel's November 23 AI Gateway announcement introduces a dedicated extraction route with ScaleDown Extract-1. It returns answers keyed by the fields you request. A field can come back missing, and a match can carry a confidence score and a pointer to the source.

Start with two fields you can check by eye

The Gateway quickstart uses Node.js 22.18 or later and a server-side AI_GATEWAY_API_KEY. Use ai@7.0.176 or later. The extraction call is experimental, so pin the version you use for evaluation. This route is AI SDK only; pointing an OpenAI-compatible or Anthropic-compatible client at the Gateway doesn't give you this extraction interface.

Start with a synthetic invoice that contains two identifiers. That makes it possible to test whether the extractor can tell a purchase order from an invoice:

import { experimental_extract } from 'ai';

const state = 'Invoice REF-341. Purchase order PO-19. Billing contacts: Ada, Noor.';
const { answers } = await experimental_extract({
  model: 'scaledown/extract-1',
  state,
  fields: {
    invoiceId: {
      type: 'text',
      instructions: 'Invoice identifier. Do not use the purchase order number.',
    },
    billingContacts: {
      type: 'list',
      items: 'text',
      instructions: 'Names explicitly listed as billing contacts.',
    },
  },
});

const answer = answers.invoiceId;
const source = answer.source;
const evidence = source?.type === 'text'
  ? state.slice(source.start, source.end)
  : null;

console.log({ value: answer.value, confidence: answer.confidence, evidence });

For this fixture, the expected value is REF-341. Remove that string and rerun. Your acceptance rule should reject PO-19 as a substitute and handle an absent invoice ID as null.

Keep the evidence next to the value

The AI SDK answer reference describes optional source fields. For text, offsets use UTF-16 code units, so JavaScript's slice can recover the cited span. Keep the original text around: normalizing whitespace or Unicode first can shift the offsets. If you pass multiple text parts, use source.part to select the right one before slicing.

For lists, inspect the evidence and confidence for each item when present. One clear contact name doesn't make the other name correct. A nullable value and a low-confidence value have different meanings, but both can warrant review. Don't interpret a missing confidence score as a perfect match or a score of 0.9 as a measured 90% accuracy rate for your documents.

Test PDFs separately

The Gateway extraction reference lists images and PDF files for Extract-1. It lists only text fields and lists of text for this model, even though the general SDK has number and date types. For amounts or dates, extract the exact source text first and normalize it in application code where you can handle ambiguous formats.

Use a synthetic PDF supplied as file bytes in state for the first document test. Don't build a review screen on the assumption that a file source comes with the same character offsets as a text source. Compare missing values, wrong values and review workload separately across text, clean PDFs and scans. Add examples with missing fields and multiple competing IDs before choosing any threshold for automatic acceptance.


I'm Paras. I work at the intersection of AI, engineering and distribution.

I help AI teams write technical guides that show how to integrate new APIs and check the results before shipping.

Book a 30-minute call