diff --git a/src/tsmm/eval/parse_answer.py b/src/tsmm/eval/parse_answer.py index 3906ed1..9921d0f 100644 --- a/src/tsmm/eval/parse_answer.py +++ b/src/tsmm/eval/parse_answer.py @@ -79,17 +79,22 @@ def _seg_from_entry(s): Accepts either ``[start, end]`` lists/tuples or ``{"start": s, "end": e}`` objects (the schema emitted by ``data/instruct.py``). Returns None if no - usable pair is found. + usable pair is found, or if the values aren't ints (e.g. a pure-LLM emits + ``["CPU Usage", 30]`` — junk that must not crash the eval pipeline). """ - if isinstance(s, dict): - if "start" in s and "end" in s: - return (int(s["start"]), int(s["end"])) + try: + if isinstance(s, dict): + if "start" in s and "end" in s: + return (int(s["start"]), int(s["end"])) + return None + if isinstance(s, (list, tuple)): + if len(s) >= 2: + return _normalize_seg(s) + if len(s) == 1: + v = int(s[0]); return (v, v) + except (ValueError, TypeError): return None - if isinstance(s, (list, tuple)): - if len(s) >= 2: - return _normalize_seg(s) - if len(s) == 1: - v = int(s[0]); return (v, v) + return None return None diff --git a/tests/test_parse_answer.py b/tests/test_parse_answer.py index 160c9ce..c90b311 100644 --- a/tests/test_parse_answer.py +++ b/tests/test_parse_answer.py @@ -73,6 +73,18 @@ class TestParseSegments: assert any('"a"' in b and '"b"' in b for b in blobs) assert '{"d": 4}' in blobs + def test_non_numeric_segment_entries_are_skipped(self): + # a pure-LLM may emit junk like ["CPU Usage", 30] — must not crash, just skip + from tsmm.eval.parse_answer import _seg_from_entry + assert _seg_from_entry(["CPU Usage", 30]) is None + assert _seg_from_entry({"start": "foo", "end": "bar"}) is None + assert _seg_from_entry("not a segment") is None + # good entries alongside junk are still extracted + text = '{"segments": [["CPU Usage", 30], [10, 20]]}' + segs, ok = parse_anomaly_segments(text) + assert ok is True + assert segs == [(10, 20)] + def test_empty_segments_list(self): text = '{"segments": [], "type": "none"}' segs, ok = parse_anomaly_segments(text)