Perception

Classify

client.documents.classify(params, options?) labels a document, or each page, with your classes. It takes the shared parameters, except that source can't be a parse_result, and:

ParameterTypeRequiredDescription
classesArray<{ id, label, description?, criteria?, subclasses? }>YesYour labels. See classes.
granularity"document" | "page"NoScore the whole file once, or once per page. Default: "document".
page_rangesArray<{ start, end }>NoPages to read and classify. Not for workbooks. Default: all pages.
unknown_policy"allow" | "force_best"No"allow" (default) may mark a unit unknown; "force_best" commits an ambiguous unit to one of your classes.
output{ max_alternatives?, include_reason? }NoHow many labels, and whether each has a reason. See output.
parse_mode"low" | "medium" | "high"NoHow hard the parse behind the classification works. Default: "low".

Each object option takes these keys:

classes

One entry per label, each an object:

KeyTypeDefaultDescription
classes[].idstringRequiredThe label's id, returned on the results.
classes[].labelstringRequiredThe label's name.
classes[].descriptionstring""What belongs in the class.
classes[].criteriaArray<string>NoneExtra rules to apply when scoring this class.
classes[].subclassesArray<{ id, label, description?, criteria? }>NoneFiner labels under this class, each with subclasses[].id and subclasses[].label (both required), and an optional subclasses[].description and subclasses[].criteria.

output

KeyTypeDefaultDescription
output.max_alternativesnumber3The ranked labels to return per unit, from 1 to 10.
output.include_reasonbooleantrueAdd a short reason to each label.
classify.ts
const job = await client.documents.classify({
  source: { type: "url", url: "https://example.com/invoice.pdf", file_name: "invoice.pdf" },
  classes: [
    { id: "invoice", label: "Invoice", description: "A supplier invoice" },
    { id: "contract", label: "Contract", description: "A signed agreement between two parties" },
  ],
  granularity: "page",
});
const done = await client.jobs.wait(job.job_id);

Class ids must be unique and can't contain /, and other, blank_page, unreadable_content and __ndi_unclassified are reserved. See Classify.

On this page