datalab-cli 0.1.0

A powerful CLI for converting, extracting, and processing documents using the Datalab API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# Extracting Structured Data

Learn how to extract specific fields from documents using JSON schemas.

---

## Prerequisites

- [Datalab CLI installed]../getting-started/installation.md
- [API key configured]../getting-started/configuration.md

---

## Basic Extraction

Extract fields from a document using an inline schema:

```bash
datalab extract invoice.pdf --schema '{
  "fields": [
    {"name": "invoice_number", "type": "string"},
    {"name": "total", "type": "number"},
    {"name": "date", "type": "string"}
  ]
}'
```

Output:
```json
{
  "invoice_number": "INV-2024-001",
  "total": 1250.00,
  "date": "2024-01-15"
}
```

---

## Schema Basics

### Schema Structure

```json
{
  "fields": [
    {
      "name": "field_name",
      "type": "string|number|boolean|array|object",
      "description": "Optional hint for extraction"
    }
  ]
}
```

### Field Types

| Type | Description | Example Value |
|------|-------------|---------------|
| `string` | Text value | `"John Doe"` |
| `number` | Numeric value | `1250.00` |
| `boolean` | True/false | `true` |
| `array` | List of values | `["item1", "item2"]` |
| `object` | Nested object | `{"name": "John", "age": 30}` |

---

## Using Schema Files

For complex schemas, use a file:

```bash
datalab extract document.pdf --schema schema.json
```

### Example: Invoice Schema

Create `invoice-schema.json`:

```json
{
  "fields": [
    {
      "name": "invoice_number",
      "type": "string",
      "description": "The unique invoice identifier"
    },
    {
      "name": "vendor_name",
      "type": "string",
      "description": "Name of the vendor/seller"
    },
    {
      "name": "invoice_date",
      "type": "string",
      "description": "Date the invoice was issued"
    },
    {
      "name": "due_date",
      "type": "string",
      "description": "Payment due date"
    },
    {
      "name": "subtotal",
      "type": "number",
      "description": "Total before tax"
    },
    {
      "name": "tax",
      "type": "number",
      "description": "Tax amount"
    },
    {
      "name": "total",
      "type": "number",
      "description": "Total amount due"
    }
  ]
}
```

Run extraction:

```bash
datalab extract invoice.pdf --schema invoice-schema.json
```

---

## Adding Descriptions

Descriptions help the AI understand what to extract:

```json
{
  "fields": [
    {
      "name": "parties",
      "type": "array",
      "description": "Names of all parties signing the contract, including both individuals and organizations"
    },
    {
      "name": "effective_date",
      "type": "string",
      "description": "The date when the contract becomes effective, not the signing date"
    }
  ]
}
```

Good descriptions:
- Clarify ambiguous terms
- Specify format expectations
- Distinguish similar fields

---

## Nested Objects

Extract complex structured data:

```json
{
  "fields": [
    {
      "name": "customer",
      "type": "object",
      "fields": [
        {"name": "name", "type": "string"},
        {"name": "email", "type": "string"},
        {"name": "phone", "type": "string"},
        {
          "name": "address",
          "type": "object",
          "fields": [
            {"name": "street", "type": "string"},
            {"name": "city", "type": "string"},
            {"name": "state", "type": "string"},
            {"name": "zip", "type": "string"}
          ]
        }
      ]
    }
  ]
}
```

Output:
```json
{
  "customer": {
    "name": "John Doe",
    "email": "john@example.com",
    "phone": "555-123-4567",
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA",
      "zip": "90210"
    }
  }
}
```

---

## Arrays of Items

Extract lists of items:

```json
{
  "fields": [
    {
      "name": "line_items",
      "type": "array",
      "description": "Individual items on the invoice",
      "items": {
        "type": "object",
        "fields": [
          {"name": "description", "type": "string"},
          {"name": "quantity", "type": "number"},
          {"name": "unit_price", "type": "number"},
          {"name": "total", "type": "number"}
        ]
      }
    }
  ]
}
```

Output:
```json
{
  "line_items": [
    {
      "description": "Widget A",
      "quantity": 10,
      "unit_price": 25.00,
      "total": 250.00
    },
    {
      "description": "Widget B",
      "quantity": 5,
      "unit_price": 50.00,
      "total": 250.00
    }
  ]
}
```

---

## Confidence Scores

Get confidence scores for extracted values:

```bash
datalab extract invoice.pdf --schema schema.json --include-scores
```

Output:
```json
{
  "invoice_number": "INV-2024-001",
  "invoice_number_score": 0.95,
  "total": 1250.00,
  "total_score": 0.98,
  "date": "2024-01-15",
  "date_score": 0.92
}
```

