Shreya Pal commited on
Commit
0d79c56
·
1 Parent(s): 4bd6a99

Implement robust JSON safety fallbacks blocking malformed Mistral outputs from 500 downstream crashes

Browse files
Files changed (1) hide show
  1. server/app.py +21 -3
server/app.py CHANGED
@@ -142,11 +142,29 @@ Respond with exactly this format:
142
  text_out = str(raw)
143
 
144
  import re
 
 
145
  match = re.search(r'\{.*?\}', text_out, re.DOTALL)
 
 
146
  if match:
147
- result = json.loads(match.group())
148
- else:
149
- raise ValueError(f"No JSON found in response: {text_out}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
  result["decision"] = result.get("decision", "flag").lower()
152
  if result["decision"] not in ("allow", "flag", "remove"):
 
142
  text_out = str(raw)
143
 
144
  import re
145
+ import ast
146
+
147
  match = re.search(r'\{.*?\}', text_out, re.DOTALL)
148
+ result = {}
149
+
150
  if match:
151
+ json_str = match.group()
152
+ try:
153
+ result = json.loads(json_str)
154
+ except Exception:
155
+ try:
156
+ result = ast.literal_eval(json_str)
157
+ if not isinstance(result, dict):
158
+ result = {}
159
+ except Exception:
160
+ pass
161
+
162
+ if not result:
163
+ result = {
164
+ "decision": "flag",
165
+ "confidence": 0.5,
166
+ "explanation": f"Complex JSON Parse Failure: {text_out.strip()}"
167
+ }
168
 
169
  result["decision"] = result.get("decision", "flag").lower()
170
  if result["decision"] not in ("allow", "flag", "remove"):