Python SDK
Updated
Install#
pip install docxtract-sdk
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"))
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#
| Argument | Default | Notes |
|---|---|---|
api_key | — | Required |
base_url | https://api.docxtract.io | No /api prefix |
base_path | /v3.1 | /v3 only if pinned to the old version |
timeout | 120 | Seconds |
max_retries | 3 | Retryable errors only |
chunk_pause_ms | 0 | Pace 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.
Something wrong or missing on this page? Tell us.