SFT double-encodes tool-call arguments for chat templates that apply `| tojson` (Qwen2.5, Llama 3.2)

Đang mở
#1,154 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@linye157 đang làm issue này rồi.

Từ ngày 4/11/2026.

  • #1157 của @linye157 — đang mở

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
50/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
machine-learning

Hướng nghiên cứu

Start with normalize_tool_call_arguments_for_chat_template in src/art_inference/chat_template.py, and check how it detects templates that iterate arguments (| items, is mapping). Run the reproduction script in the issue against a template that uses tool_call.arguments | tojson to confirm the double-encoded string. Done means that case renders a dict, while templates with an is string guard keep the exact string, and the tests in tests/unit/test_preprocessing_tokenize.py pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

When SFT data contains OpenAI-format tool calls, the arguments field is a JSON string (the shape shown in the SFT docs). normalize_tool_call_arguments_for_chat_template only turns it into a dict when the template iterates the arguments (| items, .items(), is mapping). Templates such as Qwen2.5-Instruct and Llama 3.2 Instruct render tool_call.arguments | tojson instead, so they get the string and tojson quotes it a second time:

"arguments": "{\"location\": \"Hobart\"}"

At inference time vLLM passes the arguments to the template as a dict, so the template renders "arguments": {"location": "Hobart"}. The SFT target then differs from what the same model sees and is supposed to produce.

The function and the unit tests are in src/art_inference/chat_template.py and tests/unit/test_preprocessing_tokenize.py.

normalize_tool_call_arguments_for_chat_template is also used for RL trajectory rendering and tinker server prompts, where templates with an is string guard must keep the exact sampled argument string, so any change to the detection has to leave those cases alone.

Reproduction

Requirements: jinja2==3.1.6. Set SRC to a checkout of the repo. The template below is the tool-call part of Qwen2.5-Instruct's chat template.

import importlib.util, os, sys
import jinja2
src = os.environ.get("SRC", "./src")
p = os.path.join(src, "src/art_inference/chat_template.py")
spec = importlib.util.spec_from_file_location("ct", p)
ct = importlib.util.module_from_spec(spec); spec.loader.exec_module(ct)

TEMPLATE = """{%- for message in messages %}{%- if message.role == "assistant" and message.tool_calls %}{%- for tool_call in message.tool_calls %}{%- if tool_call.function is defined %}{%- set tool_call = tool_call.function %}{%- endif %}
<tool_call>
{"name": "{{ tool_call.name }}", "arguments": {{ tool_call.arguments | tojson }}}
</tool_call>{%- endfor %}{%- endif %}{%- endfor %}"""
msgs = [{"role": "assistant", "content": None, "tool_calls": [
    {"id": "call_1", "type": "function",
     "function": {"name": "get_weather", "arguments": "{\"location\": \"Hobart\"}"}}]}]
env = jinja2.Environment()
env.policies["json.dumps_kwargs"] = {"sort_keys": False, "ensure_ascii": False}
def render(m): return env.from_string(TEMPLATE).render(messages=m)
norm = ct.normalize_tool_call_arguments_for_chat_template(msgs, TEMPLATE)
actual = render(norm)
expected = render([{"role": "assistant", "tool_calls": [{"function": {"name": "get_weather", "arguments": {"location": "Hobart"}}}]}])
print("expected:", repr(expected)); print("observed:", repr(actual))
if actual != expected:
    print("REPRODUCED"); sys.exit(1)

Expected

expected: '\n<tool_call>\n{"name": "get_weather", "arguments": {"location": "Hobart"}}\n</tool_call>'
observed: '\n<tool_call>\n{"name": "get_weather", "arguments": {"location": "Hobart"}}\n</tool_call>'

Actual

expected: '\n<tool_call>\n{"name": "get_weather", "arguments": {"location": "Hobart"}}\n</tool_call>'
observed: '\n<tool_call>\n{"name": "get_weather", "arguments": "{\\"location\\": \\"Hobart\\"}"}\n</tool_call>'

A second check, using the real Qwen2.5 tokenizer and tokenize_sft_batch, showed the trained (label != -100) text containing "arguments": "{\"location\": \"Hobart\"}".

I did not run an actual fine-tune.

Version

OpenPipe/ART at 87a8d7c9717f (main when checked on 2026-10-28, v0.5.23; Python 3.13). Still reproduces there; first seen at 51f3fbc9e2b1 (v0.5.21).

Related

#634 and #800 taught normalize_tool_call_arguments_for_chat_template to detect templates that iterate arguments (|items, Gemma's is mapping). This issue covers the remaining shape: templates that render tool_call.arguments | tojson still receive the JSON string, so it is encoded twice.

Ngôn ngữ chính
Python
Star
10.9k
Fork
1k
Merge trung bình
2 ngày 5 giờ
Pull request đã merge (30 ngày)
38

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của OpenPipe/ART

Tất cả issue của OpenPipe/ART

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.