Skip to content

API reference

Generated from the source with mkdocstrings.

Client

bibliofabric.mesh.MeshClient

Client for the MeSH RDF API: lookups, records, and SPARQL.

The API needs no credentials and serves every route from one host; the client sends Accept: application/json on every request (the record routes answer application/rdf+json regardless, and the SPARQL resource pins its own representation per call).

lookup: LookupResource cached property

The /lookup routes: labels to resource URIs, and back.

records: RecordsResource cached property

The /{id}.json record routes.

sparql: SparqlResource cached property

The SPARQL endpoint at /sparql.

The sync twin MeshSyncClient mirrors the client method for method and exports from the same module: from bibliofabric.mesh import MeshSyncClient.

Settings

bibliofabric.mesh.MeshSettings

MeSH-specific client settings.

Inherits timeout, max_retries, max_retry_wait, max_requests_per_second, max_concurrency, user_agent and mailto from :class:bibliofabric.config.BaseApiSettings and sets MeSH defaults (2 req/s, concurrency 2). The API needs no credentials.

Resources

bibliofabric.mesh.LookupResource

Client for the lookup routes of the MeSH RDF API.

The label lookups are descriptors(), terms(), and pairs(); qualifiers() lists the qualifiers a descriptor allows, details() returns the descriptor record overview, years() lists the queryable MeSH years, and labels() reads the labels of one resource URI. The routes answer unpaged arrays, so the framework paging methods do not apply.

descriptors(label: str, *, match: str = 'exact', year: str | None = None, limit: int | None = None) -> list[LookupHit] async

Find descriptors (headings) whose preferred label matches.

Parameters:

Name Type Description Default
label str

The label text to match, case-insensitively.

required
match str

exact (the API default), contains, or startswith.

'exact'
year str | None

Query a past MeSH year ("2026", "2025", "2024") instead of the current vocabulary; client.lookup.years() lists the values the API accepts.

None
limit int | None

Cap on hits, 1 to 50 (the API default is 10).

None

Returns:

Type Description
list[LookupHit]

Hits ordered as the API ranks them; an empty list matches nothing.

terms(label: str, *, match: str = 'exact', limit: int | None = None) -> list[LookupHit] async

Find entry terms (synonyms and variants) whose label matches.

pairs(label: str, descriptor: str, *, match: str = 'exact', limit: int | None = None) -> list[LookupHit] async

Find descriptor-qualifier pairs (subheadings) for one descriptor.

label matches the qualifier label; descriptor accepts the bare id ("D001249") or the full URI. A pair resource id joins both ids (D001249Q000188) and the label joins both labels (Asthma/drug therapy).

qualifiers(descriptor: str) -> list[LookupHit] async

List the qualifiers (subheadings) the descriptor allows.

details(descriptor: str, *, includes: str | None = None) -> DescriptorDetails async

Return the descriptor overview: entry terms, qualifiers, and links.

includes forwards the API's includes selector (for example "terms,seealso,qualifiers"); without it the route answers all three lists.

years() -> list[str] async

Return the MeSH years the API can query, current first.

labels(resource: str) -> list[str] async

Return the labels of one resource URI.

resource accepts the bare id ("D000001") or the full URI. The route answers label strings, not hit objects.

bibliofabric.mesh.RecordsResource

Client for the record routes: one MeSH resource per id.

get("D000001") answers the descriptor, get("Q000188") a qualifier, get("T009957") an entry term, and get("C063233") a supplementary concept. The route serves RDF as JSON-LD (application/rdf+json); the predicates stay unmodeled under their JSON-LD keys on :class:~bibliofabric.mesh.models.MeshRecord.

The route answers HTTP 200 with an empty JSON object for ids that carry no data in the queried graph (unknown or retired ids), so the client raises :class:NotFoundError for those answers instead of validating an empty record.

exists(id: str | int) -> bool async

Return True when the id carries record data.

The probe sends one GET; HEAD cannot decide existence because the route answers HTTP 200 with an empty document for missing ids.

bibliofabric.mesh.SparqlResource

Client for the MeSH SPARQL endpoint.

