Typed protobuf + pydantic models and converters for NLM PubMed XML.
Project description
pubmed-proto
Generates pubmed_proto — a typed Python package for parsing NLM PubMed XML
into protobuf and pydantic models — from the PubMed DTD.
This repository is the generator, not the package. It holds the inputs
(pubmed.dtd, pubmed_transforms.yaml) and drives
xsd-former (the
xsdformer CLI) to emit the pubmed_proto source tree, which is then built into
a wheel and published to PyPI. The generated tree (generated/) and build
outputs (dist/) are gitignored — only the inputs are version-controlled.
Consuming pubmed_proto
Depend on the published wheel, not this repo:
pip install pubmed_proto # or: uv add pubmed_proto
from lxml import etree
from pubmed_proto import xml_converter, pydantic_converter, models
tree = etree.parse("efetch_output.xml")
article_el = tree.getroot().find("PubmedArticle")
proto = xml_converter.PubmedArticle(article_el) # XML -> protobuf
model = pydantic_converter.PubmedArticle_from_proto(proto) # protobuf -> pydantic
json_str = model.model_dump_json() # pydantic -> JSON
The package exposes four modules (all typed; ships py.typed):
| module | purpose |
|---|---|
pubmed_pb2 |
compiled protobuf messages (Article, Author, …) |
models |
pydantic models mirroring the protobuf schema |
xml_converter |
PubMed XML → protobuf (per-message factory funcs) |
pydantic_converter |
protobuf ↔ pydantic (X_from_proto / X_to_proto) |
Developing the generator
Requires uv.
make generate # DTD + transforms -> generated/pubmed_proto/
make build # generate, then build the wheel into dist/
make clean # remove generated/ and dist/
uv run --group test pytest # round-trip gate over real PubMed records
Shaping the output is done in pubmed_transforms.yaml — dropping
book/admin types, flattening list wrappers, coercing booleans/timestamps, and
serializing rich-text fields to markdown. See the
xsd-former docs for the
transform reference.
Provenance & attribution
pubmed.dtd is derived from the U.S. National Library of Medicine PubMed
DTD, version pubmed_250101 (dated 2024-08-28):
https://dtd.nlm.nih.gov/ncbi/pubmed/out/pubmed_250101.dtd
NLM DTDs are U.S. Government works and public domain in the United States; the
MIT LICENSE in this repo covers CPG's own files (transforms, generator wiring,
tests), not the NLM DTD.
Local modification: the external MathML module include
(<!ENTITY % mathml-in-pubmed SYSTEM "mathml-in-pubmed.mod"> and its reference)
was removed so the DTD is self-contained for schema generation — MathML markup
in titles/abstracts is not modelled. With that one include removed, the file is
byte-identical to upstream pubmed_250101.
The DTD is vendored deliberately, not fetched at build time: it's a modified
derivative (so a fetch wouldn't reproduce it), and pinning the exact bytes keeps
the generated schema reproducible. The full provenance also lives in a comment
at the top of pubmed.dtd.
Releasing
The published version is build.version in pubmed_transforms.yaml (what
xsdformer stamps into the wheel). To release:
- Bump
build.versioninpubmed_transforms.yaml. - Publish a GitHub Release tagged
vX.Y.Zmatching that version.
The release workflow generates, builds, and publishes to PyPI via Trusted
Publishing (OIDC). It fails if the tag and build.version disagree.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pubmed_proto-0.1.0.tar.gz.
File metadata
- Download URL: pubmed_proto-0.1.0.tar.gz
- Upload date:
- Size: 30.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a375416097ee17dbd4f099b434e0854af4feafe7d51f14902bda2ecf49e58797
|
|
| MD5 |
56210a6e10f5d4a3b9371d9e76ee3746
|
|
| BLAKE2b-256 |
ac7228e1905fd830361215e64a12832714087a8490c85c4737ee2cb897bce62c
|
Provenance
The following attestation bundles were made for pubmed_proto-0.1.0.tar.gz:
Publisher:
release.yml on populationgenomics/pubmed-proto
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pubmed_proto-0.1.0.tar.gz -
Subject digest:
a375416097ee17dbd4f099b434e0854af4feafe7d51f14902bda2ecf49e58797 - Sigstore transparency entry: 2084596865
- Sigstore integration time:
-
Permalink:
populationgenomics/pubmed-proto@5d58239573785b6feaab900ea87e323438161469 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/populationgenomics
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@5d58239573785b6feaab900ea87e323438161469 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pubmed_proto-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pubmed_proto-0.1.0-py3-none-any.whl
- Upload date:
- Size: 33.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38335b12ee2a8ef5eb457e96b23aea17f541c5d350883137da430122123e2a2d
|
|
| MD5 |
60ee62f74bae90441f021ade84dba2d9
|
|
| BLAKE2b-256 |
735daf6110a908be0e508ea7abf5fd4104a4dd8692bb8dedf8318562c4970175
|
Provenance
The following attestation bundles were made for pubmed_proto-0.1.0-py3-none-any.whl:
Publisher:
release.yml on populationgenomics/pubmed-proto
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pubmed_proto-0.1.0-py3-none-any.whl -
Subject digest:
38335b12ee2a8ef5eb457e96b23aea17f541c5d350883137da430122123e2a2d - Sigstore transparency entry: 2084596893
- Sigstore integration time:
-
Permalink:
populationgenomics/pubmed-proto@5d58239573785b6feaab900ea87e323438161469 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/populationgenomics
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@5d58239573785b6feaab900ea87e323438161469 -
Trigger Event:
release
-
Statement type: