Skip to main content

Privacy-preserving, performant document extraction (text + metadata) for the Knovas Semantix platform. Python reference implementation.

Project description

knovas-extract

CI PyPI Python License

Privacy-preserving, performant document extraction (text + metadata) for the Knovas Semantix platform. Python reference implementation of the cross-language knovas-extract spec.

Status: alpha (0.1.0.dev). API stable in shape, not yet in version. Not yet on PyPI.

What it does

Give it a document (PDF, DOCX, MSG, EML, HTML, RTF, MD, TXT). Get back a ExtractionResult: extracted text, document metadata (title, author, language, dates, page count), per-page text (when paginated), and heading-derived sections.

That's the whole library. It does not chunk, embed, upload to Semantix, or talk to any network. Those concerns belong in your application code.

Why

  • Spec-first: the output contract is shared across every language implementation in the knovas-extract-* family. Your Python output and a future Node/Go/Rust output are guaranteed equivalent (within documented tolerances).
  • Fast: uses the best native library per format (PyMuPDF for PDF, selectolax for HTML, …). PDF throughput ≈ 20–100 pages/sec on a single thread; lazy-imports keep cold start ≈ 50 ms.
  • Safe by default: no network calls, no embedded-code execution, no path traversal, ZIP-bomb caps, XXE-hardened XML, typed errors only. See SECURITY.md for the full posture.

Install

pip install knovas-extract                       # core only (TXT/MD/HTML/EML)
pip install 'knovas-extract[pdf]'                # + PyMuPDF (AGPL — read NOTICE)
pip install 'knovas-extract[docx,msg,rtf]'       # + DOCX/MSG/RTF
pip install 'knovas-extract[all]'                # everything

License-sensitive embedders: install knovas-extract[minimal] for the permissive-only subset (no AGPL / GPL deps). Calling extract() on a format that needs an unavailable backend raises DependencyMissingError with the exact pip install command to fix it.

Quickstart

from knovas_extract import extract

result = extract("report.pdf")

print(result.content.text)            # full extracted text, canonicalized
print(result.metadata.title)          # e.g. "Q4 Earnings"
print(result.metadata.page_count)     # e.g. 12
for page in result.content.pages:     # per-page text (when paginated)
    print(page.index, page.text[:80])
print(result.warnings)                # e.g. ["page 7: unrecognized font"]

Bytes work too:

data = open("report.pdf", "rb").read()
result = extract(data, mime="application/pdf")

ExtractionResult.to_dict() round-trips through the spec's JSON Schema; pass it directly to anything expecting the contract shape.

Resource limits

Every extraction is bounded. Override the defaults per call:

from knovas_extract import extract, Limits

result = extract(
    "huge.docx",
    limits=Limits(
        max_input_bytes=50 * 1024 * 1024,    # 50 MiB cap
        max_pages=1_000,
        max_decompression_ratio=50,
        max_text_bytes=10 * 1024 * 1024,
    ),
)

When a limit is crossed, you get a ResourceExhaustedError with .what / .limit / .observed attributes. Defaults (in Limits()) are conservative; tune them with your throughput budget in mind.

Errors

Every call either returns an ExtractionResult or raises a subclass of ExtractError:

Exception When
UnsupportedFormatError MIME not registered (you can register a custom extractor via knovas_extract.dispatch.MIME_REGISTRY).
CorruptDocumentError Bytes couldn't be parsed as the claimed format.
EncryptedDocumentError Password-protected document, no password supplied.
ResourceExhaustedError A Limits threshold was crossed.
DependencyMissingError An optional extra isn't installed — exception tells you the exact install command.

No bare exceptions, no None, no Optional[ExtractionResult].

Security promises (enforced by CI)

  • Never makes a network call. Asserted across every test via pytest-socket.
  • Never executes embedded code. PDF JavaScript, DOCX macros, RTF object linking — all stripped, warning emitted.
  • Never writes outside an explicit tmpdir. ZIP-slip paths are rejected.
  • XML parsing is XXE-hardened. All XML goes through defusedxml.
  • Releases are Sigstore-signed + ship SLSA L3 provenance. Verify before installing in production — see RELEASING.md.

For untrusted inputs, run inside a sandbox. Copy-paste recipes for nsjail, bubblewrap, and rootless Docker in docs/sandboxing.md.

Spec conformance

This implementation conforms to spec_version = 1.0.0 of knovas/KnowledgeBase/clients/extraction/spec. The pinned spec sha is recorded in tests/spec/ (Git submodule). Every release runs the spec's golden corpus + adversarial corpus before tagging.

To run the golden tests locally against a sibling KnowledgeBase checkout:

export KNOVAS_EXTRACT_SPEC_DIR=/path/to/KnowledgeBase/clients/extraction/spec
hatch -e golden run run

Development

hatch env create                  # one-time
hatch run test                    # unit tests
hatch -e golden run run           # corpus contract tests
hatch -e property run run         # hypothesis robustness
hatch -e bench run run            # benchmarks
hatch -e lint run all             # ruff + mypy + pyright
hatch -e sec run all              # bandit + pip-audit

CI runs the full matrix (3 Pythons × 3 OSes × every gate) on every PR.

Reporting vulnerabilities

See SECURITY.md. Please do not open public issues for security reports.

License

Apache-2.0 for knovas-extract itself. Several optional extras pull AGPL / GPL libraries — see NOTICE for the full third-party license inventory.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

knovas_extract-0.1.1.tar.gz (52.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

knovas_extract-0.1.1-py3-none-any.whl (41.9 kB view details)

Uploaded Python 3

File details

Details for the file knovas_extract-0.1.1.tar.gz.

File metadata

  • Download URL: knovas_extract-0.1.1.tar.gz
  • Upload date:
  • Size: 52.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for knovas_extract-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c69c4d8b729bb56d0a68a34c48430e30acdc1840b2c7359baef9050ee1df32b9
MD5 cf24e07833d6245956d443e46e64d4f2
BLAKE2b-256 137948b74b91c1c27e2da3da9499243b3b1e6df7639cc75620a42028c0df58e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for knovas_extract-0.1.1.tar.gz:

Publisher: release.yml on Seifeddini/knovas-extract-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file knovas_extract-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: knovas_extract-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 41.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for knovas_extract-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a118962b9c92ecb91fde573a7e33b4af1dbfe9a4bb1feb1d211ff156851051e9
MD5 f347b5e77bf36ddd3cbcabe8ace415c6
BLAKE2b-256 b3579068e71c284c0f19dbb5a6741ad891277a6be99510f1d72356e29e1ef6d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for knovas_extract-0.1.1-py3-none-any.whl:

Publisher: release.yml on Seifeddini/knovas-extract-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page