~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
fix(news): skip source rows whose url is not a string (#5900) (#5934)
LearningCircuita4d99170334b
diff --git a/changelog.d/+news-feed-non-string-source-url.bugfix.md b/changelog.d/+news-feed-non-string-source-url.bugfix.mdnew file mode 100644index 0000000000..06a9fbec50--- /dev/null+++ b/changelog.d/+news-feed-non-string-source-url.bugfix.md@@ -0,0 +1 @@+The news feed no longer fails with a generic "Database error" when a stored source row's URL is not a string; such rows are now skipped instead of aborting the whole feed.diff --git a/src/local_deep_research/news/api.py b/src/local_deep_research/news/api.pyindex 2a7fef002c..3217e8a349 100644--- a/src/local_deep_research/news/api.py+++ b/src/local_deep_research/news/api.py@@ -429,10 +429,20 @@ def get_news_feed()except Exception as db_error:- logger.exception(f"Database error in research history: {db_error}")+ # This block is wide enough to catch far more than database+ # errors -- everything the feed does with a row runs inside+ # it. Name the concrete exception type and chain it, so a+ # plain ValueError from a formatting helper is not reported+ # to the logs as "Database error" with no way back to the+ # real raise site. The client still gets the generic detail.+ logger.exception(+ "Error in research history query ({}): {}",+ type(db_error).__name__,+ db_error,+ )raise DatabaseAccessException("research_history_query", _GENERIC_ERROR_DETAIL- )+ ) from db_error# If no news items found, return empty listif not news_items:diff --git a/src/local_deep_research/web/services/report_assembly_service.py b/src/local_deep_research/web/services/report_assembly_service.pyindex 343e893c0d..5b16f042c6 100644--- a/src/local_deep_research/web/services/report_assembly_service.py+++ b/src/local_deep_research/web/services/report_assembly_service.py@@ -170,14 +170,25 @@ def _format_source_link(row: ResearchResource) -> Optional[Dict[str, str]]:Shared by :func:`get_research_source_links` and its batched variant sothe two cannot drift. Returns ``None`` for a row the feed cannot link- to (a non-``http`` URL). Titles fall back to the bare domain when- missing and are truncated to 50 chars, matching the existing- list-card rendering in the news UI.+ to: a non-``http`` URL, or a ``url`` that is not a string at all.+ Titles fall back to the bare domain when missing and are truncated to+ 50 chars, matching the existing list-card rendering in the news UI.++ A non-``str`` ``url`` is skipped, not coerced — the same rule+ ``count_distinct_sources`` and ``format_links_to_markdown`` already+ apply, and for the same reason: the caller passes this result to+ :func:`_dedup_key`, and ``canonical_url_key`` raises on a non-str+ (it unpacks ``str.partition``). Coercing with ``str()`` instead would+ render a Python repr as a clickable link. ``title`` gets the same+ treatment, so a malformed row degrades to the domain fallback rather+ than printing a repr onto a news card."""- url = (row.url or "").strip()+ if not isinstance(row.url, str):+ return None+ url = row.url.strip()if not url.startswith("http"):return None- title = (row.title or "").strip()+ title = (row.title if isinstance(row.title, str) else "").strip()if not title:domain = url.split("//")[-1].split("/")[0]title = domain.replace("www.", "")diff --git a/tests/web/services/test_report_assembly_service.py b/tests/web/services/test_report_assembly_service.pyindex 9250362465..523da024dc 100644--- a/tests/web/services/test_report_assembly_service.py+++ b/tests/web/services/test_report_assembly_service.py@@ -1,6 +1,8 @@"""Tests for the report_assembly_service module."""from datetime import datetime, UTC+from types import SimpleNamespace+from unittest.mock import MagicMockimport pytestfrom sqlalchemy import create_engine@@ -526,3 +528,97 @@ class TestGetResearchSourceLinksBatch:)batch = get_research_source_links_batch([r.id], db_session, limit=2)assert len(batch[r.id]) == 2+++# ---------------------------------------------------------------------------+# Non-string row values (regression: #5900 / #5894)+# ---------------------------------------------------------------------------+++class _FakeQuery:+ """Minimal stand-in for the query chain both source-link helpers build.++ A real SQLite session cannot carry these rows: the point of the tests+ below is a ``url`` that is not a string, which the column would coerce+ or reject before the code under test ever saw it.+ """++ def __init__(self, rows):+ self._rows = rows++ def filter(self, *args, **kwargs):+ return self++ def filter_by(self, *args, **kwargs):+ return self++ def order_by(self, *args, **kwargs):+ return self++ def limit(self, *args, **kwargs):+ return self++ def all(self):+ return list(self._rows)++ def __iter__(self):+ # get_research_source_links walks the query lazily rather than+ # calling .all(), so both access shapes must work.+ return iter(self._rows)+++class _FakeSession:+ def __init__(self, rows):+ self._rows = rows++ def query(self, *args, **kwargs):+ return _FakeQuery(self._rows)+++def _row(url, title="T", research_id="r1"):+ return SimpleNamespace(research_id=research_id, url=url, title=title)+++class TestSourceLinksNonStringValues:+ """A row whose ``url`` is not a ``str`` must be skipped, never raise.++ ``_dedup_key`` -> ``canonical_url_key`` -> ``_parse_library_citation``+ unpacks ``str.partition("#")`` into three names. Handed a non-str that+ merely *looks* truthy and string-like, that unpack raises+ ``ValueError: not enough values to unpack (expected 3, got 0)``. In the+ news feed this happened inside a blanket ``except Exception`` that+ re-raised it as a ``DatabaseAccessException``, hiding the real defect.+ """++ def test_magicmock_row_is_skipped_not_fatal(self):+ """The exact shape the news API tests supply.++ ``mock_session.query(...)`` is stubbed once and reused, so+ ``get_research_source_links_batch`` receives auto-specced+ ``MagicMock`` rows whose ``.url`` responds truthily to ``strip``+ and ``startswith`` but is not a string.+ """+ row = MagicMock()+ row.research_id = "r1"+ # .url and .title deliberately left as auto-created attributes.+ batch = get_research_source_links_batch(["r1"], _FakeSession([row]))+ assert batch == {"r1": []}++ @pytest.mark.parametrize("bad_url", [12345, None, object(), b"https://x"])+ def test_non_string_url_skipped_good_rows_survive(self, bad_url):+ rows = [_row(bad_url), _row("https://ok.com/x", title="OK")]+ batch = get_research_source_links_batch(["r1"], _FakeSession(rows))+ assert batch["r1"] == [{"url": "https://ok.com/x", "title": "OK"}]++ def test_non_string_title_falls_back_to_domain(self):+ """A bad title must not put a repr on a news card."""+ batch = get_research_source_links_batch(+ ["r1"], _FakeSession([_row("https://ok.com/x", title=object())])+ )+ assert batch["r1"] == [{"url": "https://ok.com/x", "title": "ok.com"}]++ def test_single_research_variant_guards_too(self):+ """The unbatched helper shares ``_format_source_link``; assert it."""+ rows = [_row(MagicMock()), _row("https://ok.com/x", title="OK")]+ links = get_research_source_links("r1", _FakeSession(rows), limit=3)+ assert links == [{"url": "https://ok.com/x", "title": "OK"}]