Skip to content

Commit be33978

Browse files
committed
fix: raise error in r1 serialization if none or empty thinking
1 parent 2415c94 commit be33978

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

libs/core/kiln_ai/adapters/fine_tune/dataset_formatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ def build_training_data(
128128

129129

130130
def serialize_r1_style_message(thinking: str | None, final_output: str):
131-
if thinking is None or len(thinking) == 0:
132-
return final_output
131+
if thinking is None or len(thinking.strip()) == 0:
132+
raise ValueError("Thinking data is required for R1 style")
133133

134134
return f"<think>{thinking}</think>{final_output}"
135135

libs/core/kiln_ai/adapters/fine_tune/test_dataset_formatter.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,13 +926,25 @@ def test_dataset_formatter_dump_to_file_json_schema_format(mock_dataset, tmp_pat
926926
@pytest.mark.parametrize(
927927
"thinking,final_output,expected_output",
928928
[
929-
(None, "final output", "final output"),
930-
("", "final output", "final output"),
931929
("thinking", "final output", "<think>thinking</think>final output"),
930+
("thinking", '{"name":"joe"}', '<think>thinking</think>{"name":"joe"}'),
932931
],
933932
)
934933
def test_serialize_r1_style_message(thinking, final_output, expected_output):
935934
assert (
936935
serialize_r1_style_message(thinking=thinking, final_output=final_output)
937936
== expected_output
938937
)
938+
939+
940+
@pytest.mark.parametrize(
941+
"thinking,final_output",
942+
[
943+
(None, "final output"),
944+
("", "final output"),
945+
(" ", "final output"),
946+
],
947+
)
948+
def test_serialize_r1_style_message_missing_thinking(thinking, final_output):
949+
with pytest.raises(ValueError, match="Thinking data is required for R1 style"):
950+
serialize_r1_style_message(thinking=thinking, final_output=final_output)

0 commit comments

Comments
 (0)