The endpoint shares the host of the lookup and record routes, so the resource runs through the main client's limiter and settings. A SELECT answer carries head and results; an ASK answer carries boolean. The endpoint caps SELECT answers at 1000 rows per request and pages the rest through offset.

Malformed queries answer HTTP 500 with a plain-text parser message; that raises :class:~bibliofabric.exceptions.APIError.

query(query: str, *, inference: bool = False, offset: int = 0, limit: int = SPARQL_LIMIT_MAX, year: str | None = None, timeout: float | None = None) -> SparqlResult async

Run a SELECT or ASK query with GET and return the typed answer.

Parameters:

Name Type Description Default
query str

The SPARQL query string.

required
inference bool

True adds the subclasses and subproperties of every class and property the query names.

False
offset int

First row to return; page through larger result sets.

0
limit int

Rows per request, 1 to 1000 (the endpoint default).

SPARQL_LIMIT_MAX
year str | None

Query a past MeSH graph ("2026" and back) instead of the current one; client.lookup.years() lists the values.

None
timeout float | None

Per-request timeout override in seconds.

None

Returns:

Type Description
SparqlResult

The SPARQL 1.1 JSON answer as a

SparqlResult

class:~bibliofabric.sparql.SparqlResult; use rows() for

SparqlResult

plain variable -> value dictionaries.

query_post(query: str, *, inference: bool = False, offset: int = 0, limit: int = SPARQL_LIMIT_MAX, year: str | None = None, timeout: float | None = None) -> SparqlResult async

Run a SELECT or ASK query with POST and return the typed answer.

Long queries belong in a POST body, where URL length limits do not apply. The POST only reads, so the request carries the framework's transient-failure retries.

query_text(query: str, *, output_format: str = 'CSV', inference: bool = False, year: str | None = None, timeout: float | None = None) -> str async

Run a query and return the raw body in the requested format.

output_format accepts JSON, XML, CSV, TSV (for SELECT and ASK) and RDF/XML, JSON-LD, TURTLE, N3 (for CONSTRUCT). Only :meth:query and :meth:query_post parse the typed JSON shape; the body here passes through as text.

iterate(query: str, *, page_size: int = SPARQL_LIMIT_MAX, limit: int | None = None, inference: bool = False, year: str | None = None, timeout: float | None = None) -> AsyncIterator[SparqlBinding] async

Iterate over the rows of a SELECT query, 1000 per request.

The walk pages through the endpoint's offset parameter and stops on a page that carries fewer rows than page_size. The query must not carry its own LIMIT or OFFSET: the walker sets both. limit caps the total number of rows; limit=0 sends no request.

Models

bibliofabric.mesh.MeshRecord

One MeSH record from /{id}.json (RDF served as JSON-LD).

@id names the resource and @type its vocabulary class (vocab#TopicalDescriptor, vocab#Qualifier, vocab#Term, vocab#SCR_Chemical, and their siblings). Every predicate stays under its JSON-LD key through extra="allow"; values are plain strings, numbers, booleans, :class:JsonLdText literals, lists of resource URI strings, or lists of literals.

A descriptor record carries identifier and label at the top level; a term record carries prefLabel instead of label, so read the label through :attr:label_text.

label_text: str | None property

Return the record label as plain text, or None when absent.

Reads label (descriptors, qualifiers, supplementary concepts) and falls back to prefLabel (terms); both are JSON-LD literals.

bibliofabric.mesh.LookupHit

One hit of a lookup route: the resource URI and its preferred label.

bibliofabric.mesh.DescriptorDetails

The lookup/details answer: the descriptor, its terms, and its links.

bibliofabric.mesh.JsonLdText

One language-tagged JSON-LD literal ({"@language": ..., "@value": ...}).

Helpers

bibliofabric.mesh.normalize_mesh_id(id: str) -> str

Return the bare MeSH id of id.

Accepts the bare accession ("D000001"), the full resource URI ("http://id.nlm.nih.gov/mesh/D000001"), and both with a format suffix ("D000001.json"). Raises :class:ConfigurationError for anything else.

SPARQL answers use the shared SparqlResult models from bibliofabric.sparql; the client re-exports them.