From 8137d278db4add8f3a1118ebda5ee4186d029e38 Mon Sep 17 00:00:00 2001 From: marauder-actual Date: Mon, 25 May 2026 19:16:07 +0200 Subject: [PATCH] fix: parse tool_call arguments from JSON string to dict for Qwen3.5 template --- train_qwen35_27b.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/train_qwen35_27b.py b/train_qwen35_27b.py index 0bf7206..5ae44ff 100644 --- a/train_qwen35_27b.py +++ b/train_qwen35_27b.py @@ -54,9 +54,33 @@ model = FastLanguageModel.get_peft_model( # --- Dataset --- ds = load_dataset("json", data_files=DATA, split="train") +def fix_tool_calls(messages): + """Parse tool_call arguments from JSON strings to dicts for Qwen3.5 template.""" + import json as _json + fixed = [] + for msg in messages: + msg = dict(msg) + if msg.get("tool_calls"): + new_tcs = [] + for tc in msg["tool_calls"]: + tc = dict(tc) + if "function" in tc: + fn = dict(tc["function"]) + if isinstance(fn.get("arguments"), str): + try: + fn["arguments"] = _json.loads(fn["arguments"]) + except (ValueError, TypeError): + fn["arguments"] = {"raw": fn["arguments"]} + tc["function"] = fn + new_tcs.append(tc) + msg["tool_calls"] = new_tcs + fixed.append(msg) + return fixed + def to_chatml(ex): + messages = fix_tool_calls(ex["messages"]) text = tokenizer.apply_chat_template( - ex["messages"], tokenize=False, add_generation_prompt=False + messages, tokenize=False, add_generation_prompt=False ) return {"text": text}