Controlling Pipeline Depth with ProcessMode (DetectOnly, Analyze, Full) in pdf-inspector
Use pdf-inspector's ProcessMode enum to trade speed for depth: DetectOnly skips text extraction entirely, Analyze extracts text and layout metadata without Markdown generation, and Full runs the complete pipeline for production PDF-to-Markdown conversion.
The pdf-inspector library from Firecrawl provides three distinct processing levels through the ProcessMode enum. This design lets callers optimize for latency when they only need structural information, or enable full extraction when downstream systems require clean Markdown output.
Understanding ProcessMode Values
The ProcessMode enum is defined in src/process_mode.rs and controls how deeply the PDF processing pipeline executes:
/// Controls how far the PDF processing pipeline runs.
#[derive(Debug, Clone, Default, PartialEq)]
pub enum ProcessMode {
/// Only detect PDF type. Very fast — no text extraction.
DetectOnly,
/// Detect type + extract text + compute layout complexity. Skips markdown.
Analyze,
/// Full pipeline: detect, extract, convert to markdown (default).
#[default]
Full,
}
Each variant gates specific pipeline stages in process_pdf_with_options within src/lib.rs.
Runtime Behavior and Performance Characteristics
The library applies ProcessMode at three key decision points in the processing flow:
| Mode | Pipeline stages executed | Typical latency | Best for |
|---|---|---|---|
| DetectOnly | PDF type detection only (detect_pdf) |
~10 ms | Quick OCR necessity checks, filtering pipelines |
| Analyze | Detection + text extraction + layout heuristics (tables, columns) | ~30–50 ms | Structural metadata extraction, complexity analysis |
| Full | All stages + Markdown conversion via src/markdown/convert.rs |
~100–200 ms | LLM ingestion, documentation workflows |
The mode check in src/lib.rs implements early returns and conditional branching:
if options.mode == ProcessMode::DetectOnly {
// Only run detection; return early
}
// later…
let md = if options.mode == ProcessMode::Analyze {
// Skip markdown generation
} else {
// Full conversion
};
API and Usage Examples
Rust API
The PdfOptions::mode field stores your chosen ProcessMode, defaulting to Full if unspecified:
use pdf_inspector::{PdfOptions, ProcessMode, process_pdf_with_options};
let opts = PdfOptions::new()
.mode(ProcessMode::Analyze); // only detect + layout, no markdown
let result = process_pdf_with_options("sample.pdf", opts).unwrap();
println!("PDF type: {}", result.pdf_type);
println!("Pages with tables: {:?}", result.layout.pages_with_tables);
Python Bindings
The Python interface exposes the same enum values:
import pdf_inspector
# Fast detection only
info = pdf_inspector.detect_pdf("sample.pdf")
print(info.pdf_type)
# Analyze mode – get layout info without markdown
result = pdf_inspector.process_pdf("sample.pdf", mode=pdf_inspector.ProcessMode.Analyze)
print(result.layout.pages_with_tables)
Command-Line Interface
The pdf2md CLI in src/bin/pdf2md.rs maps flags to enum values:
# Only detect PDF type (~10ms)
pdf2md --detect-only sample.pdf
# Analyze mode – prints JSON with layout details (~30-50ms)
pdf2md --analyze --json sample.pdf
# Full conversion, default behavior (~100-200ms)
pdf2md sample.pdf
Where ProcessMode Is Configured
src/process_mode.rs— Enum definition and documentationsrc/lib.rs—PdfOptionsstruct holds the mode;process_pdf_with_optionsgates pipeline stagessrc/bin/pdf2md.rs— CLI flag parsing (--detect-only,--analyze) mapped to enum variantssrc/detector.rs— Fast type detection implementation used byDetectOnlysrc/markdown/convert.rs— Markdown generation skipped inDetectOnlyandAnalyzemodes
Extending ProcessMode
Adding custom pipeline depths requires three changes:
- Extend the enum in
src/process_mode.rswith a new variant - Add conditional logic in
process_pdf_with_optionsto gate relevant extraction steps - Update CLI flag handling in
src/bin/pdf2md.rsfor accessibility
Because PdfOptions stores the mode generically, existing callers automatically recognize new variants after recompilation.
Summary
- ProcessMode in
pdf-inspectorcontrols pipeline depth through three variants:DetectOnly,Analyze, andFull DetectOnlyruns onlydetect_pdffromsrc/detector.rsfor sub-10ms type identificationAnalyzeadds text extraction and layout analysis viasrc/lib.rswithout invokingsrc/markdown/convert.rsFullexecutes the complete pipeline including Markdown conversion, suitable for production LLM workflows- Configuration flows through
PdfOptions::modewith sensible defaults and CLI parity inpdf2md
Frequently Asked Questions
What is the default ProcessMode if I don't specify one?
The ProcessMode enum derives Default with Full as the default variant. When you construct PdfOptions::new() without calling .mode(), the pipeline runs complete detection, extraction, and Markdown conversion automatically.
Can I switch modes at runtime based on PDF characteristics?
Yes. Since ProcessMode is a plain enum passed to process_pdf_with_options, you can implement your own logic—perhaps using DetectOnly first to check if OCR is needed, then conditionally re-running with Full for complex documents.
How does ProcessMode affect memory usage?
DetectOnly allocates minimal memory since it never parses page content. Analyze holds extracted text and layout structures in memory. Full additionally buffers Markdown output, though all modes stream large PDFs rather than loading entirely into RAM.
Is ProcessMode available in the Python bindings?
Yes. The Python module exposes pdf_inspector.ProcessMode.DetectOnly, Analyze, and Full as enum-like constants. Pass them to process_pdf() via the mode keyword argument exactly as shown in the Python example above.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →