Skip to main content

Lumberjack logo: an axe, document, trees, and a tree ring

Lumberjack

PyPI Python CI

Turn long, structured documents into retrieval-ready chunks without losing their context.

Lumberjack is a Python library and CLI for preparing documentation, reports, and knowledge-base content for RAG. It reads a document's structure, keeps headings with the text they introduce, respects tables and code blocks, and splits only when a chunk exceeds its token budget.

中文说明

Why Lumberjack?

Plain-text splitters cut at character or token boundaries. That can detach an answer from its section title, split a table in the middle, or join unrelated topics merely because they happen to be adjacent.

Lumberjack works from a normalized document tree instead:

  • Keep useful context. Chunks carry their heading path and source metadata.
  • Honor document structure. Headings, paragraphs, lists, tables, and fenced code are handled as document blocks rather than as unstructured text.
  • Use the budget efficiently. The default splitter uses inexpensive running estimates while packing sections, then produces an authoritative final token count.
  • Choose the shape of a chunk. Pack sibling sections, retain whole subtrees, or split direct section bodies—without changing the input format.

Markdown, HTML, and DOCX are currently supported.

Install

pip install lumberjack-py

# Optional exact tokenizers, DOCX support, and the Web API
pip install "lumberjack-py[tokenizers,docx,web]"

Lumberjack requires Python 3.10 or newer.

Quick start

Pass document content, bytes, or a Path to Lumberjack.saw():

from pathlib import Path

from lumberjack import Lumberjack

jack = Lumberjack(max_tokens=1200)
result = jack.saw(Path("handbook.md"))

for chunk in result.chunks:
    print(chunk.own_heading, chunk.body, chunk.token_count)

print(result.document.metadata)

For in-memory content, pass a string directly. A plain string is always treated as content, never as a filesystem path:

result = Lumberjack(max_tokens=500).saw(
    "# Deployment\n\nDeploy the service with the approved release workflow."
)

Lumberjack.saw() returns a SplitResult containing the parsed DocTree and the final chunks. Each chunk is a Chunk dataclass whose body contains the rendered content; ancestor_headings and own_heading preserve the section context; token_count is the final count after output processing. Document title, source path, metadata, reference definitions, and line ranges are retained when available.

Use it from the command line

# Format is inferred from the file extension.
lumber handbook.md --max-tokens 1200

# Select a tokenizer or a splitter when needed.
lumber report.docx --input-format docx --tokenizer tiktoken --splitter subtree

# JSON chunks are written to standard output.
lumber page.html --input-format html --splitter exact-sibling

The CLI emits JSON, so it can feed an indexing or ingestion job directly.

Pick a splitting strategy

Strategy Best when you want
sibling (default) Well-filled chunks that pack adjacent sibling sections while retaining their shared context.
subtree A complete section subtree to stay together whenever it fits the budget.
section Each section's direct body to be handled independently.

The default strategies use incremental estimates for fast budget decisions. Use exact-sibling, exact-subtree, or exact-section when every split-time decision must recount the rendered candidate exactly. All chunks receive a final authoritative count.

See splitter strategies for practical strategy examples.

Web API

Install the web extra, then start the service:

lumberjack-serve --reload

It provides POST /lumber/api/split/text for pasted Markdown or HTML and POST /lumber/api/split/file for uploaded Markdown, HTML, and DOCX files. Both return the same serialized chunk shape as the Python API and CLI.

Customize when you need to

The defaults are intended to be usable as-is. For advanced pipelines, you can provide your own parser, tokenizer, splitter, normalizer, or transformer to Lumberjack(...). Built-in splitters accept typed block_options so tables, code fences, and custom block kinds can have their own isolation and budget policies. The public extension points live under lumberjack.parser, lumberjack.tokenizer, lumberjack.splitter, lumberjack.block, and lumberjack.protocols.

Development

uv sync --group dev --group test --extra tokenizers --extra docx --extra web
UV_CACHE_DIR=/tmp/uvcache uv run pytest

License

MIT

Release files for lumberjack-py 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lumberjack-py 0.4.0
File Size Uploaded
lumberjack_py-0.4.0.tar.gz 266.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lumberjack-py 0.4.0
File Interpreter ABI Platform
lumberjack_py-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size:337.5 kB

Release files / lumberjack_py-0.4.0.tar.gz

Download URL lumberjack_py-0.4.0.tar.gz
Size 266.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d9a7522f60ede207ed34a084a312c67f9cf9371acbbf971ae8fb661be673973a
BLAKE2b-256 checksum
How to use checksums
104b1ee7de6ed59a36309479b3fe89fd793fcc7b672b72d3d11e25baf29e6602
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 19, 2026.

Transparency log

Release files / lumberjack_py-0.4.0-py3-none-any.whl

Download URL lumberjack_py-0.4.0-py3-none-any.whl
Size 70.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
04cbbba2c8b008b64c9a4da85211a709c18dc3a6b1c954984abd5c60d103ac02
BLAKE2b-256 checksum
How to use checksums
fb76dc945986c5cf5eed9552e9c39c32498a887bdff1af46d34d7dace29c2516
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 19, 2026.

Transparency log

Release history Release notifications | RSS feed

0.5.0

2 release files

This release

0.4.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page