From cb15f9c1bd4783a13d8a4e522a75398b1c9084ca Mon Sep 17 00:00:00 2001 From: biefan <70761325+biefan@users.noreply.github.com> Date: Mon, 16 Mar 2026 20:49:18 +0000 Subject: [PATCH] ignore blank lines when reading JSONL --- pyrit/common/json_helper.py | 2 +- tests/unit/common/test_json_helper.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 tests/unit/common/test_json_helper.py diff --git a/pyrit/common/json_helper.py b/pyrit/common/json_helper.py index 17b49335de..cbb0575e5a 100644 --- a/pyrit/common/json_helper.py +++ b/pyrit/common/json_helper.py @@ -33,7 +33,7 @@ def read_jsonl(file: IO[Any]) -> list[dict[str, str]]: Returns: List[Dict[str, str]]: Parsed JSONL content. """ - return [json.loads(line) for line in file] + return [json.loads(line) for line in file if line.strip()] def write_jsonl(file: IO[Any], examples: list[dict[str, str]]) -> None: diff --git a/tests/unit/common/test_json_helper.py b/tests/unit/common/test_json_helper.py new file mode 100644 index 0000000000..4e8dddc765 --- /dev/null +++ b/tests/unit/common/test_json_helper.py @@ -0,0 +1,14 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT license. + +from io import StringIO + +from pyrit.common.json_helper import read_jsonl + + +def test_read_jsonl_ignores_blank_lines(): + file = StringIO('{"prompt": "first"}\n\n{"prompt": "second"}\n') + + result = read_jsonl(file) + + assert result == [{"prompt": "first"}, {"prompt": "second"}]