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

Aperta
#1,154 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@linye157 ci sta già lavorando.

Dal 4/11/2026.

  • #1157 di @linye157 — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
50/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
10.9k
Fork
1k
Merge medio
2g 5h
PR unite (30g)
38

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di OpenPipe/ART

Tutte le issue di OpenPipe/ART

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.