diff --git a/sbas/views.py b/sbas/views.py index 4d8cf00e..40a35d46 100644 --- a/sbas/views.py +++ b/sbas/views.py @@ -570,6 +570,28 @@ def import_llm_questions(request): new_s, n = pattern.subn(r"\\\\", s) return new_s, n + def _strip_numeric_refs_in_payload(p): + """Return a copy of payload with inline numeric refs removed from any '*_feedback' or 'feedback' fields. + Also return a list of keys that were modified. + """ + if not isinstance(p, dict): + return p, [] + cleaned = dict(p) + changed = [] + # pattern matches [1] or [1,2, 3] with optional spaces + pattern = re.compile(r"\[\s*\d+(?:\s*,\s*\d+)*\s*\]") + for k, v in list(p.items()): + if not isinstance(v, str): + continue + if k.endswith("_feedback") or k == "feedback": + newv = pattern.sub("", v).strip() + # collapse multiple spaces + newv = re.sub(r"\s{2,}", " ", newv) + if newv != v: + cleaned[k] = newv + changed.append(k) + return cleaned, changed + raw_text_stripped = raw_text.strip() raw_text, sanitized_count = _sanitize_backslashes(raw_text_stripped) logger.debug("import_llm_questions: sanitized input length %d (escaped %d backslashes)", len(raw_text), sanitized_count) @@ -638,6 +660,12 @@ def import_llm_questions(request): for idx, payload in enumerate(candidates): item_report = {"index": idx, "status": None, "errors": [], "missing_m2m": [], "resolved_m2m": {}, "payload": payload} + # Strip numeric bracket references from feedback fields (e.g., [1], [1,2]) and record which fields were changed + cleaned_payload, stripped = _strip_numeric_refs_in_payload(payload) + if stripped: + item_report['stripped_fields'] = stripped + payload = cleaned_payload + # Collect validation errors validation_errors = list(validator.iter_errors(payload)) if validation_errors: @@ -919,7 +947,10 @@ def import_llm_confirm(request): except Exception: errors.append({"index": idx, "errors": ["Missing candidate"]}) continue - + # Strip numeric bracket references here as well, before confirm import + payload, stripped = _strip_numeric_refs_in_payload(payload) + if stripped: + logger.debug("import_llm_confirm: stripped numeric refs for idx=%s fields=%s", idx, stripped) try: with transaction.atomic(): q = Question()