> ## Documentation Index
> Fetch the complete documentation index at: https://documentation.datalab.to/llms.txt
> Use this file to discover all available pages before exploring further.

# Command Line Interface

> Use the Datalab CLI to convert documents from the command line.

## Installation

The CLI is included with the SDK:

```bash theme={null}
pip install datalab-python-sdk
```

## Authentication

Set your API key as an environment variable:

```bash theme={null}
export DATALAB_API_KEY=your_api_key_here
```

Or pass it with each command:

```bash theme={null}
datalab convert document.pdf --api_key YOUR_API_KEY
```

Or sign in once with a device code:

```bash theme={null}
datalab login    # prints a link and a code; approve it in the browser
datalab logout   # deletes the saved key
```

`datalab login` saves the key to `~/.config/datalab/config.json`, readable only by your user. Every `datalab` command uses it when `--api_key` is not passed and `DATALAB_API_KEY` is not set, but only for the host you signed in to. For another `--base_url`, run `datalab login --host <url>` or pass a key.

> [!NOTE]
>
> `datalab login` needs SDK 0.7.0 or newer.

## Convert Documents

Convert documents to markdown, HTML, JSON, or chunks.

### Basic Usage

```bash theme={null}
# Convert a single file
datalab convert document.pdf

# Convert to specific format
datalab convert document.pdf --format html

# Convert with processing mode
datalab convert document.pdf --mode accurate
```

### Output Options

```bash theme={null}
# Save to specific directory
datalab convert document.pdf --output_dir ./output/

# Output formats
datalab convert document.pdf --format markdown
datalab convert document.pdf --format html
datalab convert document.pdf --format json
datalab convert document.pdf --format chunks
```

### Processing Options

```bash theme={null}
# Processing modes
datalab convert document.pdf --mode lite       # Text, tables and lists; no images or add-ons
datalab convert document.pdf --mode fast       # Lowest latency (default)
datalab convert document.pdf --mode balanced   # Balance of speed and accuracy
datalab convert document.pdf --mode accurate   # Highest accuracy

# Limit pages
datalab convert document.pdf --max_pages 10

# Specific page range (0-indexed)
datalab convert document.pdf --page_range "0-5,10,15-20"

# For spreadsheets, page_range filters by sheet index
datalab convert workbook.xlsx --page_range "0,2"

# Add page delimiters
datalab convert document.pdf --paginate
```

### Advanced Options

```bash theme={null}
# Add block IDs for citations (HTML only)
datalab convert document.pdf --format html --add_block_ids

# Disable image extraction
datalab convert document.pdf --disable_image_extraction

# Disable image captions
datalab convert document.pdf --disable_image_captions

# Skip cached results
datalab convert document.pdf --skip_cache
```

### Directory Processing

Convert all documents in a directory:

```bash theme={null}
# Convert all supported files
datalab convert ./documents/ --output_dir ./output/

# Filter by extension
datalab convert ./documents/ --extensions pdf,docx

# Control concurrency
datalab convert ./documents/ --max_concurrent 5
```

Each input saves to its own folder under the output directory, which mirrors the input directory. For example, `./documents/a/report.pdf` saves to `./output/a/report/report.md`, with its images in the same folder, so files with the same name in different folders keep separate results. Without `--output_dir`, results go to `./output/`; pass `--output_dir .` to save them in the working directory, as before SDK 0.14.0. The CLI prints the full output path when a run starts and in its summary.

Each output folder holds a `.datalab-output` file with the input's file name. A directory scan skips these folders, so running the same command again does not process its own output. The CLI checks every output folder before it submits anything. It stops if two inputs would share a folder (for example `report.pdf` and `report.docx` in the same folder; process one type with `--extensions pdf`), if a folder already exists and was not made by the CLI, if a folder holds another input's results from an earlier run, or if one input's folder would be inside another's (for example `report.pdf` next to a `report/` folder of documents).

### Convert Command Reference

| Option | Description |
| - | - |
| `--format` | Output format: `markdown`, `html`, `json`, `chunks` |
| `--mode` | `lite`, `fast` (CLI default), `balanced`, `accurate`; Lite has no add-ons or checkpoints |
| `--output_dir`, `-o` | Output directory (default: `./output`) |
| `--max_pages` | Maximum pages to process |
| `--page_range` | Specific pages (e.g., `"0-5,10"`) |
| `--paginate` | Add page delimiters |
| `--add_block_ids` | Add block IDs to HTML output |
| `--disable_image_extraction` | Don't extract images |
| `--disable_image_captions` | Don't generate image captions |
| `--skip_cache` | Force reprocessing |
| `--extensions` | File extensions to process (for directories) |
| `--max_concurrent` | Maximum concurrent requests |
| `--max_polls` | Maximum polling attempts |
| `--poll_interval` | Seconds between polls |
| `--api_key` | Datalab API key |
| `--base_url` | API base URL |

## Extract Structured Data

Extract structured data from documents using a JSON schema.

### Basic Usage

