Using Arrays for Multi-Item Extraction
Build rigid array schemas to extract structured lists of line items reliably from documents using LLM APIs.
To extract a clean list of items from a single document, define an array type directly inside your structured JSON schema response. Setting item-level property schemas guarantees that every extracted record carries uniform keys. This structure allows your application to process lists directly without extra post-processing code or regex splitting.
Define Array Types in JSON Schema
Extracting multiple records requires an array type definition at the root or within a top-level property. Use the items keyword to enforce uniform types for every array element JSON Schema object reference.
{
"type": "object",
"properties": {
"expense_items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"description": { "type": "string" },
"amount": { "type": "number" }
},
"required": ["description", "amount"]
}
}
},
"required": ["expense_items"]
}
Defining required keys inside the items schema prevents incomplete child records.
Process Line Items From Unstructured Text
Pass an unstructured invoice string to an API configured with your array extraction schema. Input text might contain two disjoint line items, such as software licenses and server rentals.
The parsing engine maps the text into the configured schema structure:
{
"expense_items": [
{ "description": "Software License", "amount": 49.99 },
{ "description": "Server Rental", "amount": 150.00 }
]
}
Your application iterates directly over this parsed list without secondary regex checks.
Diagnose Truncated Arrays and Schema Drops
Array extraction often breaks when documents contain lengthy tables. Generative responses can run into output token limits mid-list, leaving open brackets or trailing commas.
Setting unstructured output modes without enforcing strict array schemas leads to unpredictable field naming or dropped properties inside array objects. Enforce strict object validation parameters at the client layer. If documents frequently hit limit boundaries, chunk the raw text into distinct sections before calling the parsing API.
Verify Extracted Lists in Downstream Applications
Set up programmatic validation routines to confirm array formats before inserting data into database schemas. Ensure array outputs pass structural, type, and count checks.
- Run JSON schema validation to verify object shapes inside the list.
- Assert that the returned array length is greater than zero for valid source documents.
- Validate numerical bounds on extracted monetary values.
Route outputs with missing required properties or zero extracted items to a staff operational review queue.