distill: salvage well-formed strings from malformed writer arrays

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
type-two 2026-07-18 21:31:04 +10:00
parent 07a0db6b61
commit 2b3b6e8dfd

View File

@ -96,7 +96,15 @@ def ask_writer(persona, topic, refs):
start = txt.find("[")
if start < 0:
raise ValueError(f"no JSON array in writer output: {txt[:200]}")
arr, _ = json.JSONDecoder().raw_decode(txt[start:])
try:
arr, _ = json.JSONDecoder().raw_decode(txt[start:])
except json.JSONDecodeError:
# salvage: models sometimes break an array element mid-list;
# harvest the well-formed quoted strings instead of losing all 12
arr = [json.loads(f'"{m}"') for m in
re.findall(r'"((?:[^"\\]|\\.){8,300})"', txt[start:])]
if not arr:
raise
except (ValueError, json.JSONDecodeError) as e:
last_err = e
continue