Megatron-LM icon indicating copy to clipboard operation
Megatron-LM copied to clipboard

[BUG] Qwen3-Coder parser treats function tags inside string arguments as call boundaries

Open deavgprime opened this issue 1 month ago • 0 comments

Describe the bug

The Qwen3-Coder tool parser treats </function> inside a string parameter as the end of the outer function. It drops that parameter and any later parameters. If the string also contains <function=other></function>, the parser returns an additional call named other even though that text belongs to the original argument.

This can change file contents or commands returned by the generation server before an agent or evaluator receives them. I reproduced the parser output only; I did not execute any generated tool calls.

@NVIDIA/mcore-oncall

Steps/Code to reproduce bug

In a configured Megatron environment:

import json
from megatron.core.tokenizers.text.parsers.qwen3_coder_tool_parser import Qwen3CoderToolParser

tools = [{"type": "function", "function": {
    "name": "write_file",
    "parameters": {
        "type": "object",
        "properties": {"content": {"type": "string"}, "path": {"type": "string"}},
        "required": ["content", "path"],
    },
}}]

for value in ("plain text", "</tool_call>", "</function>",
              "</function><function=other></function>"):
    text = (
        "<tool_call><function=write_file><parameter=content>" + value
        + "</parameter><parameter=path>example.txt</parameter></function></tool_call>"
    )
    _, parsed = Qwen3CoderToolParser.parse(text, tools=tools)
    print(value, [(call["function"]["name"], json.loads(call["function"]["arguments"]))
                  for call in parsed["tool_calls"]])

The first two cases preserve both arguments. The last two return:

</function> [('write_file', {})]
</function><function=other></function> [('write_file', {}), ('other', {})]

Expected behavior

Each input should produce one write_file call with content equal to the supplied string and path equal to example.txt. Function-looking text inside a closed string parameter should stay in that parameter, like the already supported literal </tool_call> case.

Additional context

  • Main revision: 124c43802c0174c813cb7303c0168d93d0fdb86f.
  • Linux, Python 3.12.3, CPU only. For local validation I loaded the unmodified base_parser.py and qwen3_coder_tool_parser.py modules directly to avoid GPU package initialization. No training or serving integration test was run.
  • Eight parser cases repeated four times with the same results: four lose arguments or add a call; four controls preserve the expected output. Controls cover ordinary text, Unicode, literal </tool_call>, and an opening <function=literal> without a closing tag.
  • _find_tool_call_end() skips complete parameter blocks, but tool_call_function_regex subsequently ends the function at the first </function> without considering parameter boundaries.
  • Related work: merged #7320 addressed literal </tool_call> within parameters. Open #7193 concerns parser parity and coercion; its diff does not change this function-boundary extraction.

I can prepare a focused fix with regression tests if this scope is wanted.

deavgprime avatar Oct 31 '26 06:10 deavgprime