Skip to main content
To get an LLM to pull the same fields out of 50,000 PDFs without the output format drifting from one document to the next, define the fields once in a document extraction schema, much like a JSON schema, and extract every PDF against that one schema. In Box, that schema is a metadata template, which names each field and sets its type. The Box Extract API (POST /2.0/ai/extract_structured) returns every answer under the template’s field keys, with date fields as RFC 3339 timestamps and float fields as numbers. Box runs OCR, layout parsing, and extraction for you. You can attach a Custom Extract Agent to a folder without writing code, or call the API once per file from a script like the one in this guide. The examples use PDFs, but the API accepts other too.

How a metadata template prevents format drift

Format drift is when the same field comes back with a different name, type, or format from one document to the next. For example, one answer has total: "$1,050.00" and the next has amount_due: 2480.5. A metadata template prevents this in three ways:
  • One definition. Each request references the template by its key, so you don’t copy field definitions into every prompt.
  • Fixed keys and types. Box AI returns values under the template’s field keys, with dates as RFC 3339 timestamps and numbers as JSON numbers.
  • A typed store. When you save an answer as metadata on the file, Box returns 400 Bad Request for any value that doesn’t fit its field. Examples are text in a float field, an enum value that isn’t one of the options, or a key that isn’t in the template.
Box AI also reads each field’s description, so put the expected format there, for example “The three-letter ISO 4217 currency code”. With the template in this guide, Box AI returned these values from five invoice layouts, one of them scanned: Outputs can still vary between runs, and Box AI leaves out fields it can’t find. The script in this guide validates each answer before saving it.

Map a JSON schema to a metadata template

If you already have a JSON schema, map each property to a template field type: Two JSON schema keywords work differently:
  • required has no template equivalent. Box AI leaves out fields it can’t find, so check for required fields in your code.
  • additionalProperties: false needs no setting. The metadata write rejects keys that aren’t in the template.
For values from a known list, such as document types or vendor names you already have, use an enum field instead of string. Box then rejects any other value when you save it.

Choose between a Custom Extract Agent and the API

Both options run OCR, layout parsing, and extraction in Box. They differ in who runs the loop over your files. A Custom Extract Agent uses one metadata template, and Box locks the template when you attach a source folder. Every file in the agent’s folders uses the same schema. Box Extract processes at most 500 extractions per minute per user and 700 per minute per enterprise. See About Box Extract and Using Box Extract. The rest of this guide uses the Box Extract API.

Before you start

  • Store the PDFs in a Box folder. To upload a local folder tree, see .
  • Create a app with the Read and write all files and folders stored in Box and Manage AI scopes, and have an admin authorize it in the Admin Console. See .
  • Invite the app’s service account to the folder as an Editor, so it can read the PDFs and write metadata.
  • Get an admin to create the metadata template if you can’t. Only admins, and co-admins with permission to create and edit metadata templates, can create templates.
  • Plan for AI Units. Each extract call consumes AI Units, and confidence scores consume more. Before a large run, request an allowance increase and tell your Box account team. See the Box AI Units consumption table.
  • Install Python 3.11 or later and the Box Python SDK 10.5 or later:
    If the deprecated box-sdk-gen package is installed in the same environment, uninstall it first. Both packages install the box_sdk_gen module, so having both can break the script’s imports.

Define the document extraction schema

Create a metadata template with one field per value, and put the expected format in each field’s description:
Admins can also create templates in the Admin Console. See .

Test the schema on a sample of PDFs

Before a full run, extract a sample that covers your layouts, including scanned files and documents with missing fields. Pass the template instead of a list of fields:
The answer object uses the template’s field keys. This invoice has no due date, so answer leaves out dueDate:
ai_agent_info shows the model that handled the request. To get a confidence score for each field, add "include_confidence_score": true to the request. The response then has a confidence_score object with entries such as "totalAmount": {"level": "HIGH", "score": 1}. A score of 0.90 or higher is high, 0.70 to 0.89 is medium, and below 0.70 is low. Confidence scores work only with Gemini models, which the default Extract Agent and the Enhanced Extract Agent use. If a value comes back in the wrong form, refine the field’s description and test again. For multi-column layouts, scanned files, or dense tables, add "ai_agent": {"type": "ai_agent_id", "id": "enhanced_extract_agent"} to the request to use the .

Run the extraction across a folder

There is no batch extract endpoint. A request can include up to 25 files, but Box AI returns one combined answer for all of them. Send one file per request and run requests in parallel. The following script extracts every PDF in a folder, checks each answer against the template’s field types, and saves valid answers as metadata on the file. Save it as extract_invoices.py.
Set your app credentials, the template key, and the folder ID, then run the script:
For each file, the script prints a JSON record and appends it to extraction-log.jsonl:
The script:
  • Reads the field definitions from the template once and checks every answer against them. If the answer is empty or a value doesn’t fit its field, the script saves nothing for that file.
  • Skips files that already have an instance of the template, so you can stop and rerun it.
  • Runs 8 workers with a 120-second read timeout. The Box Python SDK retries 429 and 5xx responses, and the script retries 412 responses, starting with a 30-second wait and doubling it each time.
  • Logs the models from ai_agent_info for each extracted file.
The script reads only the top level of the folder. For PDFs in subfolders, run it for each subfolder. It also picks up only files that end in .pdf. To process other supported formats, change the extension check in list_pdfs. Start with a small batch, then raise WORKERS. For pacing guidance, see .

Review the results

Each record in extraction-log.jsonl has a status: The next run retries empty, invalid, and error files. Move files that aren’t the right kind of document out of the folder, so later runs don’t use AI Units on them.

Keep the schema stable during a run

Don’t change the template mid-run. Template changes apply to every file that already has an instance of the template, and files extracted before a description change used the old description. Box updates the default models on a regular basis and announces changes in the developer changelog. Use the models in the log to find files that a different model extracted. To keep a run on one configuration, see .

Nested fields and line items

Metadata templates have no field type for nested objects or repeating rows, such as invoice line items. For those, use inline fields with the struct or table type. A request takes either metadata_template or fields, not both, so extract line items in a separate request or define every field inline. To keep inline fields consistent, define the list once in your code and send the same list with every request. Store the results in your own database, because a metadata template can’t hold them. See and the .
Last modified on December 3, 2026