diff --git a/distill.py b/distill.py index 8b521e7..570f444 100755 --- a/distill.py +++ b/distill.py @@ -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