[Frontend] Port Step-3.5 parsers to the streaming parser engine (#59321)

Signed-off-by: sfeng33 <4florafeng@gmail.com>
This commit is contained in:
Flora Feng
2026-10-01 22:18:37 +00:00
committed by GitHub
parent ca65eb67d9
commit a5105dba05
12 changed files with 236 additions and 1647 deletions
+12 -57
View File
@@ -847,7 +847,7 @@ TX
assert chunk.tool_calls[0].function.name == "get_current_weather"
assert chunk.tool_calls[0].type == "function"
# Empty initially
assert chunk.tool_calls[0].function.arguments == ""
assert not chunk.tool_calls[0].function.arguments
break
assert header_found
@@ -867,43 +867,6 @@ TX
assert parsed_args["state"] == "TX"
def test_extract_tool_calls_complex_type_with_single_quote(step3p5_tokenizer):
"""Test parameter type conversion based on tool schema."""
tools = [
ChatCompletionToolsParam(
type="function",
function={
"name": "test_types",
"parameters": {
"type": "object",
"properties": {
"int_param": {"type": "integer"},
"float_param": {"type": "float"},
"bool_param": {"type": "boolean"},
"str_param": {"type": "string"},
"obj_param": {"type": "object"},
},
},
},
)
]
model_output = """<tool_call>
<function=test_types>
<parameter=obj_param>
{'key': 'value'}
</parameter>
</function>
</tool_call>"""
parser = Step3p5ToolParser(step3p5_tokenizer, tools=tools)
request = ChatCompletionRequest(model=MODEL, messages=[], tools=tools)
extracted_tool_calls = parser.extract_tool_calls(model_output, request=request)
args = json.loads(extracted_tool_calls.tool_calls[0].function.arguments)
assert args["obj_param"] == {"key": "value"}
def test_extract_tool_calls_streaming_mixed_content_and_multiple_tool_calls(
step3p5_tool_parser, step3p5_tokenizer, sample_tools
):
@@ -1016,8 +979,8 @@ def test_extract_tool_calls_non_streaming_mixed_content_and_multiple_tool_calls(
"""Test non-streaming extraction with mixed content and multiple tool calls.
Scenario: Model outputs "hello" + complete tool call + "hi" + complete tool call.
Expected: "hello" as content, first tool call parsed (index=0), "hi" as content,
second tool call parsed (index=1)
Expected: "hello" as content, first tool call parsed (index=0), second tool call
parsed (index=1). Non-streaming content stops at the first tool call.
"""
# Model output: hello + complete tool call + hi + complete tool call
model_output = """hello<tool_call>
@@ -1066,17 +1029,7 @@ rectangle
assert args_dict_1["dimensions"]["width"] == 10
assert args_dict_1["dimensions"]["height"] == 5
# Verify content: should contain "hello", "hi"
assert extracted_tool_calls.content is not None
assert "hello" in extracted_tool_calls.content, "Should contain 'hello' as content"
assert "hi" in extracted_tool_calls.content, "Should contain 'hi' as content"
# Verify the order: hello should come first, then hi
hello_index = extracted_tool_calls.content.find("hello")
hi_index = extracted_tool_calls.content.find("hi")
assert hello_index >= 0, "'hello' should be in content"
assert hi_index > hello_index, "'hi' should come after 'hello'"
assert extracted_tool_calls.content == "hello"
# Verify that tool call tags are NOT in the content
assert "<function=get_current_weather>" not in extracted_tool_calls.content, (
@@ -1155,12 +1108,14 @@ rectangle
request=request,
)
# Process delta result
if delta_result:
if delta_result.content:
other_content += delta_result.content
if delta_result.tool_calls:
for tool_call in delta_result.tool_calls:
# Serving flushes the parser at the end of the stream.
for delta in (delta_result, step3p5_tool_parser.finish_streaming()):
if not delta:
continue
if delta.content:
other_content += delta.content
if delta.tool_calls:
for tool_call in delta.tool_calls:
idx = tool_call.index
if idx not in tool_states:
tool_states[idx] = {