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

# Parse & Extract

> Parse a document, extract specific fields from its Markdown with a JSON schema, and save both responses with the Python or TypeScript library.

Parse a document with the [Parse API](./parse), then pull specific fields from the parsed Markdown with the [Extract API](./extract). Extract takes Markdown and a JSON schema, so every extraction starts with a parse.

The tutorial uses the [ade-python library](./ade-python) and [ade-typescript library](./ade-typescript) to:

* Parse this PDF: [Wire Transfer Form](/examples/wire-transfer.pdf)
* Extract these fields: **Bank Name** and **Total Invoice Amount**
* Save the parse response and the extract response as JSON files

> [!NOTE]
>
> These examples require the [Python](./ade-python) or [TypeScript](./ade-typescript) client library. Before running a script, set your API key and install the library and any required dependencies.

> [!NOTE]
>
> The scripts parse with a synchronous request, which consumes credits at the full rate. To parse many documents, submit them as [Parse Jobs](./parse-async) on the `standard` tier, which consume half the credits. See [Parse a Directory of Documents](./parse-directory-sample).

## 1. Download the Document to Process

Download the [Wire Transfer Form](/examples/wire-transfer.pdf) and save it to a local directory.

## 2. Create the Script

Copy the script for your language and save it as `parse-extract.py` or `parse-extract.ts` in the same directory as the PDF.

```python Python [expandable] theme={null}
import json
from pathlib import Path
from landingai_ade import LandingAIADE

# Initialize client (uses VISION_AGENT_API_KEY environment variable)
client = LandingAIADE()

# Define the extraction schema
schema = {
    "type": "object",
    "properties": {
        "bank_name": {
            "description": "The official name of the bank where the account is held.",
            "x-alternativeNames": ["Name of Bank", "Financial Institution", "Bank"],
            "type": "string"
        },
        "total_invoice_amount": {
            "description": "The total monetary amount of the invoice, including all charges and taxes.",
            "x-alternativeNames": ["Grand Total", "Amount Due", "Invoice Total"],
            "type": "number"
        }
    }
}

# Parse the document
# save_to is optional. It saves the full parse response (Markdown, structure, and metadata)
# to output/wire-transfer_parse_output.json, which you need to ground the extracted values later
parse_response = client.v2.parse(
    document=Path("wire-transfer.pdf"),
    model="dpt-3-pro-latest",
    save_to="output"
)

# Extract fields from the parsed Markdown and save the extract response
extract_response = client.v2.extract(
    schema=schema,
    markdown=parse_response.markdown,
    model="extract-latest",
    save_to="output/wire-transfer_extract_output.json"
)

print(json.dumps(extract_response.extraction, indent=2))
```

```typescript TypeScript [expandable] theme={null}
import LandingAIADE from "landingai-ade";
import fs from "fs";

// Initialize client (uses VISION_AGENT_API_KEY environment variable)
const client = new LandingAIADE();

// Define the extraction schema
const schema = {
  type: "object",
  properties: {
    bank_name: {
      description: "The official name of the bank where the account is held.",
      "x-alternativeNames": ["Name of Bank", "Financial Institution", "Bank"],
      type: "string"
    },
    total_invoice_amount: {
      description: "The total monetary amount of the invoice, including all charges and taxes.",
      "x-alternativeNames": ["Grand Total", "Amount Due", "Invoice Total"],
      type: "number"
    }
  }
};

async function main(): Promise<void> {
  // Parse the document
  // saveTo is optional. It saves the full parse response (Markdown, structure, and metadata)
  // to output/wire-transfer_parse_output.json, which you need to ground the extracted values later
  const parseResponse = await client.v2.parse({
    document: fs.createReadStream("wire-transfer.pdf"),
    model: "dpt-3-pro-latest",
    saveTo: "output"
  });

  // Extract fields from the parsed Markdown and save the extract response
  const extractResponse = await client.v2.extract({
    schema: schema,
    markdown: parseResponse.markdown,
    model: "extract-latest",
    saveTo: "output/wire-transfer_extract_output.json"
  });

  console.log(JSON.stringify(extractResponse.extraction, null, 2));
}

main();
```

## 3. Run the Script

Run the script from the same directory:

```bash Run Python theme={null}
python parse-extract.py
```

```bash Run TypeScript theme={null}
npx tsx parse-extract.ts
```

## 4. View the Output

The script prints the extracted values and saves two files to an `output` folder in the same directory:

| File | Contents |
| - | - |
| `wire-transfer_parse_output.json` | The full parse response: `markdown`, the `structure` tree of pages and blocks with the page number and bounding box of each, and `metadata`. See [Parse API Response](./parse-response). |
| `wire-transfer_extract_output.json` | The full extract response. See [Extract API Response](./extract-response). |

The extract response has these top-level fields:

| Field | Contents |
| - | - |
| `extraction` | The extracted values, in the shape of your schema: `bank_name` and `total_invoice_amount`. |
| `extraction_metadata` | The same shape, with each value replaced by a `{value, ranges}` object. Each range is a `{start, end}` pair of character offsets into the Markdown the value was copied from, and `ranges` is `null` for a value the model did not copy from the document. A value that appears in more than one place has one range per place, like `total_invoice_amount` on the wire transfer form. |
| `markdown` | The Markdown you sent, echoed back unchanged. Every range indexes into this string. |
| `metadata` | Request details, including `doc_id`: the `job_id` of the parse whose Markdown was extracted. |
| `schema_violation_error` and `warnings` | Set when the extraction is partial or has non-fatal warnings. See [Response Structure](./extract-response#response-structure). |

A run on the wire transfer form saved this `wire-transfer_extract_output.json`, shown with the IDs and most of the Markdown shortened. Your ranges and metadata can differ, because they depend on your parse and the model version:

```json wire-transfer_extract_output.json theme={null}
{
  "extraction": {
    "bank_name": "JPMorgan Chase Bank, N.A.",
    "total_invoice_amount": 15750.0
  },
  "extraction_metadata": {
    "bank_name": {
      "value": "JPMorgan Chase Bank, N.A.",
      "ranges": [{ "start": 186, "end": 224 }]
    },
    "total_invoice_amount": {
      "value": 15750.0,
      "ranges": [{ "start": 960, "end": 970 }, { "start": 112, "end": 150 }]
    }
  },
  "markdown": "WIRE TRANSFER FORM\n\n\n### Invoice Information\n\n...",
  "metadata": {
    "job_id": "extract-...",
    "doc_id": "parse-...",
    "model_version": "extract-20260819",
    "billing": { "service_tier": "priority", "total_credits": 1.0 },
    "range_units": "unicode_codepoints"
  },
  "schema_violation_error": null,
  "warnings": []
}
```

## Next Steps

Now that you have a working script, you can:

* Replace `wire-transfer.pdf` with any document you want to parse and extract from.
* Modify the `schema` to extract different fields. For guidance, see [Extraction Schema (JSON)](./ade-extract-schema-json).
* Find each extracted value on the page: pass `extraction_metadata` and the parse's `structure` to the Ground API. See [Link Extracted Data to Document Locations](./extract-grounding-sample).


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