vera_ingest package¶
PDF parsing, chunking, and conversion to .vera archives.
vera_ingest
¶
Source ingestion, chunking, and conversion adapters for VERA.
Modules:
-
convert–
Classes:
-
Chunk–Text segment produced by the extraction chunker.
-
ParsedBlock–Layout block extracted from a PDF page.
-
ParsedPage–Single page extracted from a PDF.
Functions:
-
batch_convert–Convert PDFs from a directory scan or an explicit path list.
-
parse_pdf– -
parse_pdf_structured–Parse a PDF into pages plus structured blocks.
-
chunk_pages– -
build_chunks_from_blocks–Heading-aware chunking over structured blocks.
-
detect_heading– -
figures–Return figure attachments produced during ingest.
-
figures_for–Return figure attachments linked to a query result.
-
get_page–Return ingest-provided viewer data for one page.
-
get_blocks–Return ingest-provided layout blocks.
-
get_chunk_regions–Return ingest-provided highlight regions for a chunk.
-
regions_for–Return ingest-provided highlight regions for a query result.
-
get_source_document–Return the attachment identified as the archive's source document.
-
export_source_document–Write the source attachment to disk and return its path.
Chunk
dataclass
¶
Chunk(text: str, page_start: int, page_end: int, heading_path: str, token_count: int, block_ids: list[str] = list())
Text segment produced by the extraction chunker.
Attributes:
-
text(str) –Chunk content.
-
page_start(int) –First page number spanned by the chunk.
-
page_end(int) –Last page number spanned by the chunk.
-
heading_path(str) –Heading breadcrumb at chunk start.
-
token_count(int) –Approximate token count.
-
block_ids(list[str]) –Source layout block identifiers.
ParsedBlock
dataclass
¶
ParsedBlock(page_number: int, block_type: str, text: str, bbox: tuple[float, float, float, float] | None = None, heading_level: int | None = None, image_bytes: bytes | None = None, image_ext: str = '')
Layout block extracted from a PDF page.
Attributes:
-
page_number(int) –1-based page number.
-
block_type(str) –One of
heading,paragraph,image,caption, ortable. -
text(str) –Block text content.
-
bbox(tuple[float, float, float, float] | None) –Bounding box
(x0, y0, x1, y1)in page points, when available. -
heading_level(int | None) –Heading depth for heading blocks.
-
image_bytes(bytes | None) –Raw image bytes for image blocks.
-
image_ext(str) –Image format extension for image blocks.
ParsedPage
dataclass
¶
Single page extracted from a PDF.
Attributes:
-
page_number(int) –1-based page number.
-
width(float | None) –Page width in points, when available.
-
height(float | None) –Page height in points, when available.
-
text(str) –Concatenated native text for the page.
batch_convert
¶
batch_convert(directory: str | None = None, *, paths: Sequence[str] | None = None, recursive: bool = False, overwrite: bool = False, model: str = 'hashing', parser: str = 'pymupdf', chunk_size: int = 500, overlap: int = 75, store_original: bool = True, ocr_mode: str = 'auto', ocr_language: str = 'eng', ocr_dpi: int = 300, progress: Callable[[int, int, str], None] | None = None, cancel: Any | None = None) -> dict[str, Any]
Convert PDFs from a directory scan or an explicit path list.
Parameters:
-
directory(str | None, default:None) –Root directory to scan for PDFs when
pathsis omitted. -
paths(Sequence[str] | None, default:None) –Explicit PDF file paths to convert. When set, directory discovery is skipped and
recursiveis ignored. -
recursive(bool, default:False) –When
True, scan subdirectories (directory mode only). -
overwrite(bool, default:False) –When
True, replace existing.veraoutputs. -
model(str, default:'hashing') –Embedding model name passed to :func:
convert. -
parser(str, default:'pymupdf') –PDF parser backend passed to :func:
convert. -
chunk_size(int, default:500) –Target chunk size passed to :func:
convert. -
overlap(int, default:75) –Chunk overlap passed to :func:
convert. -
store_original(bool, default:True) –Whether to embed originals passed to :func:
convert. -
ocr_mode(str, default:'auto') –OCR mode passed to :func:
convert. -
ocr_language(str, default:'eng') –OCR language passed to :func:
convert. -
ocr_dpi(int, default:300) –OCR DPI passed to :func:
convert. -
progress(Callable[[int, int, str], None] | None, default:None) –Optional
(current, total, filename)callback. -
cancel(Any | None, default:None) –Optional cancellation token.
Returns:
-
dict[str, Any]–A report dict with
converted,skipped,failed, and related -
dict[str, Any]–fields.
directoryis the scan root, or the common parent of -
dict[str, Any]–paths.
Raises:
-
NotADirectoryError–When
directoryis not a directory. -
FileNotFoundError–When a path in
pathsis missing. -
ValueError–When neither
directorynorpathsis usable.
parse_pdf_structured
¶
parse_pdf_structured(path: str, *, ocr_mode: str = 'auto', ocr_language: str = 'eng', ocr_dpi: int = 300, diagnostics: dict[str, Any] | None = None, cancel: Any | None = None) -> tuple[list[ParsedPage], list[ParsedBlock]]
Parse a PDF into pages plus structured blocks.
Detects headings via font size/weight relative to body text, keeps paragraphs as text blocks, and captures embedded images as image blocks.
build_chunks_from_blocks
¶
build_chunks_from_blocks(blocks: list[tuple[str, ParsedBlock]], chunk_size: int = 500, overlap: int = 75) -> list[Chunk]
Heading-aware chunking over structured blocks.
figures
¶
figures(document: VeraDocument, page_start: int | None = None, page_end: int | None = None, include_data: bool = False) -> list[dict[str, Any]]
Return figure attachments produced during ingest.
figures_for
¶
figures_for(document: VeraDocument, result: QueryResult, include_data: bool = False) -> list[dict[str, Any]]
Return figure attachments linked to a query result.
get_page
¶
get_page(document: VeraDocument, page_number: int) -> dict[str, Any] | None
Return ingest-provided viewer data for one page.
get_blocks
¶
get_blocks(document: VeraDocument, page_number: int | None = None) -> list[dict[str, Any]]
Return ingest-provided layout blocks.
get_chunk_regions
¶
get_chunk_regions(document: VeraDocument, chunk_id: str) -> list[dict[str, Any]]
Return ingest-provided highlight regions for a chunk.
regions_for
¶
regions_for(document: VeraDocument, result: QueryResult) -> list[dict[str, Any]]
Return ingest-provided highlight regions for a query result.
get_source_document
¶
get_source_document(document: VeraDocument) -> AttachmentRecord
Return the attachment identified as the archive's source document.
export_source_document
¶
export_source_document(document: VeraDocument, path: str | PathLike[str] | None = None) -> str
Write the source attachment to disk and return its path.
Conversion writes through VeraDocument. Viewer helpers
interpret ingest-produced attachments and metadata. See the
conversion guide and
figures and regions.
convert
¶
Functions:
-
convert–Convert a PDF into a validated
.veraarchive. -
batch_convert–Convert PDFs from a directory scan or an explicit path list.
convert
¶
convert(input_path: str, output_path: str, *, model: str = 'hashing', parser: str = 'pymupdf', chunk_size: int = 500, overlap: int = 75, store_original: bool = True, ocr_mode: str = 'auto', ocr_language: str = 'eng', ocr_dpi: int = 300, cancel: Any | None = None) -> str
Convert a PDF into a validated .vera archive.
Parses the PDF, chunks extracted text, embeds chunks, and writes the
result through :class:~vera.document.VeraDocument. The archive is
validated before the temporary file is published atomically.
Parameters:
-
input_path(str) –Source PDF path.
-
output_path(str) –Destination
.verapath. -
model(str, default:'hashing') –Embedding model name (default
"hashing"). -
parser(str, default:'pymupdf') –PDF parser backend (currently only
"pymupdf"). -
chunk_size(int, default:500) –Target chunk size in characters.
-
overlap(int, default:75) –Character overlap between consecutive chunks.
-
store_original(bool, default:True) –When
True, embed the original PDF as an attachment. -
ocr_mode(str, default:'auto') –"auto"(default),"off", or"force". -
ocr_language(str, default:'eng') –Tesseract language code (default
"eng"). -
ocr_dpi(int, default:300) –Rasterization DPI for OCR.
-
cancel(Any | None, default:None) –Optional cancellation token with
raise_if_cancelled().
Returns:
-
str–The
output_pathstring.
Raises:
-
FileNotFoundError–When
input_pathdoes not exist. -
ValueError–When no searchable text is extracted or the parser is unsupported.
batch_convert
¶
batch_convert(directory: str | None = None, *, paths: Sequence[str] | None = None, recursive: bool = False, overwrite: bool = False, model: str = 'hashing', parser: str = 'pymupdf', chunk_size: int = 500, overlap: int = 75, store_original: bool = True, ocr_mode: str = 'auto', ocr_language: str = 'eng', ocr_dpi: int = 300, progress: Callable[[int, int, str], None] | None = None, cancel: Any | None = None) -> dict[str, Any]
Convert PDFs from a directory scan or an explicit path list.
Parameters:
-
directory(str | None, default:None) –Root directory to scan for PDFs when
pathsis omitted. -
paths(Sequence[str] | None, default:None) –Explicit PDF file paths to convert. When set, directory discovery is skipped and
recursiveis ignored. -
recursive(bool, default:False) –When
True, scan subdirectories (directory mode only). -
overwrite(bool, default:False) –When
True, replace existing.veraoutputs. -
model(str, default:'hashing') –Embedding model name passed to :func:
convert. -
parser(str, default:'pymupdf') –PDF parser backend passed to :func:
convert. -
chunk_size(int, default:500) –Target chunk size passed to :func:
convert. -
overlap(int, default:75) –Chunk overlap passed to :func:
convert. -
store_original(bool, default:True) –Whether to embed originals passed to :func:
convert. -
ocr_mode(str, default:'auto') –OCR mode passed to :func:
convert. -
ocr_language(str, default:'eng') –OCR language passed to :func:
convert. -
ocr_dpi(int, default:300) –OCR DPI passed to :func:
convert. -
progress(Callable[[int, int, str], None] | None, default:None) –Optional
(current, total, filename)callback. -
cancel(Any | None, default:None) –Optional cancellation token.
Returns:
-
dict[str, Any]–A report dict with
converted,skipped,failed, and related -
dict[str, Any]–fields.
directoryis the scan root, or the common parent of -
dict[str, Any]–paths.
Raises:
-
NotADirectoryError–When
directoryis not a directory. -
FileNotFoundError–When a path in
pathsis missing. -
ValueError–When neither
directorynorpathsis usable.
batch_convert
¶
batch_convert(directory: str | None = None, *, paths: Sequence[str] | None = None, recursive: bool = False, overwrite: bool = False, model: str = 'hashing', parser: str = 'pymupdf', chunk_size: int = 500, overlap: int = 75, store_original: bool = True, ocr_mode: str = 'auto', ocr_language: str = 'eng', ocr_dpi: int = 300, progress: Callable[[int, int, str], None] | None = None, cancel: Any | None = None) -> dict[str, Any]
Convert PDFs from a directory scan or an explicit path list.
Parameters:
-
directory(str | None, default:None) –Root directory to scan for PDFs when
pathsis omitted. -
paths(Sequence[str] | None, default:None) –Explicit PDF file paths to convert. When set, directory discovery is skipped and
recursiveis ignored. -
recursive(bool, default:False) –When
True, scan subdirectories (directory mode only). -
overwrite(bool, default:False) –When
True, replace existing.veraoutputs. -
model(str, default:'hashing') –Embedding model name passed to :func:
convert. -
parser(str, default:'pymupdf') –PDF parser backend passed to :func:
convert. -
chunk_size(int, default:500) –Target chunk size passed to :func:
convert. -
overlap(int, default:75) –Chunk overlap passed to :func:
convert. -
store_original(bool, default:True) –Whether to embed originals passed to :func:
convert. -
ocr_mode(str, default:'auto') –OCR mode passed to :func:
convert. -
ocr_language(str, default:'eng') –OCR language passed to :func:
convert. -
ocr_dpi(int, default:300) –OCR DPI passed to :func:
convert. -
progress(Callable[[int, int, str], None] | None, default:None) –Optional
(current, total, filename)callback. -
cancel(Any | None, default:None) –Optional cancellation token.
Returns:
-
dict[str, Any]–A report dict with
converted,skipped,failed, and related -
dict[str, Any]–fields.
directoryis the scan root, or the common parent of -
dict[str, Any]–paths.
Raises:
-
NotADirectoryError–When
directoryis not a directory. -
FileNotFoundError–When a path in
pathsis missing. -
ValueError–When neither
directorynorpathsis usable.