Structured Data Extraction
After a document has been OCR-processed and classified, the extraction stage pulls out structured data: key-value fields, tables, and dates. The extractor uses an LLM to analyze the raw text and return typed, confidence-scored fields.
This transforms unstructured document content into machine-readable data that can feed into databases, workflows, and downstream systems.
Running Extraction
curl -X POST "http://localhost:8008/api/v1/docs/extract?document_id=DOC_ID"
Response:
{
"fields": [
{"key": "full_name", "value": "Ahmed Al Maktoum", "confidence": 0.97, "field_type": "text"},
{"key": "id_number", "value": "784-1990-1234567-1", "confidence": 0.99, "field_type": "text"},
{"key": "nationality", "value": "UAE", "confidence": 0.96, "field_type": "text"},
{"key": "expiry_date", "value": "2028-03-15", "confidence": 0.94, "field_type": "date"},
{"key": "date_of_birth", "value": "1990-07-22", "confidence": 0.93, "field_type": "date"}
],
"tables": [],
"dates": ["2028-03-15", "1990-07-22"],
"summary": "Emirates ID card for Ahmed Al Maktoum, expiring March 2028"
}
Response Fields
| Field | Type | Description |
|---|---|---|
fields | array | Extracted key-value fields with types and confidence |
tables | array | Extracted tabular data (rows and columns) |
dates | string[] | All dates found in the document (ISO 8601) |
summary | string | LLM-generated one-line summary of the document |
Field Object
| Property | Type | Description |
|---|---|---|
key | string | Field name (e.g., full_name, id_number) |
value | string | Extracted value |
confidence | float | Extraction confidence (0.0-1.0) |
field_type | string | Data type: text, date, number, currency, phone, email |
Extraction by Document Type
The extractor adapts its behavior based on the document classification:
Identity Documents
| Expected Fields | Example Value |
|---|---|
full_name | Ahmed Mohammed Al Maktoum |
full_name_ar | أحمد محمد آل مكتوم |
id_number | 784-1990-1234567-1 |
date_of_birth | 1990-07-22 |
expiry_date | 2028-03-15 |
nationality | UAE |
gender | Male |
Financial Documents
| Expected Fields | Example Value |
|---|---|
vendor_name | Dubai Electricity & Water Authority |
invoice_number | INV-2024-005678 |
total_amount | 1,234.50 AED |
due_date | 2024-02-28 |
payment_status | Pending |
Government Documents
| Expected Fields | Example Value |
|---|---|
issuing_authority | Ministry of Interior |
reference_number | MOI-2024-001234 |
subject | Trade License Renewal |
issue_date | 2024-01-15 |
recipient | Al Futtaim Group LLC |
Table Extraction
For documents containing tabular data (invoices with line items, financial statements with rows), the extractor identifies and structures table content:
{
"tables": [
{
"headers": ["Item", "Quantity", "Unit Price", "Total"],
"rows": [
["Office supplies", "10", "25.00 AED", "250.00 AED"],
["Printer cartridge", "2", "150.00 AED", "300.00 AED"]
]
}
]
}
Validation
After extraction, the validation step checks for completeness and format correctness:
curl -X POST "http://localhost:8008/api/v1/docs/validate?document_id=DOC_ID"
{
"is_valid": true,
"completeness_score": 0.85,
"issues": [
{"field": "nationality", "issue": "Missing required field", "severity": "warning"},
{"field": "expiry_date", "issue": "Date is in the past", "severity": "info"}
]
}
Validation Checks
| Check | Description |
|---|---|
| Required fields | Expected fields for the document type are present |
| Format validation | Dates are valid, ID numbers match expected patterns, phone numbers are well-formed |
| Completeness score | Percentage of expected fields that were successfully extracted |
| Cross-field consistency | Dates are logical (birth date before expiry), amounts sum correctly |
Low Confidence Fields
Fields with confidence below 0.80 should be flagged for human review. The validation endpoint surfaces these automatically in the issues array.
Chat RAG Ingest Pipeline
Docs AI integrates with Anar Chat's knowledge base through an ingest pipeline. Extracted and validated documents can be pushed to Chat's RAG system, making document content searchable and retrievable via the AI assistant.
This enables workflows where a citizen uploads a document, Docs AI processes it, and Chat can immediately answer questions about the document's content.
Configuration
| Variable | Default | Description |
|---|---|---|
LLM_MODEL | llama-3.3-70b-versatile | LLM for classification and extraction |
LLM_ARABIC_MODEL | allam-2-7b | Arabic-optimized LLM |
GROQ_API_KEY | — | Groq API key (required) |