Spaces:
Sleeping
Sleeping
Commit
Β·
3acc06b
1
Parent(s):
40b5db7
Enhance JSON parsing with fallback sanitization and regex extraction for improved error handling
Browse files- mai_dx/main.py +34 -1
mai_dx/main.py
CHANGED
@@ -1137,7 +1137,40 @@ This case has gone through {case_state.iteration} iterations. Focus on decisive
|
|
1137 |
continue
|
1138 |
|
1139 |
# Direct parsing attempt as fallback
|
1140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1141 |
|
1142 |
except (
|
1143 |
json.JSONDecodeError,
|
|
|
1137 |
continue
|
1138 |
|
1139 |
# Direct parsing attempt as fallback
|
1140 |
+
try:
|
1141 |
+
return json.loads(response)
|
1142 |
+
except json.JSONDecodeError:
|
1143 |
+
# --- Fallback Sanitization ---
|
1144 |
+
# Attempt to strip any leading table/frame characters (e.g., β, β, β°) that may wrap each line
|
1145 |
+
try:
|
1146 |
+
# Extract everything between the first '{' and last '}'
|
1147 |
+
start_curly = response.index('{')
|
1148 |
+
end_curly = response.rindex('}')
|
1149 |
+
candidate = response[start_curly:end_curly + 1]
|
1150 |
+
sanitized_lines = []
|
1151 |
+
for line in candidate.splitlines():
|
1152 |
+
# Remove common frame characters and leading whitespace
|
1153 |
+
line = line.lstrip('β|ββ°β―βββ€ ').rstrip('β|ββ°β―βββ€ ')
|
1154 |
+
sanitized_lines.append(line)
|
1155 |
+
candidate_clean = '\n'.join(sanitized_lines)
|
1156 |
+
return json.loads(candidate_clean)
|
1157 |
+
except Exception as inner_e:
|
1158 |
+
# Still failing, raise original error to trigger retry logic
|
1159 |
+
try:
|
1160 |
+
# --- Ultimate Fallback: Regex extraction ---
|
1161 |
+
import re
|
1162 |
+
atype = re.search(r'"action_type"\s*:\s*"(ask|test|diagnose)"', response, re.IGNORECASE)
|
1163 |
+
content_match = re.search(r'"content"\s*:\s*"([^"]+?)"', response, re.IGNORECASE | re.DOTALL)
|
1164 |
+
reasoning_match = re.search(r'"reasoning"\s*:\s*"([^"]+?)"', response, re.IGNORECASE | re.DOTALL)
|
1165 |
+
if atype and content_match and reasoning_match:
|
1166 |
+
return {
|
1167 |
+
"action_type": atype.group(1).lower(),
|
1168 |
+
"content": content_match.group(1).strip(),
|
1169 |
+
"reasoning": reasoning_match.group(1).strip(),
|
1170 |
+
}
|
1171 |
+
except Exception:
|
1172 |
+
pass
|
1173 |
+
raise e
|
1174 |
|
1175 |
except (
|
1176 |
json.JSONDecodeError,
|