Skip to content

Commit c7bf93d

Browse files
committed
fix(data): normalize null chat content
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
1 parent 49a164c commit c7bf93d

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎src/megatron/bridge/data/conversation_processing.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,8 @@ def normalize_chat_conversation(
578578
"""Normalize supported text-chat schemas to OpenAI-style messages.
579579
580580
Rows may use ``messages``, singular ``conversation``, or legacy plural
581-
``conversations`` with ``from``/``value`` keys.
581+
``conversations`` with ``from``/``value`` keys. Null OpenAI message content
582+
is converted to an empty string before chat-template rendering.
582583
"""
583584
if isinstance(example_or_conversation, Mapping):
584585
source = example_or_conversation
@@ -603,6 +604,8 @@ def normalize_chat_conversation(
603604
raise ValueError("Chat conversation turns must be dictionaries.")
604605
turn = dict(raw_turn)
605606
if "role" in turn and "content" in turn:
607+
if turn["content"] is None:
608+
turn["content"] = ""
606609
_normalize_assistant_tool_call_arguments(turn)
607610
normalized.append(turn)
608611
continue

‎tests/unit_tests/data/test_conversation_processing.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1339,7 +1339,7 @@ def test_shared_chat_preprocessing_normalizes_sharegpt_roles_before_templating()
13391339
assert tokenized.assistant_mask.any()
13401340

13411341

1342-
def test_normalize_chat_conversation_parses_openai_tool_call_arguments_without_mutating_input():
1342+
def test_normalize_chat_conversation_normalizes_openai_tool_calls_without_mutating_input():
13431343
row = {
13441344
"messages": [
13451345
{"role": "user", "content": "Weather?"},
@@ -1359,7 +1359,9 @@ def test_normalize_chat_conversation_parses_openai_tool_call_arguments_without_m
13591359

13601360
normalized = normalize_chat_conversation(row)
13611361

1362+
assert normalized[1]["content"] == ""
13621363
assert normalized[1]["tool_calls"][0]["function"]["arguments"] == {"city": "Seattle"}
1364+
assert row["messages"][1]["content"] is None
13631365
assert row["messages"][1]["tool_calls"][0]["function"]["arguments"] == '{"city":"Seattle"}'
13641366

13651367

‎tests/unit_tests/models/test_glm52_kimi_k3_minimax_m3_chat.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def _messages(*, tools: bool, thinking: bool) -> list[dict[str, Any]]:
157157
{
158158
"role": "assistant",
159159
"reasoning_content": "reason one" if thinking else None,
160-
"content": "",
160+
"content": None,
161161
"tool_calls": [{"function": {"name": "weather", "arguments": '{"city":"Seattle"}'}}],
162162
},
163163
{"role": "tool", "content": "12 C"},

0 commit comments

Comments
 (0)