From 49c472b46df61adbda6b716888f0050373b67a18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CNaeel=E2=80=9D?= Date: Wed, 24 Jun 2026 09:14:07 +0400 Subject: [PATCH] =?UTF-8?q?v1.0.166:=20aggressive=20JSON=20repair=20?= =?UTF-8?q?=E2=80=94=20close=20unterminated=20strings/braces?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/services/classify.py | 42 ++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/deploy/services/classify.py b/deploy/services/classify.py index b9d4a70..71a05b6 100644 --- a/deploy/services/classify.py +++ b/deploy/services/classify.py @@ -98,31 +98,53 @@ def classify_batch(batch_id): def _safe_json_parse(raw): - """Parse LLM response, fixing common JSON errors (multiline, markdown, unescaped chars).""" + """Parse LLM response, fixing common JSON errors.""" if not raw: raise ValueError("empty LLM response") - # Strip markdown wrappers text = raw.strip() + # Strip markdown if "```json" in text: text = text.split("```json")[1].split("```")[0].strip() elif "```" in text: text = text.split("```")[1].split("```")[0].strip() - # Try strict parse first + # Remove non-JSON prefix/suffix (LLM chatter) + brace_start = text.find("{") + brace_end = text.rfind("}") + if brace_start >= 0 and brace_end > brace_start: + text = text[brace_start:brace_end + 1] + + # Try strict parse try: return json.loads(text) except json.JSONDecodeError: pass - # Fix common issues: unescaped newlines inside strings, unterminated strings - # Replace literal newlines inside JSON values import re as _re - # Collapse multiline to single line - text = _re.sub(r'\n\s*', ' ', text) - # Remove trailing commas before closing braces - text = _re.sub(r',\s*}', '}', text) - text = _re.sub(r',\s*]', ']', text) + # Collapse multiline + text = _re.sub(r"\n\s*", " ", text) + # Remove trailing commas + text = _re.sub(r",\s*}", "}", text) + text = _re.sub(r",\s*]", "]", text) + + # Try again + try: + return json.loads(text) + except json.JSONDecodeError: + pass + + # Aggressive: try adding missing closing quotes/braces + text = text.rstrip() + if not text.endswith("}"): + # Count unclosed quotes + in_string = False + for i, ch in enumerate(text): + if ch == '"' and (i == 0 or text[i-1] != "\\"): + in_string = not in_string + if in_string: + text += '"' + text += "}" return json.loads(text)