cassidyjames commited on
Commit
44de8b1
·
1 Parent(s): f81f49b

model_interface: fail loudly on unexpected PolicyLM responses

Browse files

Missing flagged/categories keys were silently mapped to label 0, so a
misconfigured endpoint showed "No Policy Violation" with no categories.
The handler always returns at least one category, so treat anything else
as an error and surface the raw response.

Files changed (1) hide show
  1. utils/model_interface.py +10 -2
utils/model_interface.py CHANGED
@@ -228,8 +228,16 @@ def run_policylm(test_input: str, policy: str, structured_rules: list | None = N
228
  except Exception:
229
  raise gr.Error(f"PolicyLM endpoint returned non-JSON response: {resp.text[:500]}")
230
 
231
- flagged = bool(data.get("flagged", False))
232
- categories = data.get("categories", []) or []
 
 
 
 
 
 
 
 
233
  label = 1 if flagged else 0
234
 
235
  cutoff = POLICYLM_CUTOFFS.get(POLICYLM_DEFAULT_PRESET)
 
228
  except Exception:
229
  raise gr.Error(f"PolicyLM endpoint returned non-JSON response: {resp.text[:500]}")
230
 
231
+ # Our handler always returns ≥1 category; anything else must not read as "no violation".
232
+ if not isinstance(data, dict) or "flagged" not in data or not data.get("categories"):
233
+ raise gr.Error(
234
+ "PolicyLM endpoint returned an unexpected response (expected flagged + non-empty "
235
+ "categories; is it running policylm_handler.py as handler.py?): "
236
+ f"{resp.text[:500]}"
237
+ )
238
+
239
+ flagged = bool(data["flagged"])
240
+ categories = data["categories"]
241
  label = 1 if flagged else 0
242
 
243
  cutoff = POLICYLM_CUTOFFS.get(POLICYLM_DEFAULT_PRESET)