Skip to main content

PDF to grounded Markdown with bounding box annotations

Project description

groundmark

groundmark

Grounded Markdown for PDFs

groundmark is a thin, batteries-included wrapper around anchorite. It provides concrete implementations of anchorite's provider protocols — Pydantic AI for LLM-based Markdown generation and pdfplumber for bounding box extraction — so you can go from PDF bytes to annotated Markdown in a single call. All the heavy lifting (Smith-Waterman alignment, annotation, stripping, quote resolution) lives in anchorite.

Give it a PDF and a model string, get back Markdown with embedded bounding box coordinates that trace every text span back to its location in the source PDF.

Architecture

The library processes documents in two streams that are then merged:

  1. Semantic Stream: The PDF is sent to an LLM (via Pydantic AI) to produce clean Markdown with <!--page--> markers between pages.

  2. Positional Stream: The PDF is parsed locally by pdfplumber to extract line-level text segments and their bounding boxes.

  3. Alignment: Smith-Waterman alignment (via anchorite) maps each parsed line to its position in the Markdown, constrained by page boundaries.

  4. Annotation: Bounding box coordinates are injected as HTML span attributes:

    <span data-bbox="120,45,180,890" data-page="3">The patient presented with</span>
    

Quick Start

import asyncio
import groundmark as gm

async def main():
    pdf_bytes = open("document.pdf", "rb").read()

    config = gm.Config(model="bedrock:au.anthropic.claude-sonnet-4-6")

    # PDF -> annotated Markdown (one call)
    result = await gm.process(pdf_bytes, config)
    print(f"Coverage: {result.coverage_percent:.2%}")
    print(result.annotated_markdown[:500])

    # Strip for LLM consumption
    stripped = gm.strip(result.annotated_markdown)
    # stripped.plain_text: clean Markdown with spans removed
    # stripped.validation_map: list of (start, end, Anchor) ranges

    # Resolve verbatim quotes to PDF coordinates
    resolved = gm.resolve(result.annotated_markdown, ["the patient presented with"])
    # -> {"the patient presented with": [(page, BBox), ...]}

if __name__ == "__main__":
    asyncio.run(main())

Debug Visualizer

The included visualizer overlays extracted bounding boxes onto the source PDF, useful for diagnosing alignment issues. Blue highlights show raw extracted boxes from pdfplumber; red highlights show aligned boxes from the annotated Markdown.

python -m groundmark.visualize input.pdf output.pdf --model "bedrock:au.anthropic.claude-sonnet-4-6"

# Or with cached Markdown:
python -m groundmark.visualize input.pdf output.pdf --markdown cached.md

Visualizer output showing blue (raw) and red (aligned) bounding box overlays

Screenshot from Santoro et al., "Health outcomes and drug utilisation in children with Noonan syndrome: a European cohort study," Orphanet J Rare Dis 20:76 (2025). doi:10.1186/s13023-025-03594-7. CC-BY 4.0.

Configuration

Timeouts

The LLM call for PDF-to-Markdown conversion can take several minutes for large documents, especially with Opus on Bedrock. Timeout defaults by provider:

Provider Default Environment Variable
Bedrock (boto3) 300s AWS_READ_TIMEOUT
Anthropic (httpx) 600s — (use ModelSettings(timeout=...))

For Bedrock with Opus, 300s may not be enough. Set a higher timeout:

export AWS_READ_TIMEOUT=600

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

groundmark-0.2.0.tar.gz (568.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

groundmark-0.2.0-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file groundmark-0.2.0.tar.gz.

File metadata

  • Download URL: groundmark-0.2.0.tar.gz
  • Upload date:
  • Size: 568.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for groundmark-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fd8cd21abc9c94d9d510988d04239f860fbe981b77fbdc9ee8ea45d796007bbc
MD5 f6fe24c98e48f99a34f0ff26adbf4551
BLAKE2b-256 4bedcdc8dd644a724907d8e39cbac962c716ab6719197e5f89420f4938d4c415

See more details on using hashes here.

Provenance

The following attestation bundles were made for groundmark-0.2.0.tar.gz:

Publisher: release.yaml on populationgenomics/groundmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file groundmark-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: groundmark-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for groundmark-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cce050d945428036e64d538ffd6c8878fd02aaa7c2228cd47c45a5b30505aca0
MD5 7b12cc4cf3f6e3b228554773b6a45f77
BLAKE2b-256 322c073a53592f5e537d5fe2064105ba31f86a84f5ce5d5a2158625df9ab81d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for groundmark-0.2.0-py3-none-any.whl:

Publisher: release.yaml on populationgenomics/groundmark

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page