Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(pubmed): keep transient Europe PMC lookup failures retryable (#8024)

LearningCircuit221013d6c825
diff --git a/changelog.d/8019.bugfix.md b/changelog.d/8019.bugfix.md
new file mode 100644
index 0000000000..e932aa8be7
--- /dev/null
+++ b/changelog.d/8019.bugfix.md
@@ -0,0 +1 @@
+When a PubMed article's Europe PMC lookup is rate limited (HTTP 429) or hits a server error (5xx), and the PMC fallback finds nothing, the PubMed downloader now reports the lookup's HTTP status instead of "article may be paywalled". The download service still runs the generic downloader after PubMed, and its later result usually decides the recorded outcome. The difference is that a permanent generic 403 or 404 no longer wins over the transient lookup failure, so the paper stays retryable instead of being marked permanently failed.
diff --git a/src/local_deep_research/research_library/downloaders/pubmed.py b/src/local_deep_research/research_library/downloaders/pubmed.py
index 32d94eaca9..859c50e5b0 100644
--- a/src/local_deep_research/research_library/downloaders/pubmed.py
+++ b/src/local_deep_research/research_library/downloaders/pubmed.py
@@ -147,7 +147,12 @@ class PubMedDownloader(BaseDownloader):
pmid = pmid_match.group(1)
logger.info(f"Processing PubMed article: {pmid}")
- # Check if article is open access via Europe PMC
+ # Check if article is open access via Europe PMC. A 429 or 5xx
+ # says nothing about the article's access status, so keep the
+ # status: if the PMC fallback below also finds nothing, it is
+ # reported instead of a paywall guess, which the failure
+ # classifier would record as permanent (#8019).
+ lookup_status: Optional[int] = None
try:
api_url = (
"https://www.ebi.ac.uk/europepmc/webservices/rest/search"
@@ -156,7 +161,15 @@ class PubMedDownloader(BaseDownloader):
response = self.session.get(api_url, params=params, timeout=10)
- if response.status_code == 200:
+ if response.status_code == 429 or (
+ 500 <= response.status_code <= 599
+ ):
+ lookup_status = response.status_code
+ logger.warning(
+ f"Europe PMC lookup failed for PMID {pmid} "
+ f"(HTTP {lookup_status})"
+ )
+ elif response.status_code == 200:
data = response.json()
results = data.get("resultList", {}).get("result", [])
@@ -215,6 +228,20 @@ class PubMedDownloader(BaseDownloader):
skip_reason=f"PMC version exists but PDF not accessible (PMC ID: {pmc_id})"
)
+ if lookup_status == 429:
+ return DownloadResult(
+ skip_reason="Rate limited (429) by Europe PMC - retry later",
+ status_code=429,
+ )
+ if lookup_status is not None:
+ return DownloadResult(
+ skip_reason=(
+ f"Europe PMC lookup failed (HTTP {lookup_status}) "
+ "- retry later"
+ ),
+ status_code=lookup_status,
+ )
+
return DownloadResult(
skip_reason="No free full-text available - article may be paywalled"
)
diff --git a/tests/research_library/downloaders/test_pubmed_transient_lookup.py b/tests/research_library/downloaders/test_pubmed_transient_lookup.py
new file mode 100644
index 0000000000..0d60036a23
--- /dev/null
+++ b/tests/research_library/downloaders/test_pubmed_transient_lookup.py
@@ -0,0 +1,145 @@
+"""PubMed: a transient Europe PMC lookup failure stays retryable (#8019).
+
+When the Europe PMC search answered 429 or a 5xx and the NCBI PMC fallback
+found nothing, the downloader used to end with "No free full-text available
+- article may be paywalled" and no status code. The word "paywalled" is what
+``FailureClassifier`` matches to record a permanent ``paywall_or_login``
+failure, so a rate-limited or briefly unavailable lookup was never retried.
+"""
+
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from local_deep_research.library.download_management.failure_classifier import (
+ FailureClassifier,
+)
+from local_deep_research.research_library.downloaders.base import (
+ ContentType,
+)
+from local_deep_research.research_library.downloaders.pubmed import (
+ PubMedDownloader,
+)
+
+PUBMED_URL = "https://pubmed.ncbi.nlm.nih.gov/12345678/"
+
+
+def downloader():
+ return PubMedDownloader(timeout=30, rate_limit_delay=0)
+
+
+def _response(status_code):
+ response = MagicMock()
+ response.status_code = status_code
+ response.text = ""
+ return response
+
+
+def _classify(result):
+ return FailureClassifier().classify_failure(
+ "download_failed",
+ status_code=result.status_code,
+ url=PUBMED_URL,
+ details=result.skip_reason,
+ )
+
+
+def test_rate_limited_lookup_is_reported_as_429(downloader):
+ """Every HTTP call answers 429: the Europe PMC search, the NCBI elink
+ lookup and the PubMed page fallback, so no PMC ID is found.
+
+ Revert-catcher: without the carried status the result is the status-less
+ "may be paywalled" reason, classified as permanent ``paywall_or_login``.
+ """
+ with patch.object(
+ downloader.session, "get", return_value=_response(429)
+ ) as mock_get:
+ result = downloader.download_with_result(PUBMED_URL, ContentType.PDF)
+
+ assert mock_get.call_count >= 1
+ assert result.is_success is False
+ assert result.status_code == 429
+ assert "429" in result.skip_reason
+ assert "paywall" not in result.skip_reason.lower()
+
+ failure = _classify(result)
+ assert failure.error_type == "rate_limited"
+ assert not failure.is_permanent()
+
+
[email protected]("status_code", [500, 502, 503, 504])
+def test_server_error_lookup_carries_its_status(downloader, status_code):
+ """Revert-catcher: a 5xx from the Europe PMC search used to be dropped
+ and the attempt recorded as a permanent paywall."""
+ with patch.object(
+ downloader.session, "get", return_value=_response(status_code)
+ ):
+ result = downloader.download_with_result(PUBMED_URL, ContentType.PDF)
+
+ assert result.is_success is False
+ assert result.status_code == status_code
+ assert f"HTTP {status_code}" in result.skip_reason
+ assert "paywall" not in result.skip_reason.lower()
+
+ failure = _classify(result)
+ assert failure.error_type != "paywall_or_login"
+ assert not failure.is_permanent()
+
+
+def test_rate_limited_lookup_still_tries_the_pmc_fallback(downloader):
+ """Guards existing behaviour; not a revert-catcher for #8019 (it passes
+ with or without the fix). A failed lookup must not short-circuit: the
+ NCBI PMC ID fallback can still deliver the PDF."""
+ with (
+ patch.object(downloader.session, "get", return_value=_response(429)),
+ patch.object(
+ downloader, "_get_pmc_id_from_pmid", return_value="PMC4242"
+ ),
+ patch.object(
+ downloader, "_download_via_europe_pmc", return_value=b"%PDF-ok"
+ ) as mock_europe_pmc,
+ ):
+ result = downloader.download_with_result(PUBMED_URL, ContentType.PDF)
+
+ assert result.is_success is True
+ assert result.content == b"%PDF-ok"
+ mock_europe_pmc.assert_called_once_with("PMC4242")
+
+
+def test_pmc_fallback_failure_keeps_its_own_reason(downloader):
+ """Guards existing behaviour; not a revert-catcher for #8019 (it passes
+ with or without the fix). When the fallback finds a PMC ID but cannot
+ fetch the PDF, that more specific reason is kept and the lookup status
+ is not reported."""
+ with (
+ patch.object(downloader.session, "get", return_value=_response(503)),
+ patch.object(
+ downloader, "_get_pmc_id_from_pmid", return_value="PMC4242"
+ ),
+ patch.object(downloader, "_download_via_europe_pmc", return_value=None),
+ patch.object(downloader, "_download_via_ncbi_pmc", return_value=None),
+ ):
+ result = downloader.download_with_result(PUBMED_URL, ContentType.PDF)
+
+ assert result.is_success is False
+ assert "PMC4242" in result.skip_reason
+
+
+def test_not_open_access_answer_is_still_a_paywall(downloader):
+ """Guards existing behaviour; not a revert-catcher for #8019 (it passes
+ with or without the fix). A successful lookup that says the article is
+ not open access remains the permanent subscription classification."""
+ response = _response(200)
+ response.json.return_value = {
+ "resultList": {
+ "result": [{"isOpenAccess": "N", "journalTitle": "Some Journal"}]
+ }
+ }
+ with patch.object(downloader.session, "get", return_value=response):
+ result = downloader.download_with_result(PUBMED_URL, ContentType.PDF)
+
+ assert result.status_code is None
+ failure = _classify(result)
+ assert failure.error_type == "paywall_or_login"
+ assert failure.is_permanent()