### Interpreting Scores

| Score | Confidence | Action |
|-------|------------|--------|
| 0.9 - 1.0 | High | Trust the value |
| 0.7 - 0.9 | Good | Likely accurate |
| 0.5 - 0.7 | Medium | Review recommended |
| 0.0 - 0.5 | Low | Manual verification needed |

---

## Using Checkpoints

### Save Checkpoint

Save parsing work for reuse:

```bash
datalab extract document.pdf --schema schema1.json --save-checkpoint
# Returns checkpoint_id: "ckpt_abc123"
```

### Reuse Checkpoint

Run additional extractions without re-parsing:

```bash
# Extract with different schema, same document
datalab extract document.pdf --schema schema2.json --checkpoint-id ckpt_abc123
```

This is faster and more cost-effective for multiple extractions.

---

## Processing Options

### Processing Mode

```bash
# Fast mode for simple documents
datalab extract invoice.pdf --schema schema.json --mode fast

# Accurate mode for complex documents
datalab extract contract.pdf --schema schema.json --mode accurate
```

### Page Selection

```bash
# Limit pages
datalab extract report.pdf --schema schema.json --max-pages 10

# Specific pages
datalab extract report.pdf --schema schema.json --page-range "0-5,20-25"
```

---

## Practical Examples

### Invoice Processing

```json
{
  "fields": [
    {"name": "invoice_number", "type": "string"},
    {"name": "vendor_name", "type": "string"},
    {"name": "vendor_address", "type": "string"},
    {"name": "invoice_date", "type": "string"},
    {"name": "due_date", "type": "string"},
    {
      "name": "line_items",
      "type": "array",
      "items": {
        "type": "object",
        "fields": [
          {"name": "description", "type": "string"},
          {"name": "quantity", "type": "number"},
          {"name": "unit_price", "type": "number"},
          {"name": "amount", "type": "number"}
        ]
      }
    },
    {"name": "subtotal", "type": "number"},
    {"name": "tax", "type": "number"},
    {"name": "total", "type": "number"}
  ]
}
```

### Resume Parsing

```json
{
  "fields": [
    {"name": "name", "type": "string"},
    {"name": "email", "type": "string"},
    {"name": "phone", "type": "string"},
    {"name": "summary", "type": "string", "description": "Professional summary or objective"},
    {
      "name": "experience",
      "type": "array",
      "items": {
        "type": "object",
        "fields": [
          {"name": "company", "type": "string"},
          {"name": "title", "type": "string"},
          {"name": "start_date", "type": "string"},
          {"name": "end_date", "type": "string"},
          {"name": "responsibilities", "type": "array"}
        ]
      }
    },
    {
      "name": "education",
      "type": "array",
      "items": {
        "type": "object",
        "fields": [
          {"name": "institution", "type": "string"},
          {"name": "degree", "type": "string"},
          {"name": "graduation_date", "type": "string"}
        ]
      }
    },
    {"name": "skills", "type": "array"}
  ]
}
```

### Contract Analysis

```json
{
  "fields": [
    {"name": "contract_type", "type": "string", "description": "Type of contract (NDA, Employment, Service, etc.)"},
    {"name": "parties", "type": "array", "description": "All parties to the contract"},
    {"name": "effective_date", "type": "string"},
    {"name": "termination_date", "type": "string"},
    {"name": "governing_law", "type": "string", "description": "Jurisdiction governing the contract"},
    {
      "name": "key_terms",
      "type": "array",
      "description": "Important terms, conditions, or obligations"
    }
  ]
}
```

---

## Batch Extraction

Process multiple documents:

```bash
#!/bin/bash
schema="invoice-schema.json"

for file in invoices/*.pdf; do
    output="${file%.pdf}.json"
    echo "Extracting $file..."
    datalab extract "$file" --schema "$schema" --output "$output"
done
```

---

## Troubleshooting

### Missing Fields

If fields are not extracted:

1. Add descriptions to clarify what to look for
2. Try accurate mode for complex documents
3. Check if the field exists in the document

### Wrong Values

If values are incorrect:

1. Add more specific descriptions
2. Use confidence scores to identify low-confidence extractions
3. Try accurate mode

### Nested Data Not Extracted

Ensure your schema structure matches the document:

```json
{
  "fields": [
    {
      "name": "address",
      "type": "object",
      "fields": [
        {"name": "street", "type": "string"},
        {"name": "city", "type": "string"}
      ]
    }
  ]
}
```

---

## Next Steps

- [Fill forms with extracted data]fill-forms.md
- [Score extractions]../commands/extract-score.md
- [Learn about checkpoints]../concepts/checkpoints.md