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(library): stop classifying HTML responses as paywalls (#6410)

LearningCircuit99dae4086312
diff --git a/changelog.d/+html-download-failure-classification.bugfix.md b/changelog.d/+html-download-failure-classification.bugfix.md
new file mode 100644
index 0000000000..aa22bea51d
--- /dev/null
+++ b/changelog.d/+html-download-failure-classification.bugfix.md
@@ -0,0 +1 @@
+Report ordinary HTML responses as a non-PDF format mismatch instead of a permanent paywall failure, while preserving normal retry limits and access-error handling.
diff --git a/src/local_deep_research/library/download_management/failure_classifier.py b/src/local_deep_research/library/download_management/failure_classifier.py
index b1a205fca3..1ba6c73b36 100644
--- a/src/local_deep_research/library/download_management/failure_classifier.py
+++ b/src/local_deep_research/library/download_management/failure_classifier.py
@@ -139,6 +139,27 @@ class FailureClassifier:
error_lower = error_type.lower()
details_lower = details.lower()
+ # GenericDownloader's neutral diagnostic identifies a format mismatch;
+ # it is not evidence that the source itself is inaccessible. Keep this
+ # exact so actual access failures retain their existing classifications.
+ # download_service._fallback_pdf_extraction (the download_as_text path)
+ # wraps the downloader's reason as "PDF extraction failed: {reason}"
+ # before it reaches here, so strip that one known wrapper prefix and
+ # still require an exact match on what's left.
+ html_not_pdf_reason = "source is an html page, not a pdf"
+ pdf_extraction_wrapper = "pdf extraction failed: "
+ unwrapped_details_lower = (
+ details_lower[len(pdf_extraction_wrapper) :]
+ if details_lower.startswith(pdf_extraction_wrapper)
+ else details_lower
+ )
+ if unwrapped_details_lower == html_not_pdf_reason:
+ return TemporaryFailure(
+ "html_not_pdf",
+ "Source is an HTML page, not a PDF",
+ timedelta(hours=1),
+ )
+
# arXiv specific patterns
if "arxiv" in error_lower or "arxiv" in details_lower:
if "recaptcha" in details_lower or "captcha" in details_lower:
diff --git a/src/local_deep_research/library/download_management/models/__init__.py b/src/local_deep_research/library/download_management/models/__init__.py
index e992216219..9958930ba7 100644
--- a/src/local_deep_research/library/download_management/models/__init__.py
+++ b/src/local_deep_research/library/download_management/models/__init__.py
@@ -21,15 +21,19 @@ class Base(DeclarativeBase):
class FailureType(str, Enum):
"""Enum for failure types - ensures consistency across the codebase"""
+ PROCESSING = "processing"
NOT_FOUND = "not_found"
FORBIDDEN = "forbidden"
GONE = "gone"
RATE_LIMITED = "rate_limited"
SERVER_ERROR = "server_error"
+ HTML_NOT_PDF = "html_not_pdf"
RECAPTCHA_PROTECTION = "recaptcha_protection"
INCOMPATIBLE_FORMAT = "incompatible_format"
+ PAYWALL_OR_LOGIN = "paywall_or_login"
TIMEOUT = "timeout"
NETWORK_ERROR = "network_error"
+ DOWNLOAD_FAILED = "download_failed"
UNKNOWN_ERROR = "unknown_error"
diff --git a/src/local_deep_research/research_library/downloaders/generic.py b/src/local_deep_research/research_library/downloaders/generic.py
index aa588b62b4..47ac0dc929 100644
--- a/src/local_deep_research/research_library/downloaders/generic.py
+++ b/src/local_deep_research/research_library/downloaders/generic.py
@@ -10,6 +10,9 @@ from loguru import logger
from .base import BaseDownloader, ContentType, DownloadResult
+HTML_NOT_PDF_REASON = "Source is an HTML page, not a PDF"
+
+
class GenericDownloader(BaseDownloader):
"""Generic downloader for any URL - attempts basic PDF download."""
@@ -96,13 +99,13 @@ class GenericDownloader(BaseDownloader):
) as response:
# Check status code
if response.status_code == 200:
- # Check if it's HTML instead of PDF
+ # HTML identifies a format mismatch, not a paywall.
response_content_type = response.headers.get(
"content-type", ""
).lower()
if "text/html" in response_content_type:
return DownloadResult(
- skip_reason="Article page requires login or subscription - no direct PDF link available",
+ skip_reason=HTML_NOT_PDF_REASON,
status_code=response.status_code,
)
return DownloadResult(
diff --git a/tests/research_library/downloaders/test_generic_downloader.py b/tests/research_library/downloaders/test_generic_downloader.py
index 4dc6fb9b0e..e6ea12aea4 100644
--- a/tests/research_library/downloaders/test_generic_downloader.py
+++ b/tests/research_library/downloaders/test_generic_downloader.py
@@ -6,6 +6,7 @@ import pytest
from local_deep_research.research_library.downloaders.generic import (
GenericDownloader,
+ HTML_NOT_PDF_REASON,
)
from local_deep_research.research_library.downloaders.base import (
ContentType,
@@ -208,7 +209,7 @@ class TestGenericDownloadWithResult:
"""Returns skip reason when HTML is returned instead of PDF."""
mock_response = mocker.MagicMock()
mock_response.status_code = 200
- mock_response.content = b"<html><body>Login page</body></html>"
+ mock_response.content = b"<html><body>Public article</body></html>"
mock_response.headers = {"content-type": "text/html"}
mock_response.__enter__ = mocker.Mock(return_value=mock_response)
mock_response.__exit__ = mocker.Mock(return_value=False)
@@ -223,10 +224,8 @@ class TestGenericDownloadWithResult:
result = downloader.download_with_result("https://example.com/paper")
assert result.is_success is False
- assert (
- "login" in result.skip_reason.lower()
- or "subscription" in result.skip_reason.lower()
- )
+ assert result.skip_reason == HTML_NOT_PDF_REASON
+ assert result.status_code == 200
def test_download_with_result_timeout(self, downloader, mocker):
"""Returns skip reason for timeout."""
diff --git a/tests/research_library/downloaders/test_generic_downloader_deep_coverage.py b/tests/research_library/downloaders/test_generic_downloader_deep_coverage.py
index e6bb807614..a79dc14514 100644
--- a/tests/research_library/downloaders/test_generic_downloader_deep_coverage.py
+++ b/tests/research_library/downloaders/test_generic_downloader_deep_coverage.py
@@ -14,8 +14,12 @@ from unittest.mock import Mock, patch
import pytest
import requests
+from local_deep_research.library.download_management.failure_classifier import (
+ FailureClassifier,
+)
from local_deep_research.research_library.downloaders.generic import (
GenericDownloader,
+ HTML_NOT_PDF_REASON,
)
from local_deep_research.research_library.downloaders.base import (
ContentType,
@@ -33,8 +37,14 @@ def downloader():
class TestDownloadWithResultDiagnostic:
"""Tests for download_with_result diagnostic HTTP status branches."""
- def test_200_html_returns_login_required(self, downloader):
- """200 with HTML content type suggests login/subscription needed."""
+ @pytest.mark.parametrize(
+ "content_type",
+ ["text/html", "text/html; charset=utf-8", "TEXT/HTML; charset=UTF-8"],
+ )
+ def test_200_html_does_not_invent_permanent_paywall_failure(
+ self, downloader, content_type
+ ):
+ """HTML alone cannot justify permanently excluding a public page."""
# Mock both _download_pdf calls to fail
with patch.object(
downloader.__class__.__bases__[0],
@@ -44,7 +54,7 @@ class TestDownloadWithResultDiagnostic:
# Mock diagnostic request
mock_response = Mock()
mock_response.status_code = 200
- mock_response.headers = {"content-type": "text/html; charset=utf-8"}
+ mock_response.headers = {"content-type": content_type}
mock_response.__enter__ = Mock(return_value=mock_response)
mock_response.__exit__ = Mock(return_value=False)
@@ -56,7 +66,23 @@ class TestDownloadWithResultDiagnostic:
)
assert result.is_success is False
- assert "login or subscription" in result.skip_reason
+ assert result.skip_reason == HTML_NOT_PDF_REASON
+ assert result.status_code == 200
+ failure = FailureClassifier().classify_failure(
+ error_type=type(result.skip_reason).__name__,
+ status_code=result.status_code,
+ url="https://example.com/paper",
+ details=result.skip_reason,
+ )
+ assert failure.error_type == "html_not_pdf"
+ assert not failure.is_permanent()
+ downloader.session.get.assert_called_once_with(
+ "https://example.com/paper",
+ timeout=5,
+ allow_redirects=True,
+ stream=True,
+ )
+ mock_response.__exit__.assert_called_once()
def test_200_unexpected_content_type(self, downloader):
"""200 with non-HTML non-PDF content type."""
diff --git a/tests/research_library/test_download_service_contracts.py b/tests/research_library/test_download_service_contracts.py
index fbda4f0771..95707c241b 100644
--- a/tests/research_library/test_download_service_contracts.py
+++ b/tests/research_library/test_download_service_contracts.py
@@ -56,8 +56,8 @@ Covered:
leave the truncated file behind (DEFECT -- xfail);
* retrying a failed resource updates in place -- one Document, one
blob, one attempt row per attempt;
- * the paywall/login/404/403 skip reasons that ``GenericDownloader``
- actually emits still classify the way ``main`` classified them, with
+ * HTML-only responses are distinguished from actual access failures in
+ the skip reasons that ``GenericDownloader`` emits, with
the producer and the classifier wired together so drift in either
half fails;
* an exception message surfaced to the client is credential-scrubbed
@@ -1012,14 +1012,14 @@ def test_retrying_a_failed_resource_does_not_duplicate_rows(db, store, seeded):
@pytest.mark.parametrize(
"status_code, content_type, expected_type, expect_permanent",
[
- # The paywall case: a 200 that hands back an article landing page.
- (200, "text/html; charset=utf-8", "paywall_or_login", True),
+ # An HTML landing page alone is not evidence of a paywall.
+ (200, "text/html; charset=utf-8", "html_not_pdf", False),
(403, "text/html", "forbidden", True),
(404, "text/html", "not_found", True),
(500, "text/html", "unknown_error", False),
],
)
-def test_generic_downloader_reasons_classify_as_they_did_on_main(
+def test_generic_downloader_reasons_distinguish_html_from_access_failures(
status_code, content_type, expected_type, expect_permanent
):
"""The producer and the classifier are wired together, not hardcoded.
@@ -1056,6 +1056,65 @@ def test_generic_downloader_reasons_classify_as_they_did_on_main(
assert failure.is_permanent() is expect_permanent
+def test_fallback_pdf_extraction_wraps_html_not_pdf_reason_correctly(
+ db, store, seeded
+):
+ """The ``download_as_text`` fallback path wraps the reason too, and
+ the classifier must still recognize it there.
+
+ ``download_resource`` passes ``GenericDownloader``'s skip_reason to
+ the classifier verbatim (covered above), but
+ ``DownloadService._fallback_pdf_extraction`` -- the path
+ ``download_as_text`` takes when no API/text extraction succeeded --
+ wraps it first: ``download_service.py`` returns
+ ``f"PDF extraction failed: {error_msg}"``, and that wrapped string is
+ what ``_record_retry_attempt`` passes to the classifier as
+ ``details``. Exercise the real fallback method end to end (real
+ downloader, real classifier, real DB session) rather than
+ hand-wrapping the string here, so a change to the wrapper's exact
+ text is caught too.
+
+ REVERT: undoing the wrapper-prefix stripping in
+ ``FailureClassifier.classify_failure`` (i.e. going back to
+ ``if details_lower == "source is an html page, not a pdf":`` with no
+ unwrapping) makes this test fail: ``failure.error_type`` becomes
+ ``"unknown_error"`` instead of ``"html_not_pdf"``.
+ """
+ url = "https://journal.example.com/article/456"
+ fixture = seeded(url)
+
+ generic_response = StubResponse(
+ status_code=200,
+ content=HTML_BYTES,
+ headers={"content-type": "text/html; charset=utf-8"},
+ )
+ downloader = _make_downloader(GenericDownloader, generic_response)
+ service = make_service(store, downloader)
+
+ success, error_message = service._fallback_pdf_extraction(
+ db.session, fixture.resource
+ )
+
+ assert success is False
+ # The producer's exact string survives, just wrapped.
+ assert (
+ error_message
+ == "PDF extraction failed: Source is an HTML page, not a PDF"
+ )
+
+ failure = FailureClassifier().classify_failure(
+ error_type=type(error_message).__name__,
+ # _record_retry_attempt (download_service.py) never passes a
+ # status_code on this call path.
+ status_code=None,
+ url=url,
+ details=error_message,
+ )
+
+ assert failure.error_type == "html_not_pdf"
+ assert failure.is_permanent() is False
+
+
def test_a_401_auth_required_page_is_retried_hourly_forever():
"""CHARACTERIZATION: the 401 reason misses every paywall keyword.
@@ -1068,9 +1127,12 @@ def test_a_401_auth_required_page_is_retried_hourly_forever():
authentication-walled article is re-fetched every hour indefinitely,
while its 403 sibling is correctly permanent.
- This matches ``main`` byte for byte (``failure_classifier.py`` is
- unchanged on this branch), so it is pinned as current behaviour
- rather than as a port regression.
+ This branch does modify ``failure_classifier.py``, but only to add an
+ exact-match (and wrapped-exact-match) branch for GenericDownloader's
+ HTML-format-mismatch string; the 401 reason above matches neither
+ form, so this assertion's outcome is unaffected and still matches
+ ``main`` byte for byte. It is pinned as current behaviour rather than
+ as a port regression.
"""
url = "https://journal.example.com/article/123"
downloader = _make_downloader(