MeSH
The Mesh client (bibliofabric.mesh) reads the MeSH RDF
API of the National Library of Medicine:
Medical Subject Headings descriptors, qualifiers, entry terms, and
supplementary concept records. It covers the lookup service, the per-id
record routes, and the SPARQL endpoint on id.nlm.nih.gov. Calls return
typed Pydantic models through an async client and a generated sync twin.
Credentials and terms
The API needs no credentials. The acceptable use
policy caps the
SPARQL endpoint at 500 requests per minute per IP address; the client
paces itself to 2 requests per second with 2 concurrent requests, well
under that cap. Tune both with MESH_MAX_REQUESTS_PER_SECOND and
MESH_MAX_CONCURRENCY; see the settings guide.
The constructor also takes a keyword-only mailto= contact address; it
overrides MESH_MAILTO and appears in the User-Agent the client sends.
NLM provides MeSH data freely under its Terms and Conditions for MeSH. The conditions ask you to acknowledge NLM as the data source, not to imply NLM endorsement of your product, and to state the MeSH version when you redistribute data that is not the most current one.
Quick start
import asyncio
from bibliofabric.mesh import MeshClient
async def main() -> None:
async with MeshClient() as client:
hits = await client.lookup.descriptors("asthma", match="startswith")
for hit in hits[:3]:
print(hit.resource, hit.label)
record = await client.records.get("D000001")
print(
record.identifier, record.record_type.rsplit("#", 1)[-1], record.label_text
)
result = await client.sparql.query(
"SELECT ?d ?l WHERE { ?d a <http://id.nlm.nih.gov/mesh/vocab#TopicalDescriptor> . "
"?d <http://www.w3.org/2000/01/rdf-schema#label> 'Asthma'@en . "
"?d <http://www.w3.org/2000/01/rdf-schema#label> ?l . "
"FILTER(REGEX(STR(?d), '^http://id.nlm.nih.gov/mesh/[A-Z]')) }"
)
for row in result.rows():
print(row)
asyncio.run(main())
http://id.nlm.nih.gov/mesh/D001249 Asthma
D000001 TopicalDescriptor Calcimycin
{'d': 'http://id.nlm.nih.gov/mesh/D001249', 'l': 'Asthma'}
Sync quick start
from bibliofabric.mesh import MeshSyncClient
with MeshSyncClient() as client:
for hit in client.lookup.terms("heart attack", match="contains", limit=5):
print(hit.resource, hit.label)
details = client.lookup.details("D000001")
print(
details.descriptor,
len(details.terms),
"terms",
len(details.qualifiers),
"qualifiers",
)
Use the sync client in scripts, notebooks, or threads; it never starts or touches an event loop. Both variants take the same parameters and raise the same errors.
What the client offers
| Attribute | Returns | Methods |
|---|---|---|
client.lookup |
LookupHit lists, one DescriptorDetails |
descriptors, terms, pairs, qualifiers, details, years, labels |
client.records |
MeshRecord |
get, exists |
client.sparql |
SparqlResult, plain text, or binding rows |
query, query_post, query_text, iterate |
The lookup routes answer unpaged arrays, so lookup has no framework
search(); each method is one route. limit accepts 1 to 50 (the API
default is 10) and match accepts exact (the API default), contains,
and startswith; other values raise ConfigurationError before a request.
records.get() accepts the bare id (D000001), the full resource URI, and
the id with a .json suffix. sparql.query() returns the typed SPARQL 1.1
JSON answer for SELECT and ASK queries; CONSTRUCT queries read through
sparql.query_text() in JSON, XML, CSV, TSV, RDF/XML, JSON-LD, TURTLE, or
N3.
The annual RDF/XML dump
NLM publishes the full vocabulary as an N-Triples file
(mesh.nt.gz) under https://nlmpubs.nlm.nih.gov/projects/mesh/rdf/, with
yearly archives. The file holds every descriptor, qualifier, term, and
supplementary concept record of one MeSH year, and it is far too large to
keep as a test fixture, so the client ships no loader. Download it directly
when a task needs the whole vocabulary; the SPARQL endpoint and the record
routes serve the same data per id.
Examples
Runnable scripts live in packages/bibliofabric/examples/mesh:
quickstart.py: label lookups, one record, and a SPARQL query.sync_quickstart.py: the same calls without asyncio.
Next
- Upstream quirks: versioned graph URIs, empty documents for missing ids, and other live behavior.
- API reference: every public class generated from the source.