Adding a Custom Reference Source
This guide shows how to add support for new reference types. There are two approaches:
- YAML Configuration (recommended) - Define sources via config files, no Python required
- Python Plugin - Create a custom Python class for complex sources
YAML Configuration (Recommended)
For sources that expose a JSON API, you can define them entirely through YAML configuration. No Python code required, no pull requests needed.
Understanding the Config Files
The validator uses two separate config files:
| File | Purpose |
|---|---|
.linkml-reference-validator.yaml |
Main config: cache settings, skip_prefixes, rate limiting |
.linkml-reference-validator-sources.yaml |
Custom sources: JSON API definitions |
Quick Start
Create a file named .linkml-reference-validator-sources.yaml in your project root:
sources:
MGNIFY:
url_template: "https://www.ebi.ac.uk/metagenomics/api/v1/studies/{id}"
fields:
title: "$.data.attributes.study-name"
content: "$.data.attributes.study-abstract"
id_patterns:
- "^MGYS\\d+$"
Now you can validate MGnify references:
linkml-reference-validator validate text \
"The American Gut Project" \
MGNIFY:MGYS00000596
Configuration File Locations
Sources are loaded from these locations. If the same prefix is defined in more than one place, the most specific definition wins (highest precedence first):
- A dedicated sources file passed explicitly (
sources_file) sources:section in your main config file.linkml-reference-validator-sources.yaml(or.yml) in the current directory (project-level; if both exist,.yamlwins)~/.config/linkml-reference-validator/sources/*.yamland*.yml(user-level; files are read in alphabetical order and a later file overrides an earlier one)
So a project file overrides your personal defaults, and a file you pass explicitly overrides both. Prefixes defined in only one place are always available.
Source Configuration Fields
| Field | Required | Description |
|---|---|---|
url_template |
Yes | API URL with {id} placeholder |
fields |
JSON sources | JSONPath expressions mapping to title, content, etc. content may also be a list or a labelled mapping (see Quoting several fields) |
id_patterns |
No | Regex patterns for bare ID matching |
headers |
No | HTTP headers (supports ${ENV_VAR} interpolation) |
store_raw_response |
No | Store full API response in metadata |
content_type |
No | abstract_only or full_text; see Quoting several fields for the default |
format |
No | json (the default), or html / xml for a page source; see Page sources |
title |
No | Page sources only: a title template with {id}, e.g. "21 CFR {id}" |
Field Mappings
Use JSONPath expressions (using jsonpath-ng syntax) to extract fields from the API response:
sources:
EXAMPLE:
url_template: "https://api.example.com/items/{id}"
fields:
title: "$.name" # Simple field
content: "$.description" # Abstract/description
year: "$.published_date" # Optional: publication year
authors: "$.authors[0].name" # Optional: author info
Standard field names: title, content, year, authors, journal, doi
JSONPath Examples
Common JSONPath patterns:
| Pattern | Description |
|---|---|
$.title |
Top-level field |
$.data.attributes.name |
Nested field |
$.items[0] |
First array element |
$.results[*].name |
All names in array |
ID Pattern Matching
Use id_patterns to match bare IDs (without prefix):
sources:
MGNIFY:
url_template: "https://www.ebi.ac.uk/metagenomics/api/v1/studies/{id}"
fields:
title: "$.data.attributes.study-name"
content: "$.data.attributes.study-abstract"
id_patterns:
- "^MGYS\\d+$" # Matches MGYS00000596
- "^MGY[A-Z]\\d+$" # Matches MGYA123456
With this config, both formats work:
# With prefix
linkml-reference-validator validate text "quote" MGNIFY:MGYS00000596
# Bare ID (matched by pattern)
linkml-reference-validator validate text "quote" MGYS00000596
Authentication
For APIs requiring authentication, use environment variables:
sources:
PRIVATE_API:
url_template: "https://api.example.com/records/{id}"
fields:
title: "$.title"
content: "$.body"
headers:
Authorization: "Bearer ${API_TOKEN}"
X-API-Key: "${API_KEY}"
Set the environment variable before running:
export API_TOKEN="your-secret-token"
linkml-reference-validator validate text "quote" PRIVATE_API:123
Quoting several fields
A single JSONPath for content keeps only its first match. When the facts you
want to quote are spread across a record, give content a mapping of labels
to JSONPaths. Each field becomes one Label: value line, in the order
written, and a quote can cite any line, label included.
sources:
FDA_PRODUCT_CODE:
url_template: "https://api.fda.gov/device/classification.json?search=product_code:{id}"
fields:
title: "$.results[0].device_name"
content:
Device name: "$.results[0].device_name"
Device class: "$.results[0].device_class"
Regulation number: "$.results[0].regulation_number"
Definition: "$.results[0].definition"
FDA_PRODUCT_CODE:EBF is then cached as
Device name: Material, Tooth Shade, Resin
Device class: 2
Regulation number: 872.3690
and Device class: 2 validates against it. EBF's definition is empty,
so its line is left out, label and all.
- A list of JSONPaths instead of a mapping gives one bare value per line.
- Each path is read as
source_extra_fieldsreads one: its first match. A path that selects a list gives its items joined with spaces (for example,"$.results[0].openfda.k_number"). A wildcard such as$.results[*].namegives only its first match, so list each item's path to quote them all. - Point each path at a value or a list of values. A path that matches an
object is written as Python prints it (
{'device_name': ...}). - Only
contenttakes a list or mapping, and every JSONPath infieldsmust parse. A source with a malformed entry, an invalid path, or an unknowncontent_typeis skipped with a warning naming the problem. Before this check, a source with a bad path in a field such asyearstill loaded and that field came back empty; now the whole prefix is missing until the path is fixed.
Content type. A single-path content is cached as abstract_only, as it
always has been. A list or mapping is a record rather than an abstract, so it
is cached as full_text: a quote that fails is not told "only abstract
available", and no full-text lookup is attempted for it. Set content_type
to override either default.
Extra fields. source_extra_fields is also honoured for a configured source, keyed by its prefix. Its fields are
appended under ### name headings, as for DOI and PMID:
# .linkml-reference-validator.yaml
validation:
source_extra_fields:
FDA_PRODUCT_CODE:
regulation: "$.results[0].regulation_number"
Changing fields does not refresh the cache. An entry already cached keeps
the text it was cached with. After changing a source's fields or its
source_extra_fields, re-fetch the entries you rely on with
linkml-reference-validator cache reference PREFIX:ID --force.
Page sources (HTML or XML)
Some references are web pages rather than JSON records. A url: reference
can already quote them, but it is cited by its full URL. Set format: html
or format: xml to cite such a page by a short identifier instead: {id}
fills the URL template, and the response is read exactly as a url: page is
(see Validating URLs). HTML and XML are flattened to
readable text, so a quote can run across an inline tag, and a PDF is read for
its text.
sources:
CFR:
format: html
url_template: "https://www.ecfr.gov/api/renderer/v1/content/enhanced/current/title-21?section={id}"
title: "21 CFR {id}"
Now CFR:872.3275 fetches that section of the Code of Federal Regulations,
and
linkml-reference-validator validate text \
"Identification. Zinc oxide-eugenol is a device composed of zinc oxide-eugenol" \
CFR:872.3275
validates, though the page sets Identification. in italics.
formatsays how to read a response whose content type and first bytes say neither, such as XML served astext/plainwith no<?xmldeclaration. A response that does say what it is (text/html, an XML declaration, a doctype) is read as what it says, whichever format you declared.htmlandxmlare the only page formats; case doesn't matter.titleis a template with{id}. Without it, the title is the page'scitation_titleor<title>, and otherwise its URL, as forurl:. To put a literal brace in a title, double it:{{or}}.- Only
{id}is filled in, in both templates, andurl_templatemust use it. A source whose template names any other placeholder, or whose URL has no{id}, is skipped with a warning. - The host is fixed.
url_templatemust start withhttp://orhttps://, and{id}may only appear in the path or the query (not the#fragment, which is never sent). The id is URL-encoded (keeping/and:), soCFR:872.3275&part=1asks for that odd section rather than adding apartparameter. An id with a.or..path segment is not fetched, so it can't reach another path on the host. id_patternsworks as for JSON sources.- Not taken:
fields,headers,store_raw_responseandcontent_typebelong to JSON sources. A page source that sets one is skipped with a warning rather than having it silently ignored, so credentials inheadersare never quietly left out. Pages that need request headers are not supported yet. - The cache. Entries are cached under the short id, with the page URL as
full_text_url. An HTML or XML page carries the sameurl_source_versionstamp as aurl:page, so a change to how pages are read refreshes both. A page source that answers with a PDF is cached asfull_text_pdfwithout that stamp, as other PDF text is. A page source and a JSON source may share a prefix in different files; the usual precedence picks one (Configuration File Locations).
Storing Raw Response
Enable store_raw_response to capture the full API response in metadata:
sources:
MGNIFY:
url_template: "https://www.ebi.ac.uk/metagenomics/api/v1/studies/{id}"
fields:
title: "$.data.attributes.study-name"
content: "$.data.attributes.study-abstract"
store_raw_response: true
The raw response is saved in the cache file's metadata for later inspection.
Complete Example: MGnify
Here's a complete configuration for MGnify (EBI Metagenomics):
# .linkml-reference-validator-sources.yaml
sources:
MGNIFY:
url_template: "https://www.ebi.ac.uk/metagenomics/api/v1/studies/{id}"
id_patterns:
- "^MGYS\\d+$"
fields:
title: "$.data.attributes.study-name"
content: "$.data.attributes.study-abstract"
headers:
Accept: "application/json"
store_raw_response: true
Test it:
# Cache the reference
linkml-reference-validator cache reference MGNIFY:MGYS00000596
# Validate a quote
linkml-reference-validator validate text \
"The American Gut project is the largest crowdsourced citizen science project" \
MGNIFY:MGYS00000596
Complete Example: BioStudies
sources:
BIOSTUDIES:
url_template: "https://www.ebi.ac.uk/biostudies/api/v1/studies/{id}"
id_patterns:
- "^S-[A-Z]+\\d+$"
fields:
title: "$.title"
content: "$.description"
Complete Setup: Both Config Files
Here's a typical project setup showing both config files working together. The pattern is: skip what you can't support, configure what you can.
.linkml-reference-validator.yaml (main config):
# Main validation settings
cache_dir: references_cache
rate_limit_delay: 0.5
# Skip prefixes that have no API or aren't needed
skip_prefixes:
- SRA # No abstract API available
- ARRAYEXPRESS # Deprecated, use BioStudies
# Note: Once you add a custom source for a prefix (like MGNIFY below),
# remove it from skip_prefixes - validation will now work properly.
.linkml-reference-validator-sources.yaml (custom sources):
# Custom JSON API sources
sources:
MGNIFY:
url_template: "https://www.ebi.ac.uk/metagenomics/api/v1/studies/{id}"
id_patterns:
- "^MGYS\\d+$"
fields:
title: "$.data.attributes.study-name"
content: "$.data.attributes.study-abstract"
BIOSTUDIES:
url_template: "https://www.ebi.ac.uk/biostudies/api/v1/studies/{id}"
id_patterns:
- "^S-[A-Z]+\\d+$"
fields:
title: "$.title"
content: "$.description"
With this setup:
- PMID:12345 - Uses built-in PubMed source
- MGNIFY:MGYS00000596 - Uses your custom MGnify source
- SRA:SRP123456 - Skipped (returns valid with INFO message)
A source you define with the same prefix as a built-in one (such as
WIKIPEDIA) takes precedence over the built-in source for that prefix. Its
id_patterns are still tried only after every other built-in source.
Finding the Right JSONPath
To figure out the correct JSONPath for a new API:
-
Fetch the API response directly:
bash curl -s "https://api.example.com/items/123" | jq . -
Identify the fields you need (title, description/abstract)
-
Write the JSONPath:
$.fieldnamefor top-level fields$.parent.childfor nested fields$.array[0]for array elements
Python Plugin (Advanced)
For sources requiring custom logic (XML parsing, multiple API calls, etc.), create a Python plugin.
Overview
Each reference source is a Python class that:
- Inherits from
ReferenceSource - Implements
prefix()andfetch()methods - Registers itself with the
ReferenceSourceRegistry
Entrez Summary Sources (Recommended for NCBI IDs)
If your source is backed by NCBI Entrez, prefer the built-in EntrezSummarySource
base class. It provides shared rate limiting, email configuration, and summary parsing.
# src/linkml_reference_validator/etl/sources/my_entrez.py
"""Entrez summary source example."""
from linkml_reference_validator.etl.sources.entrez import EntrezSummarySource
from linkml_reference_validator.etl.sources.base import ReferenceSourceRegistry
@ReferenceSourceRegistry.register
class ExampleEntrezSource(EntrezSummarySource):
"""Fetch summaries from an Entrez database."""
PREFIX = "EXAMPLE"
ENTREZ_DB = "example_db"
TITLE_FIELDS = ("title", "name")
CONTENT_FIELDS = ("summary", "description")
ID_PATTERNS = (r"^EX\\d+$",)
TITLE_FIELDS and CONTENT_FIELDS are checked in order, and the first non-empty value
is used for the ReferenceContent.
Step 1: Create the Source Class
Create a new file in src/linkml_reference_validator/etl/sources/:
# src/linkml_reference_validator/etl/sources/arxiv.py
"""arXiv reference source."""
import logging
from typing import Optional
from linkml_reference_validator.models import ReferenceContent, ReferenceValidationConfig
from linkml_reference_validator.etl.sources.base import ReferenceSource, ReferenceSourceRegistry
logger = logging.getLogger(__name__)
@ReferenceSourceRegistry.register
class ArxivSource(ReferenceSource):
"""Fetch references from arXiv."""
@classmethod
def prefix(cls) -> str:
"""Return the prefix this source handles."""
return "arxiv"
def fetch(
self, identifier: str, config: ReferenceValidationConfig
) -> Optional[ReferenceContent]:
"""Fetch a paper from arXiv.
Args:
identifier: arXiv ID (e.g., '2301.07041')
config: Configuration for fetching
Returns:
ReferenceContent if successful, None otherwise
"""
# Your implementation here
# Fetch from arXiv API, parse response, return ReferenceContent
...
Step 2: Implement the fetch() Method
The fetch() method should:
- Accept an identifier (without the prefix)
- Fetch content from the external source
- Return a
ReferenceContentobject orNoneon failure
def fetch(
self, identifier: str, config: ReferenceValidationConfig
) -> Optional[ReferenceContent]:
"""Fetch a paper from arXiv."""
import requests
import time
arxiv_id = identifier.strip()
# Respect rate limiting
time.sleep(config.rate_limit_delay)
# Fetch from arXiv API
url = f"http://export.arxiv.org/api/query?id_list={arxiv_id}"
response = requests.get(url, timeout=30)
if response.status_code != 200:
logger.warning(f"Failed to fetch arxiv:{arxiv_id}")
return None
# Parse the response (arXiv returns Atom XML)
title, authors, abstract = self._parse_arxiv_response(response.text)
return ReferenceContent(
reference_id=f"arxiv:{arxiv_id}",
title=title,
content=abstract,
content_type="abstract_only",
authors=authors,
)
Step 3: Register the Source
Add the import to src/linkml_reference_validator/etl/sources/__init__.py:
from linkml_reference_validator.etl.sources.arxiv import ArxivSource
__all__ = [
# ... existing exports
"ArxivSource",
]
Step 4: Write Tests
Create tests in tests/test_sources.py:
class TestArxivSource:
"""Tests for ArxivSource."""
@pytest.fixture
def source(self):
return ArxivSource()
def test_prefix(self, source):
assert source.prefix() == "arxiv"
def test_can_handle(self, source):
assert source.can_handle("arxiv:2301.07041")
assert not source.can_handle("PMID:12345")
@patch("linkml_reference_validator.etl.sources.arxiv.requests.get")
def test_fetch(self, mock_get, source, config):
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.text = """...""" # Mock arXiv XML
mock_get.return_value = mock_response
result = source.fetch("2301.07041", config)
assert result is not None
assert result.reference_id == "arxiv:2301.07041"
Reference: ReferenceContent Fields
The ReferenceContent model has these fields:
| Field | Type | Description |
|---|---|---|
reference_id |
str |
Full reference ID with prefix (e.g., arxiv:2301.07041) |
title |
Optional[str] |
Title of the reference |
content |
Optional[str] |
Main text content for validation |
content_type |
str |
Type indicator (e.g., abstract_only, full_text) |
authors |
Optional[list[str]] |
List of author names |
journal |
Optional[str] |
Journal/venue name |
year |
Optional[str] |
Publication year |
doi |
Optional[str] |
DOI if available |
metadata |
dict |
Additional metadata (raw API response, etc.) |
Tips
- Rate limiting: Always respect
config.rate_limit_delaybetween API calls - Error handling: Return
Noneon failures, don't raise exceptions - Logging: Use
logger.warning()for failures to aid debugging - Caching: The
ReferenceFetcherhandles caching automatically - your source just needs to fetch - Testing: Mock external API calls in tests to avoid network dependencies