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

未关闭
#1,154 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@linye157 已经在做这个了。

开始于 2026年11月4日。

  • #1157 来自 @linye157 —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
50/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Python
星标
10.9k
派生
1k
平均合并
2 天 5 小时
30 天内合并 PR
38

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

OpenPipe/ART 的其他 Issue

查看 OpenPipe/ART 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。