Skip to main content

section-anything-haystack

A Haystack converter for Section Anything: it finds the sections of a PDF (every heading, its level, its pages and its exact text) and returns one Document per section, with the heading path in the metadata. The default mode runs locally with rules only, calls no model and costs nothing.

Install

pip install section-anything-haystack
# latest from GitHub:
pip install "git+https://github.com/Omniwot/section-anything#subdirectory=packages/section-anything-haystack"

Python 3.11+. Depends on section-anything and haystack-ai (2.20 or newer, 3.x included) only.

Example

from haystack_integrations.components.converters.section_anything import SectionAnythingConverter

converter = SectionAnythingConverter()  # mode="free": rules only, no model, $0
docs = converter.run(sources=["paper.pdf"])["documents"]  # file paths or ByteStreams

doc = docs[2]
print(repr(doc.content[:59]))    # '### 1.1 Scope\n\nSectioning splits a long document into parts'
print(doc.meta["heading_path"])  # 1 Introduction > 1.1 Scope
print(doc.meta["page_start"], doc.meta["page_end"])  # 1 1

In an indexing pipeline, with sections split at 1,500 characters:

from haystack import Pipeline
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.components.writers import DocumentWriter
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack_integrations.components.converters.section_anything import SectionAnythingConverter

store = InMemoryDocumentStore()
indexing = Pipeline()
indexing.add_component("converter", SectionAnythingConverter(chunk={"max_chars": 1500}))
indexing.add_component("writer", DocumentWriter(document_store=store))
indexing.connect("converter.documents", "writer.documents")
print(indexing.run({"converter": {"sources": ["paper.pdf"]}}))  # {'writer': {'documents_written': 6}}

hit = InMemoryBM25Retriever(document_store=store).run(query="data")["documents"][0]
print(hit.meta["heading_path"], hit.meta["page_start"])  # 2 Methods > 2.1 Data 2

To embed each section with its place in the document, give the embedder meta_fields_to_embed=["heading_path"]; with chunk, the text already starts with the heading path. Pipelines that use the converter save and load as YAML (Pipeline.dumps, Pipeline.loads): it lives in the haystack_integrations namespace, which Haystack 3 trusts by default, and an API key is saved only as the name of its environment variable.

Options

SectionAnythingConverter(mode="free", *, include_heading=True, chunk=False, store_full_path=False, api_key=None, section_kwargs=None), then run(sources, meta=None) (also run_async).

  • mode: free (bookmarks, table of contents and typography rules; nothing leaves the machine), quick (a small model reviews candidate heading lines, about $0.07 per 1,000 pages), standard (a model reads every line), auto (free, then quick when Free is unsure) or local:<ollama model>. Over the per-document cost cap (default $0.50) a run is downgraded (standard → quick → free) or refused.
  • include_heading: start each Document with its heading as a Markdown line (## 1 Introduction). With False, sections with no text of their own (a heading directly followed by a subheading) are skipped.
  • chunk: size-capped chunks instead of whole sections (SectionTree.chunks): True for its defaults (at most 2,000 characters, sibling sections under 200 merged), or a dict of its options (max_chars, min_chars, overlap_chars, length, prefix, lead_in). Each Document's text then starts with the chunk's heading path (3 Method > 3.1 Data) unless prefix is False, and its metadata adds chunk_index and lead_in. lead_in is off by default; with True, a chunk that starts inside a list carries the line that introduces the list, for example "Apply the following changes to the method:", in its lead_in metadata and, with prefix, in its text under the heading path.
  • store_full_path: keep each source's full path in file_path; by default only the file name.
  • api_key: for the paid modes, a Secret (Secret.from_env_var("SECTIONANY_API_KEY")), so the key is never written to a pipeline file. By default the library reads its own environment variables (SECTIONANY_API_KEY, or OPENAI_API_KEY, or ANTHROPIC_API_KEY for Claude models). Keys are never logged or put in metadata.
  • section_kwargs: other options for section_anything.section: model, base_url, provider, max_usd, prices, over_budget, max_pages, extract_timeout, progress, settings. Anything not given falls back to the SECTIONANY_* environment variables; see the library README. Unknown option names fail when the converter is built, not after a PDF is read.

A source that cannot be sectioned (not a PDF, damaged, over a size cap, no text layer, a failed model call) is skipped with a warning, as Haystack's other converters do. A configuration error (unknown mode, no API key, a model with no price) is raised, since it would fail every source.

Text and metadata

A Document's text is the section's own text, from its heading to the next heading, reflowed into paragraphs with running headers and footers removed (the same text as section-anything --body). Text before the first heading is a Document of its own (flag front_matter).

Metadata is flat (str, int, float or None), so it works with every document store and with an embedder's meta_fields_to_embed. A ByteStream's own meta and the meta passed to run are kept; the keys below take precedence over them.

Key Meaning
file_path the PDF's file name (its path with store_full_path=True), or a ByteStream's own file_path
section_index position in the document's section list (0-based)
title, number, level heading text, numbering as printed ("3.1" or ""), 1 = top level
heading_path headings from the outermost section down to this one, joined with " > "
page_start, page_end 1-based pages; page_number repeats page_start, under the key Haystack's splitters use
heading_page, heading_bbox where the heading is printed: its page and "x0,y0,x1,y1" in PDF points from the page's bottom-left corner (None when nothing is printed)
start, end character range in the library's extracted text
flags comma-separated, for example doc_title, front_matter, generated_title
document_title the document's title, if found
mode, model, document_cost_usd how the document was sectioned (after any downgrade) and what the whole document cost

To cap sizes, use chunk rather than a DocumentSplitter after the converter: chunks keep their own pages and heading path, while DocumentSplitter counts page_number from 1 again in each Document it splits.

Licence

Apache-2.0. See LICENSE and NOTICE.

Metadata

Release files for section-anything-haystack 0.1.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 section-anything-haystack 0.1.0
File Size Uploaded Provenance Actions
section_anything_haystack-0.1.0.tar.gz 22.6 kB With provenance Details for section_anything_haystack-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for section-anything-haystack 0.1.0
File Interpreter ABI Platform Actions
section_anything_haystack-0.1.0-py3-none-any.whl Python 3 none any Details for section_anything_haystack-0.1.0-py3-none-any.whl

Total release size: 39.9 kB

Release files / section_anything_haystack-0.1.0.tar.gz

Download URL section_anything_haystack-0.1.0.tar.gz
Size 22.6 kB
Tags Source
SHA-256 checksum
How to use checksums
666cee5b23057f221067183de3a3e038447a6c8f4af2efb09fc6a01fec32a64a
BLAKE2b-256 checksum
How to use checksums
c7f5243956e8ed3aa853cade4eda31443bca41a177e7ce085d8bef27b8601ce6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Dec 1, 2026.

Transparency log

Release files / section_anything_haystack-0.1.0-py3-none-any.whl

Download URL section_anything_haystack-0.1.0-py3-none-any.whl
Size 17.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da91384f40179c443a520ac5969cddbf1f5c7219abf51c7c73072dc926d4583c
BLAKE2b-256 checksum
How to use checksums
24cc983074d231f061354bb5a95bdae4c56b79f818adba085d9a28169ae566ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 Dec 1, 2026.

Transparency log

Security for section-anything-haystack 0.1.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for section_anything_haystack-0.1.0.tar.gz

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 Dec 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

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