Skip to main content

bmlib

CI

Shared Python library for biomedical literature tools — LLM abstraction, quality assessment, transparency analysis, full-text retrieval, publication ingestion, and database utilities.

Version: 0.12.0 | License: AGPL-3.0-or-later | Python: >=3.11

Installation

# Core (only jinja2 dependency)
pip install bmlib

# Editable install with all extras
uv pip install -e ".[all,dev]"

Optional dependency groups

Group Install command Provides
anthropic pip install bmlib[anthropic] Anthropic Claude LLM provider
ollama pip install bmlib[ollama] Ollama local LLM provider
openai pip install bmlib[openai] OpenAI, DeepSeek, Mistral, Gemini, and OpenAI-compatible providers
postgresql pip install bmlib[postgresql] PostgreSQL database backend
transparency pip install bmlib[transparency] Transparency analysis (httpx)
publications pip install bmlib[publications] Publication ingestion and sync (httpx)
fulltext pip install bmlib[fulltext] FullTextService retrieval (httpx). The rest of bmlib.fulltext — JATS parser, models, SectionSegmenter — needs nothing beyond core
pdf pip install bmlib[pdf] PDF → text conversion (pymupdf)
html pip install bmlib[html] HTML → canonical text, canonical_from_html (markupever, html5ever's Python binding)
embed pip install bmlib[embed] In-process embedding, bmlib.embed.Embedder (llama-cpp-python; compiles llama.cpp, so not in all)
dev pip install bmlib[dev] pytest, pytest-cov, ruff, mypy, types-psycopg2
all pip install bmlib[all] Every runtime extra above but embed (not dev)

Upgrading to 0.12

0.12.0 breaks public APIs in fulltext, llm, context_processor, publications and quality. Each break is listed under Changed — breaking in the CHANGELOG. It also moves stored data:

  • bmlib's full-text cache re-fetches every entry an earlier version rendered.
  • An OpenAlex sync now stores pmcid.
  • Re-import the Retraction Watch export to store a notice's own PMID unpadded.
  • Stored extract_study_type output moves for some reviews.
  • ensure_schema() is unchanged.

The CHANGELOG's 0.12.0 summary says what each of these costs. Upgrading from 0.10 or earlier, read 0.11.0's as well.

Modules

Module Description
bmlib.db Thin database abstraction (SQLite + PostgreSQL) with pure functions over DB-API connections
bmlib.llm Unified LLM client with pluggable providers (Anthropic, OpenAI, Ollama, DeepSeek, Mistral, Gemini) — chat, tool calling, embeddings, reasoning traces, JSON repair, and text chunking
bmlib.templates Jinja2-based prompt template engine with user-override directory fallback
bmlib.agents Base agent class for LLM-driven tasks with template rendering and JSON parsing
bmlib.context_processor Hierarchical map-reduce over content that exceeds one LLM context window — batch, extract, consolidate recursively
bmlib.quality 4-tier quality assessment pipeline for biomedical publications (metadata → LLM classifier → deep assessment → Cochrane nine-domain risk of bias), plus rule-based extractors
bmlib.transparency Multi-API transparency and bias analysis (CrossRef, Europe PMC, PubMed, OpenAlex, ClinicalTrials.gov)
bmlib.publications Publication ingestion from PubMed, bioRxiv, medRxiv, and OpenAlex with deduplication and sync
bmlib.fulltext Tiered full-text retrieval (caller-supplied sources → Europe PMC → Unpaywall → DOI), JATS XML parsing, PDF → text conversion, section segmentation, and disk-based caching
bmlib.citations Citation-marker parsing, Vancouver/APA/Harvard/Chicago formatting, and reference-list building (pure stdlib)

Quick Start

Database

from bmlib.db import connect_sqlite, execute, fetch_all, transaction

conn = connect_sqlite("~/.myapp/data.db")
with transaction(conn):
    execute(conn, "INSERT INTO papers (doi, title) VALUES (?, ?)", ("10.1101/x", "A paper"))
rows = fetch_all(conn, "SELECT * FROM papers")

LLM

from bmlib.llm import LLMClient, LLMMessage

client = LLMClient(default_provider="ollama")
response = client.chat(
    messages=[LLMMessage(role="user", content="Summarise this paper.")],
    model="ollama:medgemma4B_it_q8",
)
print(response.content)

Model strings use the format "provider:model_name":

"anthropic:claude-sonnet-4-20250514"
"openai:gpt-4o"
"ollama:medgemma4B_it_q8"
"deepseek:deepseek-chat"
"mistral:mistral-large-latest"
"gemini:gemini-2.0-flash"

Tool Calling

from bmlib.llm import LLMClient, LLMMessage, LLMToolDefinition

search = LLMToolDefinition(
    name="search_pubmed",
    description="Search PubMed for articles matching a query.",
    parameters={
        "type": "object",
        "properties": {"query": {"type": "string"}},
        "required": ["query"],
    },
)

client = LLMClient()
response = client.chat(
    messages=[LLMMessage(role="user", content="Find recent trials on statins.")],
    model="anthropic:claude-sonnet-4-20250514",
    tools=[search],
)

for call in response.tool_calls or []:
    print(call.name, call.arguments)  # arguments is already a parsed dict

To continue the conversation, append the assistant message (carrying tool_calls) and one role="tool" message per call, each with the matching tool_call_id, then send the whole list again.

Reasoning Traces

response = client.chat(
    messages=[LLMMessage(role="user", content="Complex reasoning task...")],
    model="ollama:qwen3:8b",
    think=True,  # or "low"/"medium"/"high", or an int token budget
)
print(response.thinking)  # the reasoning trace, or None
print(response.content)   # the final answer, never mixed with the trace

Each provider maps think= onto its own parameter; one with no native mapping still extracts reasoning from the response. See docs/manual/llm.md for the per-provider table.

Long Documents

from bmlib.llm import chunk_text, process_with_map_reduce

for chunk in chunk_text(paper_text, chunk_size=8000, overlap=200):
    print(chunk.chunk_index, chunk.size)

summary = process_with_map_reduce(
    paper_text,
    map_fn=lambda part: summarise(part),
    reduce_fn=lambda parts: summarise("\n".join(parts)),
)

Publication Sync

from datetime import date
from bmlib.db import connect_sqlite
from bmlib.publications import sync

conn = connect_sqlite("publications.db")
report = sync(
    conn,
    sources=["pubmed", "biorxiv"],
    # An ordinary week. A window containing a first-of-month meets a day of
    # tens of thousands of records — see docs/manual/publications.md.
    date_from=date(2025, 3, 3),
    date_to=date(2025, 3, 9),
    email="researcher@example.com",
)
print(f"Added: {report.records_added}, Merged: {report.records_merged}")

Full-Text Retrieval

from bmlib.fulltext import FullTextService

service = FullTextService(email="researcher@example.com")

# Passing identifier= enables the built-in disk cache (platform default dir).
result = service.fetch_fulltext(
    pmc_id="PMC7614751", doi="10.1234/example", identifier="PMC7614751"
)

if result.html:
    print(result.html[:200])

Quality Assessment

from bmlib.llm import LLMClient
from bmlib.quality import QualityManager

llm = LLMClient()
manager = QualityManager(
    llm=llm,
    classifier_model="anthropic:claude-3-haiku-20240307",
    assessor_model="anthropic:claude-sonnet-4-20250514",
)

assessment = manager.assess(
    title="A Randomized Controlled Trial of ...",
    abstract="We conducted a double-blind RCT ...",
    publication_types=["Randomized Controlled Trial"],
)
print(assessment.study_design, assessment.quality_tier)

Citations

from bmlib.citations import CitationStyle, DocumentMetadata, format_document

text = "Statins reduce mortality [@id:12345:Smith2023]."
documents = {
    12345: DocumentMetadata(
        document_id=12345,
        title="Statins and mortality",
        authors=["John Smith", "Anna Johnson"],  # "Given Surname"
        journal="Lancet",
        year=2023,
    )
}

# Markers are replaced in order of first appearance and the reference
# list is appended; pass include_reference_list=False to skip it.
print(format_document(text, documents, style=CitationStyle.VANCOUVER))

Transparency Analysis

from bmlib.transparency import TransparencyAnalyzer

analyzer = TransparencyAnalyzer(email="researcher@example.com")
result = analyzer.analyze("doc-001", doi="10.1038/s41586-024-00001-0")
print(result.transparency_score, result.risk_level)

Development

# Install with dev dependencies
uv pip install -e ".[all,dev]"

# Run tests
uv run pytest tests/ -v

# The PostgreSQL half of tests/test_backends.py skips unless a DSN is set,
# so a local green run can hide SQLite-only SQL. CI sets this against a
# postgres:18 service and makes the skip a failure. The database must be one
# the tests may drop every table in.
BMLIB_TEST_POSTGRESQL_DSN="host=/tmp/pgrun port=5432 dbname=bmlib_test user=postgres" \
    uv run pytest tests/test_backends.py

# Lint and format. CI pins ruff, so use the pinned version rather than
# whatever is in .venv — a stale local ruff false-flags rules a newer
# release removed, and a newer one flags files your PR never touched.
uvx ruff@0.15.20 check .
uvx ruff@0.15.20 format --check .

# Type-check. Scope and settings live in pyproject.toml, so this bare
# command is the one CI runs. It needs the extras installed above: all but
# psycopg2 ship their own py.typed (types-psycopg2 covers that one), and
# against a bare interpreter mypy reports them — and jinja2 — as missing
# stubs.
uv run mypy

Documentation

Where What
docs/manual/ Full API documentation, one page per module
CHANGELOG.md What changed, per release and unreleased
docs/DECISIONS.md Deliberate non-fixes — things that read as bugs but were investigated and closed as correct, each with the test that pins it

License

AGPL-3.0-or-later

Metadata

Release files for bmlib 0.12.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 bmlib 0.12.0
File Size Uploaded Provenance Actions
bmlib-0.12.0.tar.gz 2.0 MB With provenance Details for bmlib-0.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bmlib 0.12.0
File Interpreter ABI Platform Actions
bmlib-0.12.0-py3-none-any.whl Python 3 none any Details for bmlib-0.12.0-py3-none-any.whl

Total release size: 2.8 MB

Release files / bmlib-0.12.0.tar.gz

Download URL bmlib-0.12.0.tar.gz
Size 2.0 MB
Tags Source
SHA-256 checksum
How to use checksums
ce04ec53ecbfe9b42383f5e6aaed5a33d66932ba55c74d8ddffddb1a87fb3d10
BLAKE2b-256 checksum
How to use checksums
4d48d962efb926a1013064527e08c89719411f553efd1f5e9168504b061fc316
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 Oct 31, 2026.

Transparency log

Release files / bmlib-0.12.0-py3-none-any.whl

Download URL bmlib-0.12.0-py3-none-any.whl
Size 801.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5557fdd103de720a8bcab48f67e5fd7d6f356029b4963866644d5cc058ade485
BLAKE2b-256 checksum
How to use checksums
f8a82674b77e426886f614165703b9112e12f36833ede4f3a52932a082ce88b1
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 Oct 31, 2026.

Transparency log

Security for bmlib 0.12.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 bmlib-0.12.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 Oct 31, 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.

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