```bash theme={null}
# Extract data using a page schema
datalab extract invoice.pdf \
  --page_schema '{"type":"object","properties":{"invoice_number":{"type":"string"},"total":{"type":"number"}}}'

# Choose extraction quality independently from parse quality
datalab extract invoice.pdf \
  --page_schema '{"type":"object","properties":{"title":{"type":"string"}}}' \
  --extraction_mode accurate \
  --mode accurate

# Extract using a checkpoint from a previous conversion
datalab extract invoice.pdf \
  --page_schema '{"type":"object","properties":{"total":{"type":"number"}}}' \
  --checkpoint_id "ckpt_abc123"
```

To request phrase citations with SDK 0.9.2 or later (open beta; the format may change):

```bash CLI theme={null}
datalab extract invoice.pdf \
  --page_schema '{"type":"object","properties":{"total":{"type":"number"}}}' \
  --extraction_mode balanced \
  --citation_granularity phrase
```

### Extract Command Reference

| Option | Description |
| - | - |
| `--page_schema` | JSON schema defining fields to extract; use this or `--schema_id` |
| `--schema_id`, `--schema_version` | Reuse a saved extraction schema, optionally pinned to a version |
| `--extraction_mode` | `turbo`, `fast`, `balanced`, or `accurate`; independent of the parse mode |
| `--citation_granularity` | `block` (API default) or `phrase` (open beta, SDK 0.9.2+); unavailable in Turbo |
| `--checkpoint_id` | Checkpoint ID from a previous conversion |
| `--format` | Output format: `markdown`, `html`, `json`, `chunks` |
| `--mode` | Parse mode: `fast`, `balanced`, `accurate` |
| `--output_dir`, `-o` | Output directory (default: `./output`) |
| `--max_pages` | Maximum pages to process |
| `--page_range` | Specific pages (e.g., `"0-5,10"`) |
| `--skip_cache` | Force reprocessing |
| `--api_key` | Datalab API key |
| `--base_url` | API base URL |

## Segment Documents

Segment documents into logical sections using a schema.

### Basic Usage

```bash theme={null}
# Segment a document
datalab segment report.pdf \
  --segmentation_schema '{"segments": [{"name": "intro", "description": "Introduction"}, {"name": "body", "description": "Main content"}]}'

# Segment with a checkpoint
datalab segment report.pdf \
  --segmentation_schema '{"segments": [{"name": "summary", "description": "Executive summary"}]}' \
  --checkpoint_id "ckpt_abc123"
```

### Segment Command Reference

| Option | Description |
| - | - |
| `--segmentation_schema` | **(Required)** JSON schema defining segment names and descriptions |
| `--checkpoint_id` | Checkpoint ID from a previous conversion |
| `--mode` | Parse mode: `fast`, `balanced`, `accurate` |
| `--output_dir`, `-o` | Output directory (default: `./output`) |
| `--max_pages` | Maximum pages to process |
| `--page_range` | Specific pages (e.g., `"0-5,10"`) |
| `--skip_cache` | Force reprocessing |
| `--api_key` | Datalab API key |
| `--base_url` | API base URL |

## Track Changes

Extract tracked changes from DOCX files, PDFs, and images. DOCX files use exact XML extraction; PDFs and images use a vision model.

### Basic Usage

```bash theme={null}
# Extract tracked changes from a Word document
datalab track-changes contract.docx

# Specify output format
datalab track-changes contract.docx --format html

# With pagination
datalab track-changes contract.docx --format html --paginate
```

### Track Changes Command Reference

| Option | Description |
| - | - |
| `--format` | Comma-separated output formats: `markdown`, `html`, `chunks` (default: all three) |
| `--paginate` | Add page delimiters to output |
| `--output_dir`, `-o` | Output directory (default: `./output`) |
| `--api_key` | Datalab API key |
| `--base_url` | API base URL |

## Published Processor (Beta)

> [!NOTE]
>
> This feature is currently in beta. The API may change.

Run an existing published processor that your team can access:

```bash CLI theme={null}
datalab agent document.pdf --processor cp_abc123 --output_dir ./output/

# Pin a published version and control verification effort.
datalab agent document.pdf \
  --processor cp_abc123@2.0 \
  --verification_effort accurate \
  --mode balanced
```

