fix: parse Qwen3 XML JSON arguments first (#43243)
Signed-off-by: Yufeng He <40085740+he-yufeng@users.noreply.github.com> Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
@@ -24,7 +24,10 @@ from vllm.tokenizers.detokenizer_utils import detokenize_incrementally
|
||||
from vllm.tool_parsers.qwen3coder_tool_parser import (
|
||||
Qwen3CoderToolParser,
|
||||
)
|
||||
from vllm.tool_parsers.qwen3xml_tool_parser import Qwen3XMLToolParser
|
||||
from vllm.tool_parsers.qwen3xml_tool_parser import (
|
||||
Qwen3XMLToolParser,
|
||||
StreamingXMLToolCallParser,
|
||||
)
|
||||
|
||||
MODEL = "Qwen/Qwen3-Coder-30B-A3B-Instruct-FP8"
|
||||
|
||||
@@ -72,6 +75,23 @@ AREA_PARAMS = {
|
||||
},
|
||||
}
|
||||
|
||||
QUESTION_PARAMS = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"questions": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"question": {"type": "string"},
|
||||
"multiSelect": {"type": "boolean"},
|
||||
"answer": {"type": "string"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(params=["chat_completion", "responses_api"])
|
||||
def sample_tools(request):
|
||||
@@ -148,6 +168,47 @@ def assert_tool_calls(
|
||||
)
|
||||
|
||||
|
||||
def test_qwen3xml_deferred_array_parses_json_literals():
|
||||
parser = StreamingXMLToolCallParser()
|
||||
parser.set_tools(
|
||||
[
|
||||
ChatCompletionToolsParam(
|
||||
type="function",
|
||||
function={
|
||||
"name": "AskUserQuestion",
|
||||
"parameters": QUESTION_PARAMS,
|
||||
},
|
||||
)
|
||||
]
|
||||
)
|
||||
|
||||
delta = parser.parse_single_streaming_chunks(
|
||||
"""<tool_call>
|
||||
<function=AskUserQuestion>
|
||||
<parameter=questions>
|
||||
[{"question": "Pick a color", "multiSelect": false, "answer": null}]
|
||||
</parameter>
|
||||
</function>
|
||||
</tool_call>"""
|
||||
)
|
||||
|
||||
arguments = "".join(
|
||||
tool_call.function.arguments or ""
|
||||
for tool_call in delta.tool_calls or []
|
||||
if tool_call.function and tool_call.function.arguments is not None
|
||||
)
|
||||
|
||||
assert json.loads(arguments) == {
|
||||
"questions": [
|
||||
{
|
||||
"question": "Pick a color",
|
||||
"multiSelect": False,
|
||||
"answer": None,
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def stream_delta_message_generator(
|
||||
qwen3_tool_parser,
|
||||
qwen3_tokenizer: TokenizerLike,
|
||||
|
||||
@@ -821,7 +821,10 @@ class StreamingXMLToolCallParser:
|
||||
raw_for_parse = raw_text + "\n"
|
||||
else:
|
||||
raw_for_parse = raw_text
|
||||
parsed_value = ast.literal_eval(raw_for_parse)
|
||||
try:
|
||||
parsed_value = json.loads(raw_for_parse)
|
||||
except json.JSONDecodeError:
|
||||
parsed_value = ast.literal_eval(raw_for_parse)
|
||||
output_arguments = json.dumps(parsed_value, ensure_ascii=False)
|
||||
except Exception:
|
||||
# Fallback: output as string as-is
|
||||
|
||||
Reference in New Issue
Block a user