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(chat): guard non-dict accumulated_context and non-string strategy (#7708)

Imaduddin Bhaisahebd4978b2d6413
diff --git a/changelog.d/+collection-precheck-non-string-strategy.bugfix.md b/changelog.d/+collection-precheck-non-string-strategy.bugfix.md
new file mode 100644
index 0000000000..298a1e811b
--- /dev/null
+++ b/changelog.d/+collection-precheck-non-string-strategy.bugfix.md
@@ -0,0 +1 @@
+Starting a research run with a non-string `strategy` value and a collection search engine no longer fails with a server error; the value is ignored, and the strategy falls back to `source-based`.
diff --git a/changelog.d/7674.bugfix.md b/changelog.d/7674.bugfix.md
new file mode 100644
index 0000000000..59f029fb9c
--- /dev/null
+++ b/changelog.d/7674.bugfix.md
@@ -0,0 +1 @@
+Chat follow-ups no longer fail with a server error when a session's saved context is corrupted or is not a dictionary; it is treated as empty, and corrupt saved entities, topics or summary are replaced on the next update.
diff --git a/src/local_deep_research/chat/context.py b/src/local_deep_research/chat/context.py
index 020268038e..2691a81c5c 100644
--- a/src/local_deep_research/chat/context.py
+++ b/src/local_deep_research/chat/context.py
@@ -65,7 +65,18 @@ class ChatContextManager:
for msg in (messages or [])
if isinstance(msg, dict) and msg.get("message_type") != "step"
]
- self.accumulated_context = accumulated_context or {}
+ # The persisted JSON value is passed in unvalidated. Corruption or a
+ # legacy writer can leave a truthy non-dict (7, "abc", [1]) that a
+ # bare `or {}` would let through, so treat any non-dict as empty.
+ if isinstance(accumulated_context, dict):
+ self.accumulated_context = accumulated_context
+ else:
+ if accumulated_context:
+ logger.warning(
+ "Ignoring non-dict accumulated_context (type={})",
+ type(accumulated_context).__name__,
+ )
+ self.accumulated_context = {}
# Used by build_research_context to construct the LLM that produces
# the query-focused conversation summary.
self.settings_snapshot = settings_snapshot
@@ -268,13 +279,13 @@ class ChatContextManager:
def _get_key_entities(self) -> List[str]:
"""Get key entities from accumulated context."""
- entities: List[str] = self.accumulated_context.get("key_entities", [])
- return entities[:20]
+ entities = self.accumulated_context.get("key_entities", [])
+ return entities[:20] if isinstance(entities, list) else []
def _get_topics(self) -> List[str]:
"""Get topics from accumulated context."""
- topics: List[str] = self.accumulated_context.get("topics", [])
- return topics[:10]
+ topics = self.accumulated_context.get("topics", [])
+ return topics[:10] if isinstance(topics, list) else []
def extract_context_updates(self, new_content: str) -> Dict[str, Any]:
"""
diff --git a/src/local_deep_research/chat/service.py b/src/local_deep_research/chat/service.py
index 3edb22e9cf..cdd1e250de 100644
--- a/src/local_deep_research/chat/service.py
+++ b/src/local_deep_research/chat/service.py
@@ -1463,24 +1463,46 @@ class ChatService:
# detected without MutableDict.as_mutable() — at flush time
# the loaded snapshot equals the current value and no UPDATE
# is emitted. Same convention as research_sources_service.py.
- existing_ctx = session.accumulated_context or {}
+ existing_ctx = session.accumulated_context
+ if not isinstance(existing_ctx, dict):
+ # Corrupt/legacy non-dict value: start fresh so this
+ # write repairs the row instead of failing on dict().
+ existing_ctx = {}
ctx = dict(existing_ctx)
+ # A dict row can still hold corrupt values (key_entities: 7,
+ # topics: "abc", [1]). Only keep stored items that are
+ # strings in a list, so the merge repairs the row instead of
+ # raising (set(7), unhashable items) or exploding a string
+ # into characters (set("abc")).
+
# Merge entities (deduplicate)
if new_entities:
- existing = set(ctx.get("key_entities", []))
+ stored = ctx.get("key_entities", [])
+ existing = (
+ {x for x in stored if isinstance(x, str)}
+ if isinstance(stored, list)
+ else set()
+ )
existing.update(new_entities)
ctx["key_entities"] = list(existing)[:50]
# Merge topics
if new_topics:
- existing = set(ctx.get("topics", []))
+ stored = ctx.get("topics", [])
+ existing = (
+ {x for x in stored if isinstance(x, str)}
+ if isinstance(stored, list)
+ else set()
+ )
existing.update(new_topics)
ctx["topics"] = list(existing)[:20]
# Append to summary (with size limit)
if summary_addition:
current = ctx.get("summary", "")
+ if not isinstance(current, str):
+ current = ""
new_summary = (
f"{current}\n\n{summary_addition}"
if current
diff --git a/src/local_deep_research/search_system.py b/src/local_deep_research/search_system.py
index f7c98f21c1..dc181c968a 100644
--- a/src/local_deep_research/search_system.py
+++ b/src/local_deep_research/search_system.py
@@ -130,8 +130,10 @@ class AdvancedSearchSystem:
prompts to search engines.
"""
- # Both can come from settings or a replayed queue row, read back from
- # JSON, so either may hold any JSON type.
+ # Both are read back from JSON-typed sources, so either may hold any
+ # JSON type: strategy_name can come from settings or a replayed queue
+ # row; research_context from the chat/follow-up routers or the
+ # programmatic API parameter.
if not isinstance(strategy_name, str):
logger.warning(
f"Ignoring non-string strategy_name "
diff --git a/src/local_deep_research/web/routers/research.py b/src/local_deep_research/web/routers/research.py
index f3fb3c814b..142268ec99 100644
--- a/src/local_deep_research/web/routers/research.py
+++ b/src/local_deep_research/web/routers/research.py
@@ -447,7 +447,13 @@ def _precheck_collection_agent_enabled(
"""
if not search_engine or not search_engine.startswith("collection_"):
return None
- if not strategy or strategy.lower() not in LANGGRAPH_STRATEGY_ALIASES:
+ # ``strategy`` comes straight from the JSON body, so it may be any JSON
+ # type. A non-string cannot name the LangGraph agent; leave it to the
+ # constructor guard downstream instead of crashing on ``.lower()``.
+ if (
+ not isinstance(strategy, str)
+ or strategy.lower() not in LANGGRAPH_STRATEGY_ALIASES
+ ):
return None
try:
from ...database.models.library import Collection
diff --git a/tests/chat/test_chat_context_non_dict.py b/tests/chat/test_chat_context_non_dict.py
new file mode 100644
index 0000000000..6343247294
--- /dev/null
+++ b/tests/chat/test_chat_context_non_dict.py
@@ -0,0 +1,213 @@
+"""Tests for a non-dict persisted ``accumulated_context`` (issue #7674).
+
+``ChatSession.accumulated_context`` is a plain JSON column that the research
+trigger paths pass to ``ChatContextManager`` unvalidated. Writers always store
+a dict, but corruption or a legacy writer can leave a truthy non-dict such as
+``7``, ``"abc"`` or ``[1]``. The old ``accumulated_context or {}`` guard only
+replaced falsy values, so a truthy non-dict reached
+``self.accumulated_context.get(...)`` and raised ``AttributeError`` -> HTTP 500
+on every follow-up message in that session.
+
+Coverage:
+
+- Unit: construction with non-dict values builds a usable manager and the
+ entity/topic getters return their defaults, including when the stored
+ ``key_entities`` / ``topics`` values are themselves not lists.
+- HTTP: a session row corrupted in the database yields a clean follow-up turn
+ from both research-trigger routes (``send_message`` and ``retry_attempt``)
+ instead of a 500.
+
+The write path (``ChatService.update_accumulated_context``) is covered in
+``test_chat_service.py``.
+"""
+
+import json
+from unittest.mock import patch
+
+import pytest
+
+from local_deep_research.chat.context import ChatContextManager
+from local_deep_research.database.models import ChatSession
+from local_deep_research.database.session_context import get_user_db_session
+
+_SPAWN = "local_deep_research.web.routers.chat.start_research_process"
+
+# Truthy non-dicts are the bug; None / falsy values already worked and must
+# keep working.
+NON_DICT_VALUES = [
+ pytest.param(7, id="int"),
+ pytest.param("abc", id="str"),
+ pytest.param([1], id="list"),
+ pytest.param(None, id="none"),
+ pytest.param(0, id="falsy-int"),
+ pytest.param("", id="empty-str"),
+ pytest.param([], id="empty-list"),
+]
+
+# Two differently shaped corrupt values are enough for the HTTP tests; the
+# unit tests above cover the full value matrix.
+CORRUPT_VALUES = [
+ pytest.param(7, id="int"),
+ pytest.param([1], id="list"),
+]
+
+
+class TestChatContextManagerNonDictAccumulatedContext:
+ """Construction must tolerate any non-dict ``accumulated_context``."""
+
+ @pytest.mark.parametrize("bad_value", NON_DICT_VALUES)
+ def test_non_dict_is_treated_as_empty(self, bad_value):
+ manager = ChatContextManager("test-session", [], bad_value)
+
+ assert manager.accumulated_context == {}
+ assert manager._get_key_entities() == []
+ assert manager._get_topics() == []
+
+ @pytest.mark.parametrize("bad_value", NON_DICT_VALUES)
+ def test_build_research_context_does_not_raise(self, bad_value):
+ manager = ChatContextManager("test-session", [], bad_value)
+
+ context = manager.build_research_context()
+
+ assert context["session_id"] == "test-session"
+ assert context["key_entities"] == []
+ assert context["topics"] == []
+ assert context["is_multi_turn"] is False
+
+ def test_valid_dict_is_still_honoured(self):
+ accumulated = {
+ "key_entities": ["entity1", "entity2"],
+ "topics": ["topic1"],
+ }
+
+ manager = ChatContextManager("test-session", [], accumulated)
+
+ assert manager._get_key_entities() == ["entity1", "entity2"]
+ assert manager._get_topics() == ["topic1"]
+
+ def test_empty_dict_is_still_accepted(self):
+ manager = ChatContextManager("test-session", [], {})
+
+ assert manager.accumulated_context == {}
+ assert manager._get_key_entities() == []
+ assert manager._get_topics() == []
+
+ @pytest.mark.parametrize("bad_value", [7, "abc", {"a": 1}])
+ def test_non_list_entities_and_topics_are_treated_as_empty(self, bad_value):
+ """A dict whose own values are the wrong type must not crash either."""
+ manager = ChatContextManager(
+ "test-session",
+ [],
+ {"key_entities": bad_value, "topics": bad_value},
+ )
+
+ assert manager._get_key_entities() == []
+ assert manager._get_topics() == []
+ assert manager.build_research_context()["key_entities"] == []
+
+
+# ---------------------------------------------------------------------------
+# HTTP-level: a corrupted session row must not 500 the follow-up turn.
+# ---------------------------------------------------------------------------
+
+
+def _create_session(client, query="Test"):
+ resp = client.post(
+ "/api/chat/sessions",
+ json={"initial_query": query},
+ content_type="application/json",
+ )
+ assert resp.status_code == 200, resp.data
+ return json.loads(resp.data)["session_id"]
+
+
+def _username(client) -> str:
+ resp = client.get("/auth/check")
+ assert resp.status_code == 200, resp.text
+ return resp.json()["username"]
+
+
+def _corrupt_accumulated_context(username, session_id, value):
+ """Overwrite the persisted ``accumulated_context`` with ``value``."""
+ with get_user_db_session(username) as db:
+ session = db.query(ChatSession).filter_by(id=session_id).first()
+ assert session is not None
+ session.accumulated_context = value
+ db.commit()
+
+
+class TestCorruptedAccumulatedContextRoutes:
+ """Both routes that build a ``ChatContextManager`` from the session row."""
+
+ @pytest.mark.parametrize("bad_value", CORRUPT_VALUES)
+ def test_send_message_with_corrupted_context_is_not_a_500(
+ self, authenticated_client, bad_value
+ ):
+ session_id = _create_session(authenticated_client)
+ _corrupt_accumulated_context(
+ _username(authenticated_client), session_id, bad_value
+ )
+
+ with patch(_SPAWN) as mock_spawn:
+ response = authenticated_client.post(
+ f"/api/chat/sessions/{session_id}/messages",
+ json={
+ "content": "follow-up question",
+ "trigger_research": True,
+ },
+ content_type="application/json",
+ )
+
+ assert response.status_code == 200, response.data
+ body = json.loads(response.data)
+ assert body["success"] is True
+ assert body["research_id"] is not None
+ # The request got past context building all the way to the spawn.
+ mock_spawn.assert_called_once()
+
+ @pytest.mark.parametrize("bad_value", CORRUPT_VALUES)
+ def test_retry_attempt_with_corrupted_context_is_not_a_500(
+ self, authenticated_client, bad_value
+ ):
+ session_id = _create_session(authenticated_client)
+
+ # Seed one attempt while the context is still healthy.
+ with patch(_SPAWN):
+ send_resp = authenticated_client.post(
+ f"/api/chat/sessions/{session_id}/messages",
+ json={"content": "what is X?", "trigger_research": True},
+ content_type="application/json",
+ )
+ assert send_resp.status_code == 200, send_resp.data
+ old_research_id = json.loads(send_resp.data)["research_id"]
+
+ _corrupt_accumulated_context(
+ _username(authenticated_client), session_id, bad_value
+ )
+
+ # Same short-circuits as the happy-path retry test: skip the
+ # stale-row grace window and treat the just-seeded research as dead
+ # so delete_attempt reclaims it, leaving only the context-building
+ # path under test.
+ with (
+ patch(
+ "local_deep_research.web.routers.chat."
+ "_enforce_chat_session_research_slot",
+ return_value=None,
+ ),
+ patch(
+ "local_deep_research.chat.service.is_research_thread_alive",
+ return_value=False,
+ ),
+ patch(_SPAWN) as mock_spawn,
+ ):
+ retry_resp = authenticated_client.post(
+ f"/api/chat/sessions/{session_id}/attempts/"
+ f"{old_research_id}/retry"
+ )
+
+ assert retry_resp.status_code == 200, retry_resp.data
+ body = json.loads(retry_resp.data)
+ assert body["success"] is True
+ assert body["research_id"] != old_research_id
+ mock_spawn.assert_called_once()
diff --git a/tests/chat/test_chat_service.py b/tests/chat/test_chat_service.py
index 3b9ec8eeaa..1fa381ebfb 100644
--- a/tests/chat/test_chat_service.py
+++ b/tests/chat/test_chat_service.py
@@ -337,6 +337,107 @@ class TestChatServiceContextAccumulation:
# Summary should be capped at 8000 characters
assert len(mock_chat_session.accumulated_context["summary"]) <= 8000
+ @pytest.mark.parametrize("bad_value", [7, "abc", [1]])
+ def test_update_accumulated_context_repairs_non_dict_row(
+ self, mock_user_db_session, mock_chat_session, bad_value
+ ):
+ """A corrupt non-dict row is replaced by a fresh dict, not a crash."""
+ from local_deep_research.chat.service import ChatService
+
+ mock_chat_session.accumulated_context = bad_value
+ setup_query_mock_with_session(mock_user_db_session, mock_chat_session)
+ mock_user_db_session.commit = MagicMock()
+
+ service = ChatService(username="testuser")
+ result = service.update_accumulated_context(
+ session_id=mock_chat_session.id,
+ new_entities=["entity1"],
+ new_topics=["topic1"],
+ )
+
+ assert result is True
+ assert mock_chat_session.accumulated_context == {
+ "key_entities": ["entity1"],
+ "topics": ["topic1"],
+ }
+
+ @pytest.mark.parametrize("bad_value", [7, "abc", [1], [["x"]], None])
+ @pytest.mark.parametrize("field", ["key_entities", "topics"])
+ def test_update_accumulated_context_repairs_corrupt_list_values(
+ self, mock_user_db_session, mock_chat_session, field, bad_value
+ ):
+ """A dict row with a corrupt list value is repaired, not mangled.
+
+ Without the guard, ``7`` / ``None`` / ``[["x"]]`` raise TypeError in
+ ``set(...)``, ``"abc"`` is exploded into ``["a", "b", "c"]`` and
+ persisted, and ``[1]`` keeps its non-string item.
+ """
+ from local_deep_research.chat.service import ChatService
+
+ mock_chat_session.accumulated_context = {field: bad_value}
+ setup_query_mock_with_session(mock_user_db_session, mock_chat_session)
+ mock_user_db_session.commit = MagicMock()
+
+ service = ChatService(username="testuser")
+ result = service.update_accumulated_context(
+ session_id=mock_chat_session.id,
+ new_entities=["e1"],
+ new_topics=["t1"],
+ )
+
+ assert result is True
+ assert mock_chat_session.accumulated_context == {
+ "key_entities": ["e1"],
+ "topics": ["t1"],
+ }
+
+ @pytest.mark.parametrize("bad_value", [7, [1], {"a": 1}])
+ def test_update_accumulated_context_repairs_corrupt_summary(
+ self, mock_user_db_session, mock_chat_session, bad_value
+ ):
+ """A non-string stored summary is dropped, not formatted into text."""
+ from local_deep_research.chat.service import ChatService
+
+ mock_chat_session.accumulated_context = {"summary": bad_value}
+ setup_query_mock_with_session(mock_user_db_session, mock_chat_session)
+ mock_user_db_session.commit = MagicMock()
+
+ service = ChatService(username="testuser")
+ result = service.update_accumulated_context(
+ session_id=mock_chat_session.id,
+ summary_addition="New summary",
+ )
+
+ assert result is True
+ assert mock_chat_session.accumulated_context["summary"] == "New summary"
+
+ def test_update_accumulated_context_still_merges_valid_values(
+ self, mock_user_db_session, mock_chat_session
+ ):
+ """The guards must not change merging of well-formed rows."""
+ from local_deep_research.chat.service import ChatService
+
+ mock_chat_session.accumulated_context = {
+ "key_entities": ["e1"],
+ "topics": ["t1"],
+ "summary": "Old",
+ }
+ setup_query_mock_with_session(mock_user_db_session, mock_chat_session)
+ mock_user_db_session.commit = MagicMock()
+
+ service = ChatService(username="testuser")
+ service.update_accumulated_context(
+ session_id=mock_chat_session.id,
+ new_entities=["e2"],
+ new_topics=["t2"],
+ summary_addition="New",
+ )
+
+ ctx = mock_chat_session.accumulated_context
+ assert sorted(ctx["key_entities"]) == ["e1", "e2"]
+ assert sorted(ctx["topics"]) == ["t1", "t2"]
+ assert ctx["summary"] == "Old\n\nNew"
+
class TestChatServiceGetSession:
"""Tests for ChatService get_session method."""
diff --git a/tests/web/routes/test_research_routes_policy.py b/tests/web/routes/test_research_routes_policy.py
index 7bc8ee9e6f..691ab5d555 100644
--- a/tests/web/routes/test_research_routes_policy.py
+++ b/tests/web/routes/test_research_routes_policy.py
@@ -548,6 +548,17 @@ class TestPrecheckCollectionAgentEnabled:
f"agent_enabled=False"
)
+ @pytest.mark.parametrize("strategy", [None, 123, True, ["mcp"], {"a": 1}])
+ def test_non_string_strategy_is_ignored_not_a_crash(self, strategy):
+ # ``strategy`` is read straight from the JSON body, so it can be any
+ # JSON type. A non-string can't name the LangGraph agent: the
+ # precheck must return None rather than raise AttributeError from
+ # ``.lower()`` (the constructor guard handles it downstream).
+ result = _precheck_collection_agent_enabled(
+ "collection_abc", strategy, "user"
+ )
+ assert result is None
+
def test_non_collection_engine_is_skipped(self):
# The flag is only carried by collection_* engines in the
# current model. A static engine (or a misspelled id) must