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.1.tar.gz (567.9 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.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: groundmark-0.2.1.tar.gz
  • Upload date:
  • Size: 567.9 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.1.tar.gz
Algorithm Hash digest
SHA256 44bbecadbf31295248f36ec46a973f78ff67d911c0d32d1f878b003a2383fe64
MD5 ce0919c475b4e4c36fa0d3b9fe73b78c
BLAKE2b-256 a4864b307cd097cc5b916b1d1faf698da9d47b59812f1e14ce281a3a23befc01

See more details on using hashes here.

Provenance

The following attestation bundles were made for groundmark-0.2.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: groundmark-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d3b5d2962f038c5f140d3f20ed48a49ce54f36916fc736251724d97cebb37e40
MD5 33ed4a98e698f7e59e635dc3b636011a
BLAKE2b-256 f80686dc2b57d91b0689da66c3f1e7c53a32f4ee5895ccc6d229fe1b05a391bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for groundmark-0.2.1-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