Skip to content

Python SDK

Updated

Install#

pip install docxtract-sdk
Note
the distribution is docxtract-sdk; the import stays docxtract. The shorter name was already taken on PyPI by an unrelated DOCX text extractor.

Python 3.9+. No dependencies — standard library only, so the install pulls nothing and cannot conflict with your project's pinned requests or httpx.

Extract a document#

import os
from docxtract import DocXtract

dx = DocXtract(os.environ["DOCXTRACT_API_KEY"])
result = dx.extract("invoice.pdf", model="invoice")

print(result["vendor"])
print(result.get("line_items.0.hsn"))
Note
DocXtract keys use an underscore (sk_). A hyphen means the key is from a different API provider; the SDK rejects it immediately rather than letting you debug a 401.

Large PDFs#

A PDF over 3 pages returns 202 and requires the multi-page flow. extract() handles it:

result = dx.extract("500-page-statement.pdf", model="bank_statement")

Same call, any page count. Progress callback:

dx.extract("big.pdf", model="invoice",
           on_progress=lambda done, total, stage: print(f"{done}/{total}"))

Chunks run sequentially by design — the default rate limit is 10 requests per minute, so parallel calls only produce 429s.

Tabular extractions#

Invoice line items and bank statement rows come back as lists of dicts, so they drop straight into pandas:

df = result.to_dataframe("line_items")

Needs pip install 'docxtract-sdk[pandas]'. With no argument it uses the first row-shaped list it finds in the extracted data.

Error handling#

from docxtract import DocXtractError, RateLimitError, QuotaError

try:
    dx.extract("invoice.pdf", model="invoice")
except RateLimitError as exc:
    time.sleep(exc.retry_after or 30)
except QuotaError:
    pass                       # out of credits — do not retry
except DocXtractError as exc:
    if exc.retryable:
        requeue()
    else:
        raise

retryable is conservative: input errors, expired jobs, and exhausted credits are all False. See Error codes for the full mapping.

Discovering document types#

dx.models()   # costs no credits

Configuration#

ArgumentDefaultNotes
api_keyRequired
base_urlhttps://api.docxtract.ioNo /api prefix
base_path/v3.1/v3 only if pinned to the old version
timeout120Seconds
max_retries3Retryable errors only
chunk_pause_ms0Pace chunk calls on tight rate limits

Pre-flight validation runs locally: a bad file type or oversized file fails before any request, costing no round trip and no credits.

Full reference#

sdk/python/README.md in the platform repository.