File size: 8,917 Bytes
b0ed872 0ee11bd b0ed872 0ee11bd b275ff7 b0ed872 0ee11bd b275ff7 b0ed872 0ee11bd b275ff7 0ee11bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | ---
language:
- en
license: mit
library_name: anthropic
tags:
- pdf
- document-parsing
- ocr
- multimodal
- equations
- table-extraction
- agent
- claude
- information-extraction
- scientific-documents
pipeline_tag: document-question-answering
model_name: PDF Atomic Parser
authors:
- algorembrant
sdk: other
sdk_version: "1.0.0"
app_file: pdf_atomic_parser.py
short_description: >
Atomically parse complex PDFs (equations, graphs, algorithms, tables)
using Claude claude-opus-4-6 without hallucination. Agent-ready.
---
# PDF Atomic Parser






Atomically parse and understand complex PDF documents using **claude-opus-4-6** (Anthropic).
Handles equations, graphs, algorithms, unique drawings, multi-column layouts, scanned pages,
and 100+ page documents without hallucination.
Designed to be dropped into local agent pipelines as a callable module.
## What Makes This Work
Claude processes PDFs natively through Anthropic's document API. Each page is sent as a
base64-encoded PDF chunk (or rendered at 300 DPI in image mode) alongside a structured
JSON extraction prompt. The model simultaneously sees:
- The rasterized visual content (charts, graphs, drawings, handwriting)
- The underlying text layer (searchable text, equations, captions)
This dual perception eliminates the need for separate OCR, layout parsers, or equation
recognizers. The model returns fully structured JSON containing LaTeX equations, Markdown
tables, verbatim algorithm code, and semantic figure descriptions per page.
---
## Features
| Feature | Description |
|---|---|
| Native PDF API | Sends PDF bytes directly; Claude sees both text and visuals |
| Image mode | Renders pages at 300 DPI via PyMuPDF for maximum fidelity |
| LaTeX equations | Every equation extracted as proper LaTeX |
| Table extraction | Tables as Markdown and list-of-dicts JSON |
| Algorithm extraction | Pseudocode and code blocks verbatim with language detection |
| Figure description | Semantic descriptions of charts, plots, diagrams, drawings |
| SQLite caching | Pages are cached; re-runs skip already-parsed pages |
| Chunked processing | Handles 100+ page documents by splitting into chunks |
| Multiple output formats | JSON, Markdown, plain text |
| Agent interface | `AgentPDFInterface` class for programmatic use |
| Batch processing | Process entire directories of PDFs |
---
## Requirements
- Python 3.10 or higher
- An Anthropic API key with access to `claude-opus-4-6`
- No GPU required; all inference runs through the Anthropic API
### External System Dependencies
PyMuPDF (installed via pip) requires no external system libraries on most platforms.
On some Linux systems you may need:
```bash
sudo apt-get install -y libmupdf-dev
```
On macOS:
```bash
brew install mupdf
```
On Windows: PyMuPDF ships with pre-built wheels on PyPI; no additional steps needed.
---
## Installation
```bash
git clone https://github.com/algorembrant/pdf-atomic-parser.git
cd pdf-atomic-parser
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
pip install -r requirements.txt
```
Set your API key:
```bash
export ANTHROPIC_API_KEY="sk-ant-..." # Linux / macOS
set ANTHROPIC_API_KEY=sk-ant-... # Windows CMD
$env:ANTHROPIC_API_KEY="sk-ant-..." # Windows PowerShell
```
---
## Quick Start
### Parse a PDF
```bash
python pdf_atomic_parser.py parse document.pdf
```
Outputs `document_parsed.json` in the current directory.
### Full Atomic Extraction (JSON + Markdown + Text)
```bash
python pdf_atomic_parser.py atomic document.pdf --output ./results/
```
### Ask a Question
```bash
python pdf_atomic_parser.py query document.pdf "What is the main loss function?"
```
### Extract Only Equations
```bash
python pdf_atomic_parser.py extract-equations document.pdf
```
### Use in an Agent Pipeline
```python
from pdf_atomic_parser import AgentPDFInterface
agent = AgentPDFInterface(model="opus")
# Full structured parse
result = agent.parse("paper.pdf")
# Just equations as list of dicts
equations = agent.get_equations("paper.pdf")
for eq in equations:
print(f"Page {eq['page']}: {eq['latex']}")
# Just tables
tables = agent.get_tables("paper.pdf")
# Semantic query
answer = agent.ask("paper.pdf", "What datasets were used for evaluation?")
print(answer)
```
---
## Usage Reference
### Command Overview
| Command | Purpose |
|---|---|
| `parse <pdf>` | Parse entire PDF to JSON/Markdown/text |
| `atomic <pdf>` | Full extraction to output directory (all formats) |
| `extract-equations <pdf>` | Extract LaTeX equations only |
| `extract-tables <pdf>` | Extract tables only |
| `extract-algorithms <pdf>` | Extract algorithms and code blocks only |
| `extract-figures <pdf>` | Extract figure descriptions only |
| `query <pdf> "<question>"` | Semantic question-answering over document |
| `batch <dir>` | Batch process all PDFs in a directory |
| `estimate <pdf>` | Estimate token count and cost before parsing |
| `cache-stats` | Show SQLite cache statistics |
| `list-cache` | List all cached documents |
| `clear-cache <pdf>` | Clear cached pages for a document |
### Global Options
| Option | Default | Description |
|---|---|---|
| `--model` | `opus` | `opus`, `sonnet`, `haiku`, or full model string |
| `--mode` | `native` | `native` (PDF bytes) or `image` (300 DPI PNG per page) |
| `--chunk-size` | `20` | Number of pages per API call |
| `--verbose` | off | Enable debug logging |
### parse / atomic Options
| Option | Default | Description |
|---|---|---|
| `--output / -o` | auto | Output file or directory path |
| `--format / -f` | `json` | `json`, `markdown`, or `text` |
| `--pages` | all | Page range, e.g. `1-50` |
---
## Output Schema
Each parsed document returns a `DocumentResult` with:
- `title`, `authors`, `abstract`, `document_summary`
- `page_results`: list of `PageResult` per page
Each `PageResult` contains:
```json
{
"page_number": 3,
"raw_text": "Full verbatim text...",
"summary": "This page describes...",
"section_headers": ["Introduction", "Related Work"],
"keywords": ["transformer", "attention", "BERT"],
"equations": [
{
"index": 0,
"latex": "\\mathcal{L} = -\\sum_{i} y_i \\log \\hat{y}_i",
"description": "Cross-entropy loss function",
"inline": false
}
],
"tables": [
{
"index": 0,
"markdown": "| Model | Accuracy |\n|---|---|\n| BERT | 94.2 |",
"json_data": [{"Model": "BERT", "Accuracy": "94.2"}],
"caption": "Table 1: Benchmark results"
}
],
"algorithms": [
{
"index": 0,
"name": "Algorithm 1: Backpropagation",
"language": "pseudocode",
"code": "for each layer l from L to 1:\n ...",
"description": "Gradient descent update rule"
}
],
"figures": [
{
"index": 0,
"figure_type": "line_chart",
"description": "Training loss over 100 epochs...",
"data_summary": "Y-axis: loss 0-2.0, X-axis: epoch 0-100...",
"caption": "Figure 2: Training curves"
}
]
}
```
---
## Choosing a Mode
| Scenario | Recommended Mode | Reason |
|---|---|---|
| Standard digital PDF | `native` (default) | Fastest, uses both text and visual layers |
| Scanned / photographed PDF | `image` | Text layer absent; vision handles everything |
| PDF with complex math | `image` | 300 DPI render ensures equation clarity |
| Very large file (>32 MB) | `image` | Native API has 32 MB size limit per chunk |
| Cost-sensitive workflow | `native` | Fewer tokens consumed |
---
## Cost Estimate
Rough estimates per 100-page academic paper:
| Model | Est. Tokens | Est. Cost |
|---|---|---|
| claude-opus-4-6 | ~120,000 | ~$3.50 |
| claude-sonnet-4-6 | ~120,000 | ~$0.60 |
| claude-haiku-4-5 | ~120,000 | ~$0.10 |
Use `python pdf_atomic_parser.py estimate document.pdf` for a per-document estimate.
---
## Caching
Parsed pages are stored in `~/.cache/pdf_atomic_parser/.pdf_parser_cache.db`.
Re-running on the same document skips already-parsed pages automatically.
The cache key is `(document_SHA256, page_number, model, mode)`.
---
## Project Structure
```
pdf-atomic-parser/
pdf_atomic_parser.py Main tool (single file, no splitting needed)
requirements.txt Python dependencies
README.md This file
model_card.yml Hugging Face model card
.gitignore
.gitattributes
```
---
## Author
**algorembrant**
---
## License
MIT License. See LICENSE file.
|