[Bugfix][ToolParser] Handle braces in required tool streaming strings (#45389)

Signed-off-by: Ting Sun <suntcrick@gmail.com>
Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
Ting SUN
2026-06-23 20:29:34 +00:00
committed by GitHub
co-authored by Flora Feng
parent 11b56b2ff2
commit 899d72a58c
2 changed files with 78 additions and 44 deletions
+35 -33
View File
@@ -280,15 +280,7 @@ def test_structured_outputs_json_without_parameters(
)
@pytest.mark.parametrize("output", VALID_TOOLS)
@pytest.mark.parametrize("empty_params", [False, True])
@pytest.mark.parametrize("delta_len", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
def test_streaming_output_valid(output, empty_params, delta_len):
output = deepcopy(output)
if empty_params:
output = [{"name": o["name"], "parameters": {}} for o in output]
output_json = json.dumps(output)
def _collect_required_tool_streaming_json(output_json: str, delta_len: int) -> str:
previous_text = ""
function_name_returned = False
messages = []
@@ -327,6 +319,38 @@ def test_streaming_output_valid(output, empty_params, delta_len):
else:
combined_messages += message.tool_calls[0].function.arguments
combined_messages += "}]"
return combined_messages
@pytest.mark.parametrize("output", VALID_TOOLS)
@pytest.mark.parametrize("empty_params", [False, True])
@pytest.mark.parametrize("delta_len", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
def test_streaming_output_valid(output, empty_params, delta_len):
output = deepcopy(output)
if empty_params:
output = [{"name": o["name"], "parameters": {}} for o in output]
output_json = json.dumps(output)
combined_messages = _collect_required_tool_streaming_json(output_json, delta_len)
assert json.loads(combined_messages) == output
assert json.dumps(json.loads(combined_messages)) == output_json
@pytest.mark.parametrize(
"city",
[
"a { b",
"a } b",
"a }} b",
'a " } b',
r"a \ } b",
],
)
@pytest.mark.parametrize("delta_len", [1, 2, 3, 8, 9999])
def test_streaming_output_valid_with_braces_in_string(city, delta_len):
output = [{"name": "get_current_weather", "parameters": {"city": city}}]
output_json = json.dumps(output)
combined_messages = _collect_required_tool_streaming_json(output_json, delta_len)
assert json.loads(combined_messages) == output
assert json.dumps(json.loads(combined_messages)) == output_json
@@ -334,30 +358,8 @@ def test_streaming_output_valid(output, empty_params, delta_len):
def test_streaming_output_valid_with_trailing_extra_data():
output = [{"name": "get_current_weather", "parameters": {"city": "Vienna"}}]
output_json = json.dumps(output) + "\nDONE"
previous_text = ""
function_name_returned = False
messages = []
delta_len = 3
for i in range(0, len(output_json), delta_len):
delta_text = output_json[i : i + delta_len]
current_text = previous_text + delta_text
delta_message, function_name_returned = extract_required_tool_call_streaming(
previous_text=previous_text,
current_text=current_text,
delta_text=delta_text,
function_name_returned=function_name_returned,
tool_call_idx=None,
tool_call_id_type="random",
)
if delta_message:
messages.append(delta_message)
previous_text = current_text
assert len(messages) > 0
combined_messages = _collect_required_tool_streaming_json(output_json, delta_len=3)
assert json.loads(combined_messages) == output
FUNCTION_TOOL = FunctionTool(