kaos-pdf 0.1.0a1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- kaos_pdf-0.1.0a1/.gitignore +17 -0
- kaos_pdf-0.1.0a1/CHANGELOG.md +70 -0
- kaos_pdf-0.1.0a1/LICENSE +201 -0
- kaos_pdf-0.1.0a1/NOTICE +8 -0
- kaos_pdf-0.1.0a1/PKG-INFO +273 -0
- kaos_pdf-0.1.0a1/README.md +235 -0
- kaos_pdf-0.1.0a1/kaos_pdf/__init__.py +93 -0
- kaos_pdf-0.1.0a1/kaos_pdf/__main__.py +5 -0
- kaos_pdf-0.1.0a1/kaos_pdf/_version.py +1 -0
- kaos_pdf-0.1.0a1/kaos_pdf/cli.py +471 -0
- kaos_pdf-0.1.0a1/kaos_pdf/errors.py +25 -0
- kaos_pdf-0.1.0a1/kaos_pdf/extract.py +2826 -0
- kaos_pdf-0.1.0a1/kaos_pdf/model.py +83 -0
- kaos_pdf-0.1.0a1/kaos_pdf/ocr/__init__.py +55 -0
- kaos_pdf-0.1.0a1/kaos_pdf/ocr/base.py +133 -0
- kaos_pdf-0.1.0a1/kaos_pdf/ocr/tesseract.py +209 -0
- kaos_pdf-0.1.0a1/kaos_pdf/py.typed +0 -0
- kaos_pdf-0.1.0a1/kaos_pdf/serve.py +67 -0
- kaos_pdf-0.1.0a1/kaos_pdf/tables/__init__.py +49 -0
- kaos_pdf-0.1.0a1/kaos_pdf/tables/base.py +130 -0
- kaos_pdf-0.1.0a1/kaos_pdf/tables/pdfplumber.py +214 -0
- kaos_pdf-0.1.0a1/kaos_pdf/tools.py +598 -0
- kaos_pdf-0.1.0a1/pyproject.toml +168 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.pyc
|
|
3
|
+
*.pyo
|
|
4
|
+
.venv/
|
|
5
|
+
.pytest_cache/
|
|
6
|
+
.ruff_cache/
|
|
7
|
+
.mypy_cache/
|
|
8
|
+
.coverage
|
|
9
|
+
*.egg-info/
|
|
10
|
+
dist/
|
|
11
|
+
build/
|
|
12
|
+
htmlcov/
|
|
13
|
+
coverage.xml
|
|
14
|
+
# uv.lock is gitignored at v0.1.0a1 because the [mcp] optional extra
|
|
15
|
+
# and the kaos-mcp dev dep reference a sibling package not yet on
|
|
16
|
+
# PyPI; uv lock can't resolve them. Re-track when kaos-mcp ships.
|
|
17
|
+
uv.lock
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `kaos-pdf` are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0a1] — 2026-05-07
|
|
11
|
+
|
|
12
|
+
First public alpha. Apache-2.0. Earlier internal versions were proprietary.
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
|
|
16
|
+
- **PDF extraction pipeline** built on `pypdfium2` (Apache-2.0): produces
|
|
17
|
+
a `kaos-content` `ContentDocument` AST with provenance (source URI,
|
|
18
|
+
1-based page, bounding box, extractor name, confidence) on every node.
|
|
19
|
+
PDFium calls are serialised through a global lock so the library is
|
|
20
|
+
safe under threaded executors. See `docs/THREAD_SAFETY.md`.
|
|
21
|
+
- **Public Python API** — `extract_pdf`, `extract_pdf_bytes`,
|
|
22
|
+
`extract_pdf_with_tables`, `extract_page_text`, `render_page`,
|
|
23
|
+
`get_page_count`, `get_pdf_metadata`, `get_pdf_outline`,
|
|
24
|
+
`classify_document`, `classify_page`, plus the re-exported
|
|
25
|
+
`search_document` / `SearchResult` / `SearchResults` from
|
|
26
|
+
`kaos-content`. `kaos_pdf.__all__` enumerates the surface.
|
|
27
|
+
- **Typed result models** — `PdfMetadata` and `PdfOutlineEntry`
|
|
28
|
+
`@dataclass(frozen=True, slots=True)` types. Sparse `to_dict()`
|
|
29
|
+
(None fields omitted) preserves the wire format for JSON consumers.
|
|
30
|
+
`page_count` is carried directly on `PdfMetadata`.
|
|
31
|
+
- **Seven MCP tools** registered via `register_pdf_tools(runtime)`
|
|
32
|
+
(requires `kaos-mcp` from source until that package ships on PyPI):
|
|
33
|
+
`kaos-pdf-extract-parse`, `kaos-pdf-extract-page-text`,
|
|
34
|
+
`kaos-pdf-render-page`, `kaos-pdf-metadata`,
|
|
35
|
+
`kaos-pdf-search-document`, `kaos-pdf-get-outline`,
|
|
36
|
+
`kaos-pdf-classify-page`. All seven are read-only / idempotent /
|
|
37
|
+
non-destructive / non-open-world. Every `ToolResult.create_error()`
|
|
38
|
+
call site returns a three-part recovery hint (what / how to fix /
|
|
39
|
+
alternative tool) for LLM self-correction.
|
|
40
|
+
- **Optional extras**:
|
|
41
|
+
- `[ocr]` — `pytesseract` engine for scanned PDFs (requires the
|
|
42
|
+
system `tesseract` binary). `OCRMode` is the `extract_pdf(ocr=...)`
|
|
43
|
+
setting; `OCREngine` is the engine ABC; `TesseractEngine` is the
|
|
44
|
+
Apache-2.0 default. OCR paragraphs carry `Provenance.confidence`.
|
|
45
|
+
- `[tables]` — `pdfplumber` engine (MIT, pure Python — no Java, no
|
|
46
|
+
GPU) for borderless and multi-line tables. Extracted tables become
|
|
47
|
+
`TabularDocument` with typed columns; live in the body with
|
|
48
|
+
`Provenance.extractor = "kaos-pdf/tables/{engine}"`.
|
|
49
|
+
- `[nlp]` — `kaos-nlp-core` for BM25 sentence-level search via
|
|
50
|
+
`search_document(..., level="sentence")`.
|
|
51
|
+
- **Two CLI entry points** — `kaos-pdf` (admin) and `kaos-pdf-serve`
|
|
52
|
+
(MCP server, stdio + streamable HTTP). Every structured subcommand
|
|
53
|
+
supports `--json` for machine-readable output. CLI uses 1-based page
|
|
54
|
+
numbers; the Python API is 0-based.
|
|
55
|
+
- **Errors** — `KaosPdfError` base + `PdfNotFoundError`,
|
|
56
|
+
`PdfExtractionError`, `PdfRenderError`. Tools translate these into
|
|
57
|
+
`ToolResult.create_error()`.
|
|
58
|
+
|
|
59
|
+
### Notes
|
|
60
|
+
|
|
61
|
+
- VLM page programs (describe / classify / VLM-OCR) live in
|
|
62
|
+
`kaos-llm-core[vision]` ≥ 0.1.0a3, not in `kaos-pdf`. The split
|
|
63
|
+
keeps the extraction → LLM dependency direction one-directional.
|
|
64
|
+
- `kaos-pdf` does not and will not depend on AGPL or GPL libraries.
|
|
65
|
+
This rules out Surya for OCR and camelot-lattice / Tabula for
|
|
66
|
+
tables.
|
|
67
|
+
- The `[mcp]` extra is intentionally not declared at 0.1.0a1 because
|
|
68
|
+
`kaos-mcp` is not yet on PyPI; `uv lock` refuses to resolve any
|
|
69
|
+
declared extra whose package is unresolvable. The extra returns
|
|
70
|
+
once `kaos-mcp` ships.
|
kaos_pdf-0.1.0a1/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 273 Ventures LLC
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
kaos_pdf-0.1.0a1/NOTICE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
kaos-pdf
|
|
2
|
+
Copyright 2026 273 Ventures LLC.
|
|
3
|
+
|
|
4
|
+
This product includes software developed at 273 Ventures LLC
|
|
5
|
+
(https://273ventures.com).
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0. See the LICENSE file
|
|
8
|
+
distributed with this software for the full text of the license.
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: kaos-pdf
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: PDF extraction and document processing for KAOS — structured AST output with provenance
|
|
5
|
+
Project-URL: Homepage, https://kelvin.legal
|
|
6
|
+
Project-URL: Documentation, https://docs.kelvin.legal
|
|
7
|
+
Project-URL: Repository, https://github.com/273v/kaos-pdf
|
|
8
|
+
Project-URL: Issues, https://github.com/273v/kaos-pdf/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/273v/kaos-pdf/blob/main/CHANGELOG.md
|
|
10
|
+
Author-email: 273 Ventures LLC <it@273ventures.com>
|
|
11
|
+
Maintainer-email: Michael Bommarito <mike@273ventures.com>
|
|
12
|
+
License-Expression: Apache-2.0
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
License-File: NOTICE
|
|
15
|
+
Keywords: extraction,kaos,mcp,ocr,pdf,pypdfium2,tables
|
|
16
|
+
Classifier: Development Status :: 3 - Alpha
|
|
17
|
+
Classifier: Intended Audience :: Developers
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
23
|
+
Classifier: Topic :: Office/Business
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Classifier: Topic :: Text Processing
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Python: >=3.13
|
|
28
|
+
Requires-Dist: kaos-content[images,layout,markdown]<0.2,>=0.1.0a1
|
|
29
|
+
Requires-Dist: kaos-core<0.2,>=0.1.0a3
|
|
30
|
+
Requires-Dist: pypdfium2>=4.30.0
|
|
31
|
+
Provides-Extra: nlp
|
|
32
|
+
Requires-Dist: kaos-nlp-core<0.2,>=0.1.0a2; extra == 'nlp'
|
|
33
|
+
Provides-Extra: ocr
|
|
34
|
+
Requires-Dist: pytesseract>=0.3.10; extra == 'ocr'
|
|
35
|
+
Provides-Extra: tables
|
|
36
|
+
Requires-Dist: pdfplumber>=0.11.0; extra == 'tables'
|
|
37
|
+
Description-Content-Type: text/markdown
|
|
38
|
+
|
|
39
|
+
# kaos-pdf
|
|
40
|
+
|
|
41
|
+
> **Part of [Kelvin Agentic OS](https://kelvin.legal) (KAOS)** — open agentic
|
|
42
|
+
> infrastructure for legal work, built by
|
|
43
|
+
> [273 Ventures](https://273ventures.com).
|
|
44
|
+
> See the [full KAOS package map](https://github.com/273v) for the rest of the stack.
|
|
45
|
+
|
|
46
|
+
[](https://pypi.org/project/kaos-pdf/)
|
|
47
|
+
[](https://pypi.org/project/kaos-pdf/)
|
|
48
|
+
[](https://github.com/273v/kaos-pdf/blob/main/LICENSE)
|
|
49
|
+
[](https://github.com/273v/kaos-pdf/actions/workflows/ci.yml)
|
|
50
|
+
|
|
51
|
+
`kaos-pdf` is the PDF-extraction layer of KAOS — it turns a PDF byte stream
|
|
52
|
+
into a typed [`kaos-content`](https://github.com/273v/kaos-content)
|
|
53
|
+
`ContentDocument` AST with provenance (page numbers, bounding boxes,
|
|
54
|
+
extraction confidence) on every node, plus a small set of read-only MCP
|
|
55
|
+
tools for agentic workflows. The engine is
|
|
56
|
+
[`pypdfium2`](https://github.com/pypdfium2-team/pypdfium2) (Apache-2.0)
|
|
57
|
+
and all PDFium calls are serialised through a global lock so the library
|
|
58
|
+
is safe to call from threaded executors. No raw text strings escape — every
|
|
59
|
+
result is an AST node, a typed dataclass, or a `KaosImage`.
|
|
60
|
+
|
|
61
|
+
The base install is intentionally small: three runtime dependencies
|
|
62
|
+
(`kaos-content[images,layout,markdown]`, `kaos-core`, `pypdfium2`) and no
|
|
63
|
+
compiled native code beyond the PDFium wheel. Heavier capabilities are
|
|
64
|
+
opt-in extras: `[ocr]` adds `pytesseract` for scanned pages (and requires
|
|
65
|
+
a system `tesseract` binary), `[tables]` adds `pdfplumber` (MIT, pure
|
|
66
|
+
Python — no Java, no GPU) for borderless and multi-line tables, and
|
|
67
|
+
`[nlp]` adds `kaos-nlp-core` for BM25 sentence-level search. VLM page
|
|
68
|
+
programs (describe / classify / OCR-via-VLM) live in
|
|
69
|
+
`kaos-llm-core[vision]` ≥ 0.1.0a3 — they were moved out of `kaos-pdf` to
|
|
70
|
+
keep the extraction → LLM dependency direction one-directional. We do not
|
|
71
|
+
and will not depend on AGPL or GPL libraries (this rules out Surya for
|
|
72
|
+
OCR and camelot-lattice / Tabula for tables).
|
|
73
|
+
|
|
74
|
+
## Install
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
uv add kaos-pdf
|
|
78
|
+
# or
|
|
79
|
+
pip install kaos-pdf
|
|
80
|
+
|
|
81
|
+
# OCR for scanned PDFs (requires system tesseract binary)
|
|
82
|
+
uv add 'kaos-pdf[ocr]'
|
|
83
|
+
|
|
84
|
+
# Structured table extraction via pdfplumber
|
|
85
|
+
uv add 'kaos-pdf[tables]'
|
|
86
|
+
|
|
87
|
+
# BM25 sentence-level search via kaos-nlp-core
|
|
88
|
+
uv add 'kaos-pdf[nlp]'
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`kaos-pdf` requires Python **3.13** or newer (3.14 is supported). The
|
|
92
|
+
package is pure Python — the only native code is the PDFium wheel shipped
|
|
93
|
+
by `pypdfium2`, which has prebuilt wheels for Linux, macOS, and Windows
|
|
94
|
+
on x86_64 and arm64.
|
|
95
|
+
|
|
96
|
+
## Quick start
|
|
97
|
+
|
|
98
|
+
Extract a PDF into the document AST, render a page, and search for a term:
|
|
99
|
+
|
|
100
|
+
```python
|
|
101
|
+
from kaos_pdf import (
|
|
102
|
+
extract_pdf,
|
|
103
|
+
get_pdf_metadata,
|
|
104
|
+
get_pdf_outline,
|
|
105
|
+
render_page,
|
|
106
|
+
search_document,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
# Parse the whole document into a kaos-content ContentDocument
|
|
110
|
+
doc = extract_pdf("contract.pdf")
|
|
111
|
+
print(len(doc.body), "top-level blocks")
|
|
112
|
+
|
|
113
|
+
# Typed metadata (PdfMetadata dataclass; sparse to_dict() for JSON)
|
|
114
|
+
meta = get_pdf_metadata("contract.pdf")
|
|
115
|
+
print(meta.page_count, meta.title, meta.author)
|
|
116
|
+
|
|
117
|
+
# Outline / bookmarks (list[PdfOutlineEntry], also typed)
|
|
118
|
+
for entry in get_pdf_outline("contract.pdf"):
|
|
119
|
+
print(" " * entry.level, entry.title, "p", entry.page)
|
|
120
|
+
|
|
121
|
+
# Render the first page as a 300-DPI PIL image (returned as KaosImage)
|
|
122
|
+
image = render_page("contract.pdf", page_number=0, dpi=300)
|
|
123
|
+
image.pil.save("page-1.png")
|
|
124
|
+
|
|
125
|
+
# AST-grounded search — paragraph-level by default
|
|
126
|
+
hits = search_document(doc, "indemnification", top_k=5)
|
|
127
|
+
for hit in hits.results:
|
|
128
|
+
print(f"score={hit.score:.2f} :: {hit.text[:80]}")
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Every node in the returned `ContentDocument` carries a `Provenance`
|
|
132
|
+
(source path, 1-based page, bounding box, extractor name, confidence)
|
|
133
|
+
so downstream consumers — citation verifiers, redaction tooling,
|
|
134
|
+
labelers — can ground answers back to the original PDF.
|
|
135
|
+
|
|
136
|
+
## Concepts
|
|
137
|
+
|
|
138
|
+
The package is a thin, typed surface over `pypdfium2`. The most important
|
|
139
|
+
entries:
|
|
140
|
+
|
|
141
|
+
| Concept | What it is |
|
|
142
|
+
|---|---|
|
|
143
|
+
| **`extract_pdf(path, *, pages=None, ocr="never", tables="geometric", extract_images=False, image_src_builder=...)`** | Primary entry point. Returns a `ContentDocument`. `pages` selects 0-based indices; `ocr` is `"never"` / `"auto"` / `"always"`; `tables` is `"geometric"` / `"engine"` / `"disabled"`; `image_src_builder` lets callers control the image URI policy (default inlines as `data:` URLs). |
|
|
144
|
+
| **`extract_pdf_bytes(data, ...)` / `extract_pdf_with_tables(path, ...)`** | Bytes-input variant and the sidecar form that returns `(ContentDocument, TabularDocument)` when you want tables out of the body. |
|
|
145
|
+
| **`render_page(path, page_number, *, dpi=300, grayscale=False)`** | Renders a single page (0-based) to a `KaosImage` (PIL + DPI + provenance). |
|
|
146
|
+
| **`extract_page_text(path, page_number)` / `get_page_count(path)`** | Lightweight per-page text + page-count helpers that skip full AST construction. |
|
|
147
|
+
| **`PdfMetadata` / `PdfOutlineEntry`** | `@dataclass(frozen=True, slots=True)` result types returned by `get_pdf_metadata()` and `get_pdf_outline()`. Sparse `to_dict()` (None fields omitted) preserves the historical wire format. `page_count` lives on `PdfMetadata` directly — no extra `get_page_count()` call needed. |
|
|
148
|
+
| **`classify_document(path)` / `classify_page(path, page_number)`** | Lightweight document/page-type heuristics (e.g. `text`, `scanned`, `mixed`). |
|
|
149
|
+
| **`search_document(doc, query, *, top_k=10, level="paragraph")`** | Re-exported from `kaos-content`. AST-grounded ranked search returning `SearchResults` with `total_matches` / `has_more` for pagination. `level="sentence"` requires the `[nlp]` extra. |
|
|
150
|
+
| **`OCRMode` / `OCREngine` / `TesseractEngine`** | OCR pluggability. `OCRMode` is the `extract_pdf(ocr=...)` setting; `OCREngine` is the engine ABC; `TesseractEngine` is the Apache-2.0 default (install with `[ocr]` + system tesseract). OCR paragraphs carry `Provenance.confidence` so verifiers can weight them. |
|
|
151
|
+
| **`TableMode` / `TableEngine` / `ExtractedTable` / `TableResult`** | Table pluggability. `pdfplumber` is the MIT default behind `[tables]`. Extracted tables become `TabularDocument` with typed columns and live in the body with `Provenance.extractor = "kaos-pdf/tables/{engine}"`. |
|
|
152
|
+
| **`ParsePDFTool`, `GetPageTextTool`, `RenderPageTool`, `PDFMetadataTool`, `SearchDocumentTool`, `GetOutlineTool`, `ClassifyPageTool`** | The seven `KaosTool` subclasses exposed over MCP as `kaos-pdf-extract-parse`, `-extract-page-text`, `-render-page`, `-metadata`, `-search-document`, `-get-outline`, `-classify-page`. All seven are `readOnly`, `idempotent`, non-destructive, non-open-world. Register with `register_pdf_tools(runtime)`. |
|
|
153
|
+
| **Errors (`KaosPdfError`, `PdfNotFoundError`, `PdfExtractionError`, `PdfRenderError`)** | Dedicated exception hierarchy. MCP tools translate these into `ToolResult.create_error()` with the documented three-part recovery hint (what / how to fix / alternative tool). |
|
|
154
|
+
|
|
155
|
+
## CLI
|
|
156
|
+
|
|
157
|
+
`kaos-pdf` ships two entry-point scripts. Every structured command on
|
|
158
|
+
the admin CLI supports `--json` for machine-readable output piped to
|
|
159
|
+
other agents:
|
|
160
|
+
|
|
161
|
+
```bash
|
|
162
|
+
kaos-pdf --help # admin CLI
|
|
163
|
+
kaos-pdf-serve --help # MCP server
|
|
164
|
+
|
|
165
|
+
kaos-pdf info contract.pdf --json # metadata + page count + classification
|
|
166
|
+
kaos-pdf outline contract.pdf --json # PDF bookmarks (falls back to detected headings)
|
|
167
|
+
kaos-pdf page contract.pdf 3 --json # plain text from a single page (1-based)
|
|
168
|
+
kaos-pdf extract contract.pdf -f markdown -p 1-5 # full AST → markdown / text / json / html
|
|
169
|
+
kaos-pdf render contract.pdf 1 --dpi 300 -o p1.png # render a page as PNG
|
|
170
|
+
kaos-pdf classify contract.pdf --page 1 --json # document- or page-level type
|
|
171
|
+
kaos-pdf search contract.pdf "indemnification" -k 5 # AST-grounded ranked search
|
|
172
|
+
|
|
173
|
+
kaos-pdf-serve # stdio (Claude Code / Desktop)
|
|
174
|
+
kaos-pdf-serve --http --port 8000 # streamable HTTP
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
The admin CLI uses 1-based page numbers (consistent with how the file
|
|
178
|
+
opens in any PDF viewer) and translates internally to the 0-based indices
|
|
179
|
+
the Python API uses. `kaos-pdf-serve` exposes the seven MCP tools listed
|
|
180
|
+
in **Concepts** above.
|
|
181
|
+
|
|
182
|
+
## Compatibility & status
|
|
183
|
+
|
|
184
|
+
| Aspect | |
|
|
185
|
+
|---|---|
|
|
186
|
+
| **Python** | 3.13, 3.14 |
|
|
187
|
+
| **OS** | Linux, macOS, Windows (pure-Python wheel; the only native code is the PDFium wheel shipped by `pypdfium2`) |
|
|
188
|
+
| **Maturity** | Alpha (`Development Status :: 3 - Alpha`). The public API is documented in `kaos_pdf.__all__`. |
|
|
189
|
+
| **Stability policy** | Pre-1.0: minor bumps may change behaviour. Every change is documented in [`CHANGELOG.md`](CHANGELOG.md). The MCP tool surface (`kaos-pdf-*` names) and the `KAOS_PDF_*` environment-variable namespace are public API and follow the same policy. |
|
|
190
|
+
| **Test coverage** | 340 unit tests plus a small integration tier hitting the MCP wire end-to-end. Bounded unit gate (`pytest tests/unit -q --no-cov`) finishes in ~35s. |
|
|
191
|
+
| **Type checker** | Validated with [`ty`](https://docs.astral.sh/ty/), Astral's Python type checker. |
|
|
192
|
+
|
|
193
|
+
## Companion packages
|
|
194
|
+
|
|
195
|
+
`kaos-pdf` is one of the packages in the
|
|
196
|
+
[Kelvin Agentic OS](https://kelvin.legal). The broader stack:
|
|
197
|
+
|
|
198
|
+
| Package | Layer | What it does |
|
|
199
|
+
|---|---|---|
|
|
200
|
+
| [`kaos-core`](https://github.com/273v/kaos-core) | Core | Foundational runtime, MCP-native types, registries, execution engine, VFS |
|
|
201
|
+
| [`kaos-content`](https://github.com/273v/kaos-content) | Core | Typed document AST: Block/Inline, provenance, views |
|
|
202
|
+
| [`kaos-mcp`](https://github.com/273v/kaos-mcp) | Bridge | FastMCP server, `kaos` management CLI, MCP resource templates |
|
|
203
|
+
| [`kaos-pdf`](https://github.com/273v/kaos-pdf) | Extraction | PDF → AST with provenance |
|
|
204
|
+
| [`kaos-web`](https://github.com/273v/kaos-web) | Extraction | Web extraction, browser automation, search, domain intelligence |
|
|
205
|
+
| [`kaos-office`](https://github.com/273v/kaos-office) | Extraction | DOCX / PPTX / XLSX readers + writers to AST |
|
|
206
|
+
| [`kaos-tabular`](https://github.com/273v/kaos-tabular) | Extraction | DuckDB-powered SQL analytics |
|
|
207
|
+
| [`kaos-source`](https://github.com/273v/kaos-source) | Data | Government + financial data connectors (Federal Register, eCFR, EDGAR, GovInfo, PACER, GLEIF) |
|
|
208
|
+
| [`kaos-llm-client`](https://github.com/273v/kaos-llm-client) | LLM | Multi-provider LLM transport |
|
|
209
|
+
| [`kaos-llm-core`](https://github.com/273v/kaos-llm-core) | LLM | Typed LLM programming (Signatures, Programs, Optimizers) |
|
|
210
|
+
| [`kaos-nlp-core`](https://github.com/273v/kaos-nlp-core) | Primitives (Rust) | High-performance NLP primitives |
|
|
211
|
+
| [`kaos-nlp-transformers`](https://github.com/273v/kaos-nlp-transformers) | ML | Dense embeddings + retrieval |
|
|
212
|
+
| [`kaos-graph`](https://github.com/273v/kaos-graph) | Primitives (Rust) | Graph algorithms + RDF/SPARQL |
|
|
213
|
+
| [`kaos-ml-core`](https://github.com/273v/kaos-ml-core) | Primitives (Rust) | Classical ML on the document AST |
|
|
214
|
+
| [`kaos-citations`](https://github.com/273v/kaos-citations) | Legal | Legal citation extraction, resolution, verification |
|
|
215
|
+
| [`kaos-agents`](https://github.com/273v/kaos-agents) | Agentic | Agent runtime, memory, recipes |
|
|
216
|
+
| [`kaos-reference`](https://github.com/273v/kaos-reference) | Sample | Reference module for module authors |
|
|
217
|
+
|
|
218
|
+
Packages depend on `kaos-core`; everything else is opt-in. Mix and match the
|
|
219
|
+
ones you need.
|
|
220
|
+
|
|
221
|
+
## Development
|
|
222
|
+
|
|
223
|
+
```bash
|
|
224
|
+
git clone https://github.com/273v/kaos-pdf
|
|
225
|
+
cd kaos-pdf
|
|
226
|
+
uv sync --group dev
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Install pre-commit hooks (recommended — they run the same checks as CI on
|
|
230
|
+
every commit, scoped to staged files):
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
uvx pre-commit install
|
|
234
|
+
uvx pre-commit run --all-files # one-time full sweep
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Manual QA commands (the same set CI runs):
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
uv run ruff format --check kaos_pdf tests
|
|
241
|
+
uv run ruff check kaos_pdf tests
|
|
242
|
+
uv run ty check kaos_pdf tests
|
|
243
|
+
uv run pytest tests/unit -q --no-cov
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
## Build from source
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
uv build
|
|
250
|
+
uv pip install dist/*.whl
|
|
251
|
+
python -c "import kaos_pdf; print(kaos_pdf.__version__)" # smoke import
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## Contributing
|
|
255
|
+
|
|
256
|
+
Issues and pull requests are welcome. By contributing you certify the
|
|
257
|
+
[Developer Certificate of Origin v1.1](https://developercertificate.org/) —
|
|
258
|
+
sign every commit with `git commit -s`. Please open an issue before starting
|
|
259
|
+
on a non-trivial change so we can align on scope.
|
|
260
|
+
|
|
261
|
+
## Security
|
|
262
|
+
|
|
263
|
+
For security issues, **please do not file a public issue**. Report privately
|
|
264
|
+
via [GitHub Private Vulnerability Reporting](https://github.com/273v/kaos-pdf/security/advisories/new)
|
|
265
|
+
or email **security@273ventures.com**. See [SECURITY.md](SECURITY.md) for the
|
|
266
|
+
full disclosure policy.
|
|
267
|
+
|
|
268
|
+
## License
|
|
269
|
+
|
|
270
|
+
Apache License 2.0 — see [LICENSE](LICENSE) and [NOTICE](NOTICE).
|
|
271
|
+
|
|
272
|
+
Copyright 2026 [273 Ventures LLC](https://273ventures.com).
|
|
273
|
+
Built for [kelvin.legal](https://kelvin.legal).
|