Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.
Python cliconsumer-gpudpofine-tuningggufhuggingfacellmllmopslocal-ailocal-llmloralow-vrammachine-learningollamapeftpythonpytorchqlorasfttransformers
Fix #1434: soup expect fails closed on BOM, malformed and non-object JSONL rows (#1457)
Kratos0ae08c7fbbf4
diff --git a/changelog.d/0.75.1/1457.fixed.md b/changelog.d/0.75.1/1457.fixed.mdnew file mode 100644index 00000000..290d9490--- /dev/null+++ b/changelog.d/0.75.1/1457.fixed.md@@ -0,0 +1 @@+- **`soup expect` fails closed on unreadable JSONL input (#1434 by @wanjinhao1 in #1457).** The gate now reads the dataset as `utf-8-sig` like the training loader, so a UTF-8 BOM no longer hides the first row from PII and other checks. Malformed JSON lines and valid JSON that is not an object are refused as an input error (exit 3) with their line numbers instead of being silently skipped, and a file with no checkable rows is refused rather than passing with `Rows 0`.diff --git a/docs/compliance.md b/docs/compliance.mdindex c4752148..bbfe9f2a 100644--- a/docs/compliance.md+++ b/docs/compliance.md@@ -149,7 +149,7 @@ Scanned fields per format:`expect_no_refusal_pattern` reads only the assistant side: assistant/gpt turns, DPO `chosen`, KTO `completion`, Alpaca `output`/`response`, RAFT `answer`, PRM `completions`, `segments` with `label: true`, and `text` for plaintext rows. A row with text but no assistant side passes it.-Empty-text safeguard: rows containing no extractable text fail closed on all expectation gates to prevent unparseable or blank rows from passing CI.+Empty-text safeguard: rows containing no extractable text fail closed on all expectation gates to prevent unparseable or blank rows from passing CI. Malformed JSON lines and valid JSON that is not an object are refused as an input error (exit 3) with their line numbers, a UTF-8 BOM is stripped the way the training loader strips it, and a file that yields no checkable rows is refused rather than passed.Every path is shell-quoted and validated to stay under the repo root, so therendered workflow is injection-safe. Edit the paths to match your repo.diff --git a/docs/data.md b/docs/data.mdindex 6cc74db3..8e0c06e2 100644--- a/docs/data.md+++ b/docs/data.md@@ -233,6 +233,7 @@ EOFsoup expect data.jsonl suite.yaml # exit 2 on suite failure# Inspects ChatML, ShareGPT, DPO (chosen/rejected), KTO (completion), Alpaca,# and sentence embedding formats. Fails closed if any row yields zero extractable text.+# Malformed or non-object lines and files with no checkable rows are refused with exit 3.# Magpie synthetic data — chat-template-prefix harvest (live, v0.71.6)soup data gen-magpie --base meta-llama/Llama-3.1-8B-Instruct \diff --git a/src/soup_cli/commands/expect.py b/src/soup_cli/commands/expect.pyindex c91aab28..45e29f82 100644--- a/src/soup_cli/commands/expect.py+++ b/src/soup_cli/commands/expect.py@@ -44,8 +44,8 @@ def _load_jsonl_rows(data_path: str) -> List[Mapping[str, object]]:if os.path.getsize(real) > _MAX_DATA_BYTES:raise ValueError(f"data file exceeds {_MAX_DATA_BYTES} bytes")rows: List[Mapping[str, object]] = []- skipped = 0- with open(real, "r", encoding="utf-8") as handle:+ uncheckable: List[str] = []+ with open(real, "r", encoding="utf-8-sig") as handle:for line_no, line in enumerate(handle, start=1):if not line.strip():continue@@ -54,13 +54,24 @@ def _load_jsonl_rows(data_path: str) -> List[Mapping[str, object]]:try:row = json.loads(line)except json.JSONDecodeError:- skipped += 1+ uncheckable.append(f"line {line_no}: malformed JSON")continueif isinstance(row, dict):rows.append(row)- if skipped:- console.print(- f"[yellow]Note: skipped {skipped} malformed JSONL line(s)[/]"+ else:+ uncheckable.append(f"line {line_no}: not a JSON object")+ if uncheckable:+ shown = uncheckable[:10]+ more = f" (+{len(uncheckable) - 10} more)" if len(uncheckable) > 10 else ""+ raise ValueError(+ f"refusing {data_path}: {len(uncheckable)} line(s) could not be checked: "+ + "; ".join(shown)+ + more+ )+ if not rows:+ raise ValueError(+ f"refusing {data_path}: no checkable rows were found; "+ "a run that checked zero rows cannot pass")return rowsdiff --git a/tests/test_issue1434_expect_fail_closed.py b/tests/test_issue1434_expect_fail_closed.pynew file mode 100644index 00000000..cb0a48d9--- /dev/null+++ b/tests/test_issue1434_expect_fail_closed.py@@ -0,0 +1,99 @@+"""Tests for Issue #1434: ``soup expect`` fails closed on unreadable JSONL input.++A UTF-8 BOM must be stripped like the training loader does, malformed JSON+lines and non-object rows must be reported (not silently dropped), and a run+that checked zero rows must not pass.+"""++from __future__ import annotations++from pathlib import Path++import pytest+from typer.testing import CliRunner++from soup_cli.cli import app+from tests.conftest import strip_ansi++PII_LINE = b'{"text": "mail [email protected]"}\n'+SUITE_YAML = "expectations:\n - name: expect_no_pii\n"++FILES = {+ "bom.jsonl": b"\xef\xbb\xbf" + PII_LINE + b'{"text": "hi"}\n',+ "nobom.jsonl": PII_LINE + b'{"text": "hi"}\n',+ "array_per_line.jsonl": b'[{"text": "mail [email protected]"}]\n',+ "non_object_rows.jsonl": b'"mail [email protected]"\n42\nnull\n',+ "all_malformed.jsonl": b"{not json\n{still: not}\n",+ "empty.jsonl": b"",+}+++def _run(tmp_path: Path, monkeypatch: pytest.MonkeyPatch, name: str, content: bytes):+ (tmp_path / "suite.yaml").write_text(SUITE_YAML, encoding="utf-8")+ (tmp_path / name).write_bytes(content)+ monkeypatch.chdir(tmp_path)+ return CliRunner().invoke(app, ["expect", name, "suite.yaml"])+++def test_bom_file_matches_no_bom_control(tmp_path, monkeypatch):+ """The BOM-prefixed file reports the PII in row 1 and exits 2, like the control."""+ with_bom = _run(tmp_path, monkeypatch, "bom.jsonl", FILES["bom.jsonl"])+ assert with_bom.exit_code == 2+ clean = " ".join(strip_ansi(with_bom.output).split())+ assert "[email protected]" in clean or "FAIL" in clean++ control = _run(tmp_path, monkeypatch, "nobom.jsonl", FILES["nobom.jsonl"])+ assert control.exit_code == 2+ clean = " ".join(strip_ansi(control.output).split())+ assert "[email protected]" in clean or "FAIL" in clean+++ "name, expected_fragment",+ [+ ("empty.jsonl", "no checkable rows"),+ ("all_malformed.jsonl", "malformed JSON"),+ ("array_per_line.jsonl", "not a JSON object"),+ ("non_object_rows.jsonl", "not a JSON object"),+ ],+)+def test_uncheckable_files_fail_nonzero(tmp_path, monkeypatch, name, expected_fragment):+ """Files with no checkable row exit non-zero and say why lines could not be checked."""+ result = _run(tmp_path, monkeypatch, name, FILES[name])+ assert result.exit_code == 3+ clean = " ".join(strip_ansi(result.output).split())+ assert expected_fragment in clean+ assert "PASS" not in clean+++def test_single_malformed_line_reported_with_line_number(tmp_path, monkeypatch):+ """One malformed line among valid rows is reported with its line number."""+ content = b'{"text": "hi"}\n{broken\n{"text": "ok"}\n'+ result = _run(tmp_path, monkeypatch, "mixed.jsonl", content)+ assert result.exit_code == 3+ clean = " ".join(strip_ansi(result.output).split())+ assert "line 2" in clean+ assert "malformed JSON" in clean+++def test_refusal_is_exit_3_not_a_suite_failure(tmp_path, monkeypatch):+ result = _run(tmp_path, monkeypatch, "bad.jsonl", b'{"text": "hi"}\n{broken\n')+ assert result.exit_code == 3, result.output+++def test_refusal_message_counts_the_uncheckable_lines(tmp_path, monkeypatch):+ result = _run(tmp_path, monkeypatch, "bad.jsonl", b"{not json\n{still: not}\n")+ assert result.exit_code == 3+ clean = " ".join(strip_ansi(result.output).split())+ assert "2 line(s) could not be checked" in clean+++def test_refusal_lists_at_most_ten_lines_and_counts_the_rest(tmp_path, monkeypatch):+ content = b'{"text": "ok"}\n' + b"".join(b"{bad%d\n" % i for i in range(12))+ result = _run(tmp_path, monkeypatch, "twelve.jsonl", content)+ assert result.exit_code == 3+ clean = " ".join(strip_ansi(result.output).split())+ assert "12 line(s) could not be checked" in clean+ assert "line 11: malformed JSON" in clean+ assert "line 12" not in clean+ assert "(+2 more)" in clean