Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(memory): parse update facts JSON with raw_decode

santhrealeee0431fffd0
diff --git a/deeptutor/services/memory/consolidator/modes/update.py b/deeptutor/services/memory/consolidator/modes/update.py
index 90393c59c..d9f740c5e 100644
--- a/deeptutor/services/memory/consolidator/modes/update.py
+++ b/deeptutor/services/memory/consolidator/modes/update.py
@@ -624,10 +624,13 @@ def _extract_json_object(raw: str) -> str | None:
if text.rstrip().endswith("```"):
text = text.rstrip()[:-3]
start = text.find("{")
- end = text.rfind("}")
- if start == -1 or end <= start:
+ if start == -1:
return None
- return text[start : end + 1]
+ try:
+ _parsed, end = json.JSONDecoder().raw_decode(text[start:])
+ except json.JSONDecodeError:
+ return None
+ return text[start : start + end]
def _append_facts_to_doc(
diff --git a/tests/services/memory/test_update_facts_json.py b/tests/services/memory/test_update_facts_json.py
new file mode 100644
index 000000000..776559f81
--- /dev/null
+++ b/tests/services/memory/test_update_facts_json.py
@@ -0,0 +1,16 @@
+"""Regression: memory update fact JSON tolerates trailing brace prose."""
+
+from __future__ import annotations
+
+from deeptutor.services.memory.consolidator.modes import update as update_mode
+
+
+def test_parse_facts_tolerates_trailing_brace_prose() -> None:
+ raw = (
+ '{"facts":[{"text":"hello","section":"Notes","refs":["a:b"]}]}'
+ " trailing {note}"
+ )
+ facts = update_mode._parse_facts(raw)
+ assert len(facts) == 1
+ assert facts[0].text == "hello"
+ assert facts[0].section == "Notes"