Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Comments

Fix: handle multiple tool calls in qwen3_schema#4709

Merged
qgallouedec merged 5 commits intohuggingface:mainfrom
mattbui:fix-qwen3-schema-multi-tools
Dec 22, 2025
Merged

Fix: handle multiple tool calls in qwen3_schema#4709
qgallouedec merged 5 commits intohuggingface:mainfrom
mattbui:fix-qwen3-schema-multi-tools

Conversation

@mattbui
Copy link
Contributor

@mattbui mattbui commented Dec 17, 2025

What does this PR do?

Fixes #4708

Update qwen3_schema used in trl.chat_template_utils.parse_response to handle multiple tool calls in the response.

Main changes to the schema:

  • Update the main regex to capture everything from the first <tool_call> to the last </tool_call> into the tool_calls group
  • Add x-regex-iterator to iterate over that captured string, finding each <tool_call>...</tool_call> block and extracting the JSON content inside
  • Changed from [{type: 'function', function: @}] to {type: 'function', function: @} since we're now processing items individually, not wrapping in an array
from transformers import AutoTokenizer
from trl.chat_template_utils import parse_response

tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")

qwen3_schema = {
    "x-regex": r"^(?:<think>\n?(?P<reasoning_content>.+?)\n?</think>\s*)?(?P<content>.*?)(?=(?:<tool_call>|<\|im_end\|>|$))(?P<tool_calls>(?:<tool_call>.+?</tool_call>\s*)+)?\s*(?:<\|im_end\|>|$)",
    "type": "object",
    "properties": {
        "role": {"const": "assistant"},
        "content": {"type": "string"},
        "reasoning_content": {"type": "string"},
        "tool_calls": {
            "type": "array",
            "x-regex-iterator": r"<tool_call>\s*(.+?)\s*</tool_call>",
            "items": {
                "x-parser": "json",
                "x-parser-args": {"transform": "{type: 'function', function: @}"},
                "type": "object",
                "properties": {
                    "type": {"const": "function"},
                    "function": {
                        "type": "object",
                        "properties": {
                            "name": {"type": "string"},
                            "arguments": {
                                "type": "object",
                                "additionalProperties": {},
                            },
                        },
                    },
                },
            },
        },
    },
}

tokenizer.response_schema = qwen3_schema

text = """<tool_call>
{"name": "multiply", "arguments": {"a": 3, "b": 4}}
</tool_call>
<|im_end|>"""
parse_response(tokenizer, tokenizer(text)["input_ids"])
# {'role': 'assistant',
#  'content': '',
#  'tool_calls': [{'type': 'function',
#    'function': {'name': 'multiply', 'arguments': {'a': 3, 'b': 4}}}]}

text = """<tool_call>
{"name": "multiply", "arguments": {"a": 3, "b": 4}}
</tool_call>
<tool_call>
{"name": "addition", "arguments": {"a": 3, "b": 4}}
</tool_call>
<tool_call>
{"name": "divide","arguments": {"a": 3, "b": 4}}
</tool_call>
<|im_end|>"""
parse_response(tokenizer, tokenizer(text)["input_ids"])
# {
#     "role": "assistant",
#     "content": "",
#     "tool_calls": [
#         {"type": "function", "function": {"name": "multiply", "arguments": {"a": 3, "b": 4}}},
#         {"type": "function", "function": {"name": "addition", "arguments": {"a": 3, "b": 4}}},
#         {"type": "function", "function": {"name": "divide", "arguments": {"a": 3, "b": 4}}},
#     ],
# }

Before submitting

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@mattbui mattbui changed the title fix: multiple tool calls in qwen3 schema Fix: handle multiple tool calls in qwen3_schema Dec 17, 2025
@qgallouedec
Copy link
Member

Thanks! @Rocketknight1 do you mind having a quick look?

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, makes sense to me! But reminder that we will be overhauling the parse_response format probably in January, and we'll have to refactor these schemas then.

@qgallouedec
Copy link
Member

Thanks @Rocketknight1

But reminder that we will be overhauling the parse_response

Yes I'm following this closely 👍

@qgallouedec qgallouedec merged commit 22b386a into huggingface:main Dec 22, 2025
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

qwen3_schema fails to parse multiple parallel tool calls

4 participants