The processor determines its task and output format. See [Run a Published Processor](https://documentation.datalab.to/docs/recipes/pipelines/custom-processors.md) for prerequisites and SDK examples.

| Option | Description |
| - | - |
| `--processor` | **Required.** Published processor ID, optionally followed by `@version` |
| `--mode` | `fast` or `balanced`; API default: `balanced` |
| `--verification_effort` | `fast`, `balanced`, or `accurate`; API default: `fast` |
| `--return_images` | Return run images inline when supported |
| `--skip_cache` | Run again instead of using cached output |
| `--output_dir`, `-o` | Output directory (default: `./output`) |
| `--extensions` | Comma-separated extensions for directory processing |
| `--max_concurrent` | Maximum concurrent requests for directories (default: 5) |
| `--max_polls` | Maximum polling attempts (default: 300) |
| `--poll_interval` | Seconds between polls (default: 1) |
| `--api_key` | Datalab API key |
| `--base_url` | API base URL |

## Create Document

Create a DOCX document from markdown with track changes.

### Basic Usage

```bash theme={null}
# Create a document from a markdown file
datalab create-document --markdown input.md --output output.docx

# Create a document from inline markdown content. $'...' turns \n into a newline;
# inside plain double quotes, \n stays a literal backslash and n.
datalab create-document \
  --markdown $'# Title\n\nDocument content here.' \
  --output document.docx
```

### Create Document Command Reference

| Option | Description |
| - | - |
| `--markdown` | **(Required)** Markdown content or path to a markdown file |
| `--output`, `-o` | **(Required)** Output file path for the generated DOCX |
| `--api_key` | Datalab API key |
| `--base_url` | API base URL |

## Compare (Open Beta)

> [!NOTE]
>
> This feature is currently in beta. The API may change.

Compare 2 to 4 variants of a pipeline on the same files, and see which one
reads them best. See [Compare with the SDK and API](https://documentation.datalab.to/docs/recipes/compare/sdk.md)
for the request and response shapes. These commands need SDK 0.11.0 or later.
`datalab eval` is the deprecated SDK 0.10.0 name of these commands.

```bash Run and wait theme={null}
datalab compare run invoice_1.pdf invoice_2.pdf --pipeline pl_abc123 --wait
```

```bash Check later theme={null}
datalab compare get COMPARISON_ID
datalab compare list --pipeline_id pl_abc123
datalab compare differences COMPARISON_ID FILE_ID
```

The summary from `run --wait` and `get` lists each variant's share and each
file's ID, which `compare differences` takes (SDK 0.15.0 or later).

| Option | Description |
| - | - |
| `--pipeline` | **Required** on `run`. Pipeline ID to compare (`pl_XXXXX`) |
| `--collection_id` | Compare the files of a collection instead of file paths |
| `--variant-json` | 2 to 4 variants as JSON, or a path to a JSON file. Omit for the default Fast/Balanced/Accurate comparison |
| `--version` | Pipeline version to run (`0`=draft, omit=active) |
| `--wait` | Poll until the comparison finishes and print a summary. Exits with status 1 if the comparison fails |
| `--poll_interval` | Seconds between polls with `--wait` (default: 10) |
| `--timeout` | Maximum seconds to wait with `--wait` (default: 7200) |
| `--json` | Print the raw result as JSON instead of a summary |
| `--api_key` | Datalab API key |
| `--base_url` | API base URL |

## Exit Status

The document commands (`convert`, `extract`, `segment`, `track-changes`, `fill`, `form-fields`, `agent` and `create-document`) exit with status 1 when any file fails, so scripts and CI jobs can check the result. Invalid options exit with status 2.

After a run over several files, the summary gives the number of files that succeeded and failed, and names each failed file with its error. It names the output directory only when the run saved results to it, and says `No output was saved.` when it saved none. A run that saves nothing leaves no empty output directory behind.

`compare run --wait` exits with status 1 when the comparison fails or any of its files fails. Skipped files don't count as failures. `compare get` exits 0 for a failed comparison, because it reports the status it read.

## Examples

### Batch Convert PDFs

```bash theme={null}
# Convert all PDFs in a directory with accurate mode
datalab convert ./invoices/ \
  --extensions pdf \
  --mode accurate \
  --format json \
  --output_dir ./processed/
```

### Extract Data from Documents

```bash theme={null}
# Extract structured data using a schema
datalab extract invoice.pdf \
  --page_schema '{
    "invoice_number": {"type": "string", "description": "Invoice ID"},
    "total": {"type": "number", "description": "Total amount"},
    "vendor": {"type": "string", "description": "Vendor name"}
  }' \
  --mode balanced \
  --output_dir ./extracted/
```

### High-Throughput Processing

```bash theme={null}
# Process many files with high concurrency
datalab convert ./documents/ \
  --max_concurrent 10 \
  --mode fast \
  --output_dir ./output/
```

## Getting Help

```bash theme={null}
# General help
datalab --help

# Command-specific help
datalab convert --help
datalab extract --help
datalab segment --help
datalab track-changes --help
datalab agent --help
datalab create-document --help
```

## Next Steps

> [card] [Quickstart](https://documentation.datalab.to/docs/welcome/quickstart.md)
>
> Get up and running with Datalab in minutes.

> [card] [Batch Processing](https://documentation.datalab.to/docs/recipes/conversion/batch-documents.md)
>
> Process multiple documents efficiently in parallel.

> [card] [SDK Reference](https://documentation.datalab.to/docs/welcome/sdk.md)
>
> Explore the full Python SDK for advanced usage.

> [card] [Supported File Types](https://documentation.datalab.to/docs/common/supportedfiletypes.md)
>
> See all document formats supported by Datalab.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.