verl icon indicating copy to clipboard operation
verl copied to clipboard

[rollout] DeepSeek chat templates cannot render the dict tool-call arguments verl carries

Open ruiling-smartbear opened this issue 3 months ago • 0 comments

Summary

verl stores tool-call arguments as a dict throughout its message history (OpenAIFunctionCallSchema.arguments is a mapping and stays one through model_dump() — the shape the Transformers tool-calling docs recommend, with templates expected to render it via | tojson). The official DeepSeek chat templates (V3, R1, V3.1) instead splice arguments into the prompt by string concatenation:

{{'<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}

so any full-history render of a conversation that contains a tool-call turn raises:

messages = [
    {"role": "user", "content": "Weather in Seattle?"},
    {"role": "assistant", "content": "", "tool_calls": [
        {"id": "c0", "type": "function",
         "function": {"name": "get_weather", "arguments": {"city": "Seattle"}}}]},
    {"role": "tool", "content": "rain", "tool_call_id": "c0"},
]
tokenizer.apply_chat_template(messages, tokenize=True)
# TypeError: can only concatenate str (not "dict") to str

Reproduced against the official templates of DeepSeek-R1, DeepSeek-V3, and DeepSeek-V3.1 (transformers 5.12.1). This is an interface mismatch rather than a verl bug — DeepSeek's own API contract types arguments as a JSON string — and it is documented outside verl: deepseek-ai/DeepSeek-V3#860 is this exact error, huggingface/transformers#45419 surveys templates and finds DeepSeek V3/R1 accept str but not dict, and TRL ships a conversion fallback in trl/chat_template_utils.py for the same reason.

Where it bites in verl

As far as I can tell, the affected surfaces on current main are the ones that re-render a full history through the chat template:

  • Initial prompt rendering for datasets whose prompts already contain tool-call turns (ContinuousTokenBuilder.build_initial_tokens renders the raw prompt through apply_chat_template), and data.filter_overlong_prompts (verl/utils/tokenizer/deepseek.py's module docstring already notes this path calls apply_chat_template directly).
  • scripts/chat_template_checker.py: every tool trajectory fails at the ground-truth render, so the checker currently cannot validate the DeepSeek family at all. The failure is additionally masked: the dummy-user retry in verl/utils/tokenizer/chat_template.py fails too and raises its own error about the dummy message's list content, hiding the original dict TypeError.
  • Any replay-style consistency check (the tokenization_sanity_check_mode mechanism discussed in #7662) hits the same TypeError the moment it runs in the active path.

Not affected: the Continuous Token builders' incremental renders (the DeepSeek builder already carries its synthetic tool-call arguments as "{}" strings), and tool execution itself (parsers hand tools parsed arguments).

Suggested fix

Keep the internal history as dicts (other families' templates render them via | tojson, and tools consume them structurally) and adapt at the render boundary, as TRL does: when a render raises TypeError, retry with a copy of the messages whose dict arguments are JSON-serialized, and have the dummy-user retry in verl/utils/tokenizer/chat_template.py re-raise the original error when it fails as well, so the real cause stays visible. I have this working (it takes the checker from all-error to green on R1/V3/V3.1) and can send a PR.

ruiling-smartbear avatar Sep 02 '26 00:09 ruiling-smartbear