test_minimax_tool_parser.py 51.9 KB
Newer Older
1
# SPDX-License-Identifier: Apache-2.0
2
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3
4
5
# ruff: noqa: E501

import json
6
from typing import Any
7
8
9

import pytest

10
11
12
13
14
from vllm.entrypoints.openai.protocol import (
    ChatCompletionToolsParam,
    FunctionCall,
    ToolCall,
)
15
from vllm.tokenizers import get_tokenizer
16
from vllm.tool_parsers.minimax_tool_parser import MinimaxToolParser
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

# Use a common model that is likely to be available
MODEL = "MiniMaxAi/MiniMax-M1-40k"


@pytest.fixture(scope="module")
def minimax_tokenizer():
    return get_tokenizer(tokenizer_name=MODEL)


@pytest.fixture
def minimax_tool_parser(minimax_tokenizer):
    return MinimaxToolParser(minimax_tokenizer)


32
33
34
@pytest.fixture
def sample_tools():
    return [
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        ChatCompletionToolsParam(
            type="function",
            function={
                "name": "get_current_weather",
                "description": "Get the current weather",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "city": {"type": "string", "description": "The city name"},
                        "state": {"type": "string", "description": "The state code"},
                        "unit": {"type": "string", "enum": ["fahrenheit", "celsius"]},
                    },
                    "required": ["city", "state"],
                },
            },
        ),
        ChatCompletionToolsParam(
            type="function",
            function={
                "name": "calculate_area",
                "description": "Calculate area of a shape",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "shape": {"type": "string"},
                        "dimensions": {"type": "object"},
                        "precision": {"type": "integer"},
                    },
                },
            },
        ),
66
67
68
    ]


69
70
71
def assert_tool_calls(
    actual_tool_calls: list[ToolCall], expected_tool_calls: list[ToolCall]
):
72
73
    assert len(actual_tool_calls) == len(expected_tool_calls)

74
75
76
    for actual_tool_call, expected_tool_call in zip(
        actual_tool_calls, expected_tool_calls
    ):
77
78
79
80
81
82
83
84
85
86
        assert isinstance(actual_tool_call.id, str)
        assert len(actual_tool_call.id) > 16

        assert actual_tool_call.type == "function"
        assert actual_tool_call.function == expected_tool_call.function


def test_extract_tool_calls_no_tools(minimax_tool_parser):
    model_output = "This is a test"
    extracted_tool_calls = minimax_tool_parser.extract_tool_calls(
87
88
        model_output, request=None
    )  # type: ignore[arg-type]
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    assert not extracted_tool_calls.tools_called
    assert extracted_tool_calls.tool_calls == []
    assert extracted_tool_calls.content == model_output


@pytest.mark.parametrize(
    ids=[
        "single_tool_call",
        "multiple_tool_calls",
        "tool_call_with_content_before",
        "tool_call_with_single_line_json",
        "tool_call_incomplete_tag",
    ],
    argnames=["model_output", "expected_tool_calls", "expected_content"],
    argvalues=[
        (
            """<tool_calls>
{"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}}
</tool_calls>""",
            [
109
110
111
112
113
114
115
116
117
118
119
120
                ToolCall(
                    function=FunctionCall(
                        name="get_current_weather",
                        arguments=json.dumps(
                            {
                                "city": "Dallas",
                                "state": "TX",
                                "unit": "fahrenheit",
                            }
                        ),
                    )
                )
121
122
123
124
125
126
127
128
129
            ],
            None,
        ),
        (
            """<tool_calls>
{"name": "get_current_weather", "arguments": {"city": "Dallas", "state": "TX", "unit": "fahrenheit"}}
{"name": "get_current_weather", "arguments": {"city": "Orlando", "state": "FL", "unit": "fahrenheit"}}
</tool_calls>""",
            [
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
                ToolCall(
                    function=FunctionCall(
                        name="get_current_weather",
                        arguments=json.dumps(
                            {
                                "city": "Dallas",
                                "state": "TX",
                                "unit": "fahrenheit",
                            }
                        ),
                    )
                ),
                ToolCall(
                    function=FunctionCall(
                        name="get_current_weather",
                        arguments=json.dumps(
                            {
                                "city": "Orlando",
                                "state": "FL",
                                "unit": "fahrenheit",
                            }
                        ),
                    )
                ),
154
155
156
157
158
159
160
161
            ],
            None,
        ),
        (
            """I'll help you check the weather. <tool_calls>
{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}
</tool_calls>""",
            [
162
163
164
165
166
167
168
169
170
171
172
173
                ToolCall(
                    function=FunctionCall(
                        name="get_current_weather",
                        arguments=json.dumps(
                            {
                                "city": "Seattle",
                                "state": "WA",
                                "unit": "celsius",
                            }
                        ),
                    )
                )
174
175
176
177
178
179
180
181
            ],
            "I'll help you check the weather.",
        ),
        (
            """<tool_calls>
{"name": "get_current_weather", "arguments": {"city": "New York", "state": "NY", "unit": "celsius"}}
</tool_calls>""",
            [
182
183
184
185
186
187
188
189
190
191
192
193
                ToolCall(
                    function=FunctionCall(
                        name="get_current_weather",
                        arguments=json.dumps(
                            {
                                "city": "New York",
                                "state": "NY",
                                "unit": "celsius",
                            }
                        ),
                    )
                )
194
195
196
197
198
199
200
            ],
            None,
        ),
        (
            """<tool_calls>
{"name": "get_current_weather", "arguments": {"city": "Boston", "state": "MA"}}""",
            [
201
202
203
204
205
206
207
208
209
210
211
                ToolCall(
                    function=FunctionCall(
                        name="get_current_weather",
                        arguments=json.dumps(
                            {
                                "city": "Boston",
                                "state": "MA",
                            }
                        ),
                    )
                )
212
213
214
215
216
            ],
            None,
        ),
    ],
)
217
218
219
def test_extract_tool_calls(
    minimax_tool_parser, model_output, expected_tool_calls, expected_content
):
220
    extracted_tool_calls = minimax_tool_parser.extract_tool_calls(
221
222
        model_output, request=None
    )  # type: ignore[arg-type]
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
    assert extracted_tool_calls.tools_called

    assert_tool_calls(extracted_tool_calls.tool_calls, expected_tool_calls)

    assert extracted_tool_calls.content == expected_content


def test_preprocess_model_output_with_thinking_tags(minimax_tool_parser):
    """Test that tool calls within thinking tags are removed during preprocessing."""
    model_output = """<think>Let me think about this. <tool_calls>
{"name": "fake_tool", "arguments": {"param": "value"}}
</tool_calls> This should be removed.</think>

I'll help you with that. <tool_calls>
{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA"}}
</tool_calls>"""

240
    processed_output = minimax_tool_parser.preprocess_model_output(model_output)
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261

    # The tool call within thinking tags should be removed
    assert "fake_tool" not in processed_output
    # But the thinking tag itself should remain
    assert "<think>" in processed_output
    assert "</think>" in processed_output
    # The actual tool call outside thinking tags should remain
    assert "get_current_weather" in processed_output


def test_extract_tool_calls_with_thinking_tags(minimax_tool_parser):
    """Test tool extraction when thinking tags contain tool calls that should be ignored."""
    model_output = """<think>I should use a tool. <tool_calls>
{"name": "ignored_tool", "arguments": {"should": "ignore"}}
</tool_calls></think>

Let me help you with the weather. <tool_calls>
{"name": "get_current_weather", "arguments": {"city": "Miami", "state": "FL", "unit": "fahrenheit"}}
</tool_calls>"""

    extracted_tool_calls = minimax_tool_parser.extract_tool_calls(
262
263
        model_output, request=None
    )  # type: ignore[arg-type]
264
265
266

    assert extracted_tool_calls.tools_called
    assert len(extracted_tool_calls.tool_calls) == 1
267
    assert extracted_tool_calls.tool_calls[0].function.name == "get_current_weather"
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

    # Content extraction is based on the position of the first <tool_calls> in the original model_output
    # Since preprocessing removes tool calls within thinking tags, the actual first <tool_calls> is the external one
    expected_content = """<think>I should use a tool. <tool_calls>
{"name": "ignored_tool", "arguments": {"should": "ignore"}}
</tool_calls></think>

Let me help you with the weather."""
    assert extracted_tool_calls.content == expected_content


def test_extract_tool_calls_invalid_json(minimax_tool_parser):
    """Test that invalid JSON in tool calls is handled gracefully."""
    model_output = """<tool_calls>
{"name": "valid_tool", "arguments": {"city": "Seattle"}}
{invalid json here}
{"name": "another_valid_tool", "arguments": {"param": "value"}}
</tool_calls>"""

    extracted_tool_calls = minimax_tool_parser.extract_tool_calls(
288
289
        model_output, request=None
    )  # type: ignore[arg-type]
290
291
292
293
294

    assert extracted_tool_calls.tools_called
    # Should extract only the valid JSON tool calls
    assert len(extracted_tool_calls.tool_calls) == 2
    assert extracted_tool_calls.tool_calls[0].function.name == "valid_tool"
295
    assert extracted_tool_calls.tool_calls[1].function.name == "another_valid_tool"
296
297
298
299
300
301
302
303
304
305
306
307


def test_extract_tool_calls_missing_name_or_arguments(minimax_tool_parser):
    """Test that tool calls missing name or arguments are filtered out."""
    model_output = """<tool_calls>
{"name": "valid_tool", "arguments": {"city": "Seattle"}}
{"name": "missing_args"}
{"arguments": {"city": "Portland"}}
{"name": "another_valid_tool", "arguments": {"param": "value"}}
</tool_calls>"""

    extracted_tool_calls = minimax_tool_parser.extract_tool_calls(
308
309
        model_output, request=None
    )  # type: ignore[arg-type]
310
311
312
313
314

    assert extracted_tool_calls.tools_called
    # Should extract only the valid tool calls with both name and arguments
    assert len(extracted_tool_calls.tool_calls) == 2
    assert extracted_tool_calls.tool_calls[0].function.name == "valid_tool"
315
    assert extracted_tool_calls.tool_calls[1].function.name == "another_valid_tool"
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343


def test_streaming_basic_functionality(minimax_tool_parser):
    """Test basic streaming functionality."""
    # Reset streaming state
    minimax_tool_parser.current_tool_name_sent = False
    minimax_tool_parser.prev_tool_call_arr = []
    minimax_tool_parser.current_tool_id = -1
    minimax_tool_parser.streamed_args_for_tool = []

    # Test with a simple tool call
    current_text = """<tool_calls>
{"name": "get_current_weather", "arguments": {"city": "Seattle"}}
</tool_calls>"""

    # First call should handle the initial setup
    result = minimax_tool_parser.extract_tool_calls_streaming(
        previous_text="",
        current_text=current_text,
        delta_text="</tool_calls>",
        previous_token_ids=[],
        current_token_ids=[],
        delta_token_ids=[],
        request=None,
    )

    # The result might be None or contain tool call information
    # This depends on the internal state management
344
    if result is not None and hasattr(result, "tool_calls") and result.tool_calls:
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
        assert len(result.tool_calls) >= 0


def test_streaming_with_content_before_tool_calls(minimax_tool_parser):
    """Test streaming when there's content before tool calls."""
    # Reset streaming state
    minimax_tool_parser.current_tool_name_sent = False
    minimax_tool_parser.prev_tool_call_arr = []
    minimax_tool_parser.current_tool_id = -1
    minimax_tool_parser.streamed_args_for_tool = []

    current_text = "I'll help you with that. <tool_calls>"

    # When there's content before tool calls, it should be returned as content
    result = minimax_tool_parser.extract_tool_calls_streaming(
        previous_text="I'll help you",
        current_text=current_text,
        delta_text=" with that. <tool_calls>",
        previous_token_ids=[],
        current_token_ids=[],
        delta_token_ids=[],
        request=None,
    )

369
    if result is not None and hasattr(result, "content"):
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
        # Should contain some content
        assert result.content is not None


def test_streaming_no_tool_calls(minimax_tool_parser):
    """Test streaming when there are no tool calls."""
    current_text = "This is just regular text without any tool calls."

    result = minimax_tool_parser.extract_tool_calls_streaming(
        previous_text="This is just regular text",
        current_text=current_text,
        delta_text=" without any tool calls.",
        previous_token_ids=[],
        current_token_ids=[],
        delta_token_ids=[],
        request=None,
    )

    # Should return the delta text as content
    assert result is not None
390
    assert hasattr(result, "content")
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
    assert result.content == " without any tool calls."


def test_streaming_with_thinking_tags(minimax_tool_parser):
    """Test streaming with thinking tags that contain tool calls."""
    # Reset streaming state
    minimax_tool_parser.current_tool_name_sent = False
    minimax_tool_parser.prev_tool_call_arr = []
    minimax_tool_parser.current_tool_id = -1
    minimax_tool_parser.streamed_args_for_tool = []

    current_text = """<think><tool_calls>{"name": "ignored", "arguments": {}}</tool_calls></think><tool_calls>{"name": "real_tool", "arguments": {"param": "value"}}</tool_calls>"""

    result = minimax_tool_parser.extract_tool_calls_streaming(
        previous_text="",
        current_text=current_text,
        delta_text=current_text,
        previous_token_ids=[],
        current_token_ids=[],
        delta_token_ids=[],
        request=None,
    )

    # The preprocessing should remove tool calls from thinking tags
    # and only process the real tool call
416
    if result is not None and hasattr(result, "tool_calls") and result.tool_calls:
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
        for tool_call in result.tool_calls:
            assert tool_call.function.name != "ignored"


def test_extract_tool_calls_multiline_json_not_supported(minimax_tool_parser):
    """Test that multiline JSON in tool calls is not currently supported."""
    model_output = """<tool_calls>
{
  "name": "get_current_weather",
  "arguments": {
    "city": "New York",
    "state": "NY",
    "unit": "celsius"
  }
}
</tool_calls>"""

    extracted_tool_calls = minimax_tool_parser.extract_tool_calls(
435
436
        model_output, request=None
    )  # type: ignore[arg-type]
437
438
439
440
441

    # Multiline JSON is currently not supported, should return no tools called
    assert not extracted_tool_calls.tools_called
    assert extracted_tool_calls.tool_calls == []
    assert extracted_tool_calls.content is None
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465


def test_streaming_arguments_incremental_output(minimax_tool_parser):
    """Test that streaming arguments are returned incrementally, not cumulatively."""
    # Reset streaming state
    minimax_tool_parser.current_tool_name_sent = False
    minimax_tool_parser.prev_tool_call_arr = []
    minimax_tool_parser.current_tool_id = -1
    minimax_tool_parser.streamed_args_for_tool = []

    # Simulate progressive tool call building
    stages = [
        # Stage 1: Function name complete
        '<tool_calls>\n{"name": "get_current_weather", "arguments": ',
        # Stage 2: Arguments object starts with first key
        '<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": ',
        # Stage 3: First parameter value added
        '<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": "Seattle"',
        # Stage 4: Second parameter added
        '<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA"',
        # Stage 5: Third parameter added, arguments complete
        '<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}',
        # Stage 6: Tool calls closed
        '<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}\n</tool',
466
        '<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}\n</tool_calls>',
467
468
469
470
471
472
473
    ]

    function_name_sent = False
    previous_args_content = ""

    for i, current_text in enumerate(stages):
        previous_text = stages[i - 1] if i > 0 else ""
474
        delta_text = current_text[len(previous_text) :] if i > 0 else current_text
475
476
477
478
479
480
481
482
483
484
485
486
487
488

        result = minimax_tool_parser.extract_tool_calls_streaming(
            previous_text=previous_text,
            current_text=current_text,
            delta_text=delta_text,
            previous_token_ids=[],
            current_token_ids=[],
            delta_token_ids=[],
            request=None,
        )

        print(f"Stage {i}: Current text: {repr(current_text)}")
        print(f"Stage {i}: Delta text: {repr(delta_text)}")

489
        if result is not None and hasattr(result, "tool_calls") and result.tool_calls:
490
491
492
493
494
495
            tool_call = result.tool_calls[0]

            # Check if function name is sent (should happen only once)
            if tool_call.function and tool_call.function.name:
                assert tool_call.function.name == "get_current_weather"
                function_name_sent = True
496
                print(f"Stage {i}: Function name sent: {tool_call.function.name}")
497
498
499
500

            # Check if arguments are sent incrementally
            if tool_call.function and tool_call.function.arguments:
                args_fragment = tool_call.function.arguments
501
                print(f"Stage {i}: Got arguments fragment: {repr(args_fragment)}")
502
503
504
505
506

                # For incremental output, each fragment should be new content only
                # The fragment should not contain all previous content
                if i >= 2 and previous_args_content:  # After we start getting arguments
                    # The new fragment should not be identical to or contain all previous content
507
508
509
                    assert args_fragment != previous_args_content, (
                        f"Fragment should be incremental, not cumulative: {args_fragment}"
                    )
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532

                    # If this is truly incremental, the fragment should be relatively small
                    # compared to the complete arguments so far
                    if len(args_fragment) > len(previous_args_content):
                        print(
                            "Warning: Fragment seems cumulative rather than incremental"
                        )

                previous_args_content = args_fragment

    # Verify function name was sent at least once
    assert function_name_sent, "Function name should have been sent"


def test_streaming_arguments_delta_only(minimax_tool_parser):
    """Test that each streaming call returns only the delta (new part) of arguments."""
    # Reset streaming state
    minimax_tool_parser.current_tool_name_sent = False
    minimax_tool_parser.prev_tool_call_arr = []
    minimax_tool_parser.current_tool_id = -1
    minimax_tool_parser.streamed_args_for_tool = []

    # Simulate two consecutive calls with growing arguments
533
534
535
    call1_text = (
        '<tool_calls>\n{"name": "test_tool", "arguments": {"param1": "value1"}}'
    )
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
    call2_text = '<tool_calls>\n{"name": "test_tool", "arguments": {"param1": "value1", "param2": "value2"}}'

    print(f"Call 1 text: {repr(call1_text)}")
    print(f"Call 2 text: {repr(call2_text)}")

    # First call - should get the function name and initial arguments
    result1 = minimax_tool_parser.extract_tool_calls_streaming(
        previous_text="",
        current_text=call1_text,
        delta_text=call1_text,
        previous_token_ids=[],
        current_token_ids=[],
        delta_token_ids=[],
        request=None,
    )

    print(f"Result 1: {result1}")
553
    if result1 and hasattr(result1, "tool_calls") and result1.tool_calls:
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
        for i, tc in enumerate(result1.tool_calls):
            print(f"  Tool call {i}: {tc}")

    # Second call - should only get the delta (new part) of arguments
    result2 = minimax_tool_parser.extract_tool_calls_streaming(
        previous_text=call1_text,
        current_text=call2_text,
        delta_text=', "param2": "value2"}',
        previous_token_ids=[],
        current_token_ids=[],
        delta_token_ids=[],
        request=None,
    )

    print(f"Result 2: {result2}")
569
    if result2 and hasattr(result2, "tool_calls") and result2.tool_calls:
570
571
572
573
        for i, tc in enumerate(result2.tool_calls):
            print(f"  Tool call {i}: {tc}")

    # Verify the second call only returns the delta
574
    if result2 is not None and hasattr(result2, "tool_calls") and result2.tool_calls:
575
576
577
578
579
580
581
        tool_call = result2.tool_calls[0]
        if tool_call.function and tool_call.function.arguments:
            args_delta = tool_call.function.arguments
            print(f"Arguments delta from second call: {repr(args_delta)}")

            # Should only contain the new part, not the full arguments
            # The delta should be something like ', "param2": "value2"}' or just '"param2": "value2"'
582
583
584
585
            assert (
                ', "param2": "value2"}' in args_delta
                or '"param2": "value2"' in args_delta
            ), f"Expected delta containing param2, got: {args_delta}"
586
587

            # Should NOT contain the previous parameter data
588
589
590
            assert '"param1": "value1"' not in args_delta, (
                f"Arguments delta should not contain previous data: {args_delta}"
            )
591
592

            # The delta should be relatively short (incremental, not cumulative)
593
594
595
596
            expected_max_length = len(', "param2": "value2"}') + 10  # Some tolerance
            assert len(args_delta) <= expected_max_length, (
                f"Delta seems too long (possibly cumulative): {args_delta}"
            )
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622

            print("✓ Delta validation passed")
        else:
            print("No arguments in result2 tool call")
    else:
        print("No tool calls in result2 or result2 is None")
        # This might be acceptable if no incremental update is needed
        # But let's at least verify that result1 had some content
        assert result1 is not None, "At least the first call should return something"


def test_streaming_openai_compatibility(minimax_tool_parser):
    """Test that streaming behavior with buffering works correctly."""
    # Reset streaming state
    minimax_tool_parser.current_tool_name_sent = False
    minimax_tool_parser.prev_tool_call_arr = []
    minimax_tool_parser.current_tool_id = -1
    minimax_tool_parser.streamed_args_for_tool = []
    # Reset buffering state
    minimax_tool_parser.pending_buffer = ""
    minimax_tool_parser.in_thinking_tag = False
    minimax_tool_parser.thinking_depth = 0

    # Test scenario: simple buffering without complex tool call context
    test_cases: list[dict[str, Any]] = [
        {
623
624
625
626
627
            "stage": "Token: <",
            "previous": "",
            "current": "<",
            "delta": "<",
            "expected_content": None,  # Should be buffered
628
629
        },
        {
630
631
632
633
634
            "stage": "Token: tool_calls>",
            "previous": "<",
            "current": "<tool_calls>",
            "delta": "tool_calls>",
            "expected_content": None,  # Complete tag, should not output
635
636
        },
        {
637
638
639
640
641
            "stage": "Regular content",
            "previous": "Hello",
            "current": "Hello world",
            "delta": " world",
            "expected_content": " world",  # Normal content should pass through
642
643
        },
        {
644
645
646
647
648
            "stage": "Content with end tag start",
            "previous": "Text",
            "current": "Text content</tool_",
            "delta": " content</tool_",
            "expected_content": " content",  # Content part output, </tool_ buffered
649
650
        },
        {
651
652
653
654
655
            "stage": "Complete end tag",
            "previous": "Text content</tool_",
            "current": "Text content</tool_calls>",
            "delta": "calls>",
            "expected_content": None,  # Complete close tag, should not output
656
657
658
659
660
661
662
663
664
665
        },
    ]

    for i, test_case in enumerate(test_cases):
        print(f"\n--- Stage {i}: {test_case['stage']} ---")
        print(f"Previous: {repr(test_case['previous'])}")
        print(f"Current:  {repr(test_case['current'])}")
        print(f"Delta:    {repr(test_case['delta'])}")

        result = minimax_tool_parser.extract_tool_calls_streaming(
666
667
668
            previous_text=test_case["previous"],
            current_text=test_case["current"],
            delta_text=test_case["delta"],
669
670
671
672
673
674
675
676
677
            previous_token_ids=[],
            current_token_ids=[],
            delta_token_ids=[],
            request=None,
        )

        print(f"Result: {result}")

        # Check expected content
678
679
        if test_case["expected_content"] is None:
            assert result is None or not getattr(result, "content", None), (
680
                f"Stage {i}: Expected no content, got {result}"
681
            )
682
683
            print("✓ No content output as expected")
        else:
684
            assert result is not None and hasattr(result, "content"), (
685
                f"Stage {i}: Expected content, got {result}"
686
687
            )
            assert result.content == test_case["expected_content"], (
688
                f"Stage {i}: Expected content {test_case['expected_content']}, got {result.content}"
689
            )
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
            print(f"✓ Content matches: {repr(result.content)}")

    print("✓ Streaming test with buffering completed successfully")


def test_streaming_thinking_tag_buffering(minimax_tool_parser):
    """Test that tool calls within thinking tags are properly handled during streaming."""
    # Reset streaming state
    minimax_tool_parser.current_tool_name_sent = False
    minimax_tool_parser.prev_tool_call_arr = []
    minimax_tool_parser.current_tool_id = -1
    minimax_tool_parser.streamed_args_for_tool = []
    # Reset buffering state
    minimax_tool_parser.pending_buffer = ""
    minimax_tool_parser.in_thinking_tag = False
    minimax_tool_parser.thinking_depth = 0

    # Test scenario: tool calls within thinking tags should be ignored
    test_cases: list[dict[str, Any]] = [
        {
710
711
712
713
714
            "stage": "Start thinking",
            "previous": "",
            "current": "<think>I need to use a tool. <tool_calls>",
            "delta": "<think>I need to use a tool. <tool_calls>",
            "expected_content": "<think>I need to use a tool. <tool_calls>",  # Should pass through as content
715
716
        },
        {
717
718
719
720
721
            "stage": "Tool call in thinking",
            "previous": "<think>I need to use a tool. <tool_calls>",
            "current": '<think>I need to use a tool. <tool_calls>\n{"name": "ignored_tool", "arguments": {"param": "value"}}\n</tool_calls>',
            "delta": '\n{"name": "ignored_tool", "arguments": {"param": "value"}}\n</tool_calls>',
            "expected_content": '\n{"name": "ignored_tool", "arguments": {"param": "value"}}\n</tool_calls>',  # </tool_calls> should be preserved in thinking tags
722
723
        },
        {
724
725
726
727
728
729
            "stage": "Real tool call after thinking",
            "previous": '<think>I need to use a tool. <tool_calls>\n{"name": "ignored_tool", "arguments": {"param": "value"}}\n</tool_calls></think>',
            "current": '<think>I need to use a tool. <tool_calls>\n{"name": "ignored_tool", "arguments": {"param": "value"}}\n</tool_calls></think>\n<tool_calls>',
            "delta": "\n<tool_calls>",
            "expected_content": "\n",  # Should output '\n' and suppress <tool_calls>
        },
730
731
732
733
734
735
736
737
738
    ]

    for i, test_case in enumerate(test_cases):
        print(f"\n--- Stage {i}: {test_case['stage']} ---")
        print(f"Previous: {repr(test_case['previous'])}")
        print(f"Current:  {repr(test_case['current'])}")
        print(f"Delta:    {repr(test_case['delta'])}")

        result = minimax_tool_parser.extract_tool_calls_streaming(
739
740
741
            previous_text=test_case["previous"],
            current_text=test_case["current"],
            delta_text=test_case["delta"],
742
743
744
745
746
747
748
749
750
            previous_token_ids=[],
            current_token_ids=[],
            delta_token_ids=[],
            request=None,
        )

        print(f"Result: {result}")

        # Check expected content
751
752
753
        if "expected_content" in test_case:
            if test_case["expected_content"] is None:
                assert result is None or not getattr(result, "content", None), (
754
                    f"Stage {i}: Expected no content, got {result}"
755
                )
756
            else:
757
                assert result is not None and hasattr(result, "content"), (
758
                    f"Stage {i}: Expected content, got {result}"
759
760
                )
                assert result.content == test_case["expected_content"], (
761
                    f"Stage {i}: Expected content {test_case['expected_content']}, got {result.content}"
762
                )
763
764
765
                print(f"✓ Content matches: {repr(result.content)}")

        # Check tool calls
766
767
768
769
770
771
        if test_case.get("expected_tool_call"):
            assert (
                result is not None
                and hasattr(result, "tool_calls")
                and result.tool_calls
            ), f"Stage {i}: Expected tool call, got {result}"
772
773

            tool_call = result.tool_calls[0]
774
            assert tool_call.function.name == "real_tool", (
775
                f"Expected real_tool, got {tool_call.function.name}"
776
            )
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
            print(f"✓ Real tool call detected: {tool_call.function.name}")

    print("✓ Thinking tag buffering test completed successfully")


def reset_streaming_state(minimax_tool_parser):
    """Helper function to properly reset the streaming state for MinimaxToolParser."""
    # Reset minimax-specific state
    minimax_tool_parser._reset_streaming_state()

    # Reset base class state (these should still be reset for compatibility)
    minimax_tool_parser.prev_tool_call_arr = []
    minimax_tool_parser.current_tool_id = -1
    minimax_tool_parser.current_tool_name_sent = False
    minimax_tool_parser.streamed_args_for_tool = []


def test_streaming_complex_scenario_with_multiple_tools(minimax_tool_parser):
    """Test complex streaming scenario: tools inside <think> tags and multiple tool calls in one group."""
    # Reset streaming state
    reset_streaming_state(minimax_tool_parser)

    # Complex scenario: tools inside thinking tags and multiple tools in one group
    test_stages: list[dict[str, Any]] = [
        {
802
803
804
805
806
807
            "stage": "Initial content",
            "previous": "",
            "current": "Let me help you with this task.",
            "delta": "Let me help you with this task.",
            "expected_content": "Let me help you with this task.",
            "expected_tool_calls": 0,
808
809
        },
        {
810
811
812
813
814
815
            "stage": "Start thinking tag",
            "previous": "Let me help you with this task.",
            "current": "Let me help you with this task.<think>I need to analyze this situation first.",
            "delta": "<think>I need to analyze this situation first.",
            "expected_content": "<think>I need to analyze this situation first.",
            "expected_tool_calls": 0,
816
817
        },
        {
818
819
820
821
822
823
            "stage": "Tool call inside thinking tag starts",
            "previous": "Let me help you with this task.<think>I need to analyze this situation first.",
            "current": "Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>",
            "delta": "<tool_calls>",
            "expected_content": "<tool_calls>",  # Inside thinking tags, tool tags should be preserved as content
            "expected_tool_calls": 0,
824
825
        },
        {
826
827
828
829
830
831
            "stage": "Complete tool call inside thinking tag",
            "previous": "Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>",
            "current": 'Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls>',
            "delta": '\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls>',
            "expected_content": '\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls>',
            "expected_tool_calls": 0,  # Tools inside thinking tags should be ignored
832
833
        },
        {
834
835
836
837
838
839
            "stage": "End thinking tag",
            "previous": 'Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls>',
            "current": 'Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls></think>',
            "delta": "</think>",
            "expected_content": "</think>",
            "expected_tool_calls": 0,
840
841
        },
        {
842
843
844
845
846
847
            "stage": "Multiple tools group starts",
            "previous": 'Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls></think>',
            "current": 'Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls></think>\nNow I need to get weather information and calculate area.<tool_calls>',
            "delta": "\nNow I need to get weather information and calculate area.<tool_calls>",
            "expected_content": "\nNow I need to get weather information and calculate area.",  # <tool_calls> should be filtered
            "expected_tool_calls": 0,
848
849
        },
        {
850
851
852
853
854
855
856
            "stage": "First tool in group",
            "previous": 'Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls></think>\nNow I need to get weather information and calculate area.<tool_calls>',
            "current": 'Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls></think>\nNow I need to get weather information and calculate area.<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}',
            "delta": '\n{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}',
            "expected_content": None,  # No content should be output when tool call is in progress
            "expected_tool_calls": 1,
            "expected_tool_name": "get_current_weather",
857
858
        },
        {
859
860
861
862
863
864
865
            "stage": "Second tool in group",
            "previous": 'Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls></think>\nNow I need to get weather information and calculate area.<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}',
            "current": 'Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls></think>\nNow I need to get weather information and calculate area.<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}\n{"name": "calculate_area", "arguments": {"shape": "rectangle", "dimensions": {"width": 10, "height": 5}}}',
            "delta": '\n{"name": "calculate_area", "arguments": {"shape": "rectangle", "dimensions": {"width": 10, "height": 5}}}',
            "expected_content": None,
            "expected_tool_calls": 1,
            "expected_tool_name": "calculate_area",
866
867
        },
        {
868
869
870
871
872
873
874
            "stage": "Complete tool calls group",
            "previous": 'Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls></think>\nNow I need to get weather information and calculate area.<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}\n{"name": "calculate_area", "arguments": {"shape": "rectangle", "dimensions": {"width": 10, "height": 5}}}',
            "current": 'Let me help you with this task.<think>I need to analyze this situation first.<tool_calls>\n{"name": "internal_analysis", "arguments": {"query": "analyze situation"}}\n</tool_calls></think>\nNow I need to get weather information and calculate area.<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}\n{"name": "calculate_area", "arguments": {"shape": "rectangle", "dimensions": {"width": 10, "height": 5}}}</tool_calls>',
            "delta": "</tool_calls>",
            "expected_content": None,
            "expected_tool_calls": 0,
        },
875
876
877
878
879
880
881
882
883
884
885
886
887
    ]

    tool_calls_count = 0

    for i, test_case in enumerate(test_stages):
        print(f"\n--- Stage {i}: {test_case['stage']} ---")
        print(
            f"Previous: {repr(test_case['previous'][:100])}{'...' if len(test_case['previous']) > 100 else ''}"
        )
        print(f"Current:  {repr(test_case['current'][-100:])}")
        print(f"Delta:    {repr(test_case['delta'])}")

        result = minimax_tool_parser.extract_tool_calls_streaming(
888
889
890
            previous_text=test_case["previous"],
            current_text=test_case["current"],
            delta_text=test_case["delta"],
891
892
893
894
895
896
897
898
899
            previous_token_ids=[],
            current_token_ids=[],
            delta_token_ids=[],
            request=None,
        )

        print(f"Result: {result}")

        # Check expected content
900
901
        if test_case["expected_content"] is None:
            assert result is None or not getattr(result, "content", None), (
902
                f"Stage {i}: Expected no content output, got {result}"
903
            )
904
905
            print("✓ No content output as expected")
        else:
906
            assert result is not None and hasattr(result, "content"), (
907
                f"Stage {i}: Expected content output, got {result}"
908
909
            )
            assert result.content == test_case["expected_content"], (
910
                f"Stage {i}: Expected content {repr(test_case['expected_content'])}, got {repr(result.content)}"
911
            )
912
913
914
            print(f"✓ Content matches: {repr(result.content)}")

        # Check tool calls
915
916
917
918
919
920
        expected_tool_calls = test_case["expected_tool_calls"]
        actual_tool_calls = (
            len(result.tool_calls)
            if result and hasattr(result, "tool_calls") and result.tool_calls
            else 0
        )
921
922

        if expected_tool_calls > 0:
923
            assert actual_tool_calls >= expected_tool_calls, (
924
                f"Stage {i}: Expected at least {expected_tool_calls} tool calls, got {actual_tool_calls}"
925
            )
926

927
            if "expected_tool_name" in test_case:
928
929
930
                # Find the tool call with the expected name
                found_tool_call = None
                for tool_call in result.tool_calls:
931
                    if tool_call.function.name == test_case["expected_tool_name"]:
932
933
934
                        found_tool_call = tool_call
                        break

935
                assert found_tool_call is not None, (
936
                    f"Stage {i}: Expected tool name {test_case['expected_tool_name']} not found in tool calls: {[tc.function.name for tc in result.tool_calls]}"
937
                )
938
939
940
                print(f"✓ Tool call correct: {found_tool_call.function.name}")

                # Ensure tools inside thinking tags are not called
941
                assert found_tool_call.function.name != "internal_analysis", (
942
                    f"Stage {i}: Tool 'internal_analysis' inside thinking tags should not be called"
943
                )
944
945
946
947

            tool_calls_count += actual_tool_calls
            print(f"✓ Detected {actual_tool_calls} tool calls")
        else:
948
            assert actual_tool_calls == 0, (
949
                f"Stage {i}: Expected no tool calls, got {actual_tool_calls}"
950
            )
951
952
953
954

    # Verify overall results
    print("\n=== Test Summary ===")
    print(f"Total tool calls count: {tool_calls_count}")
955
956
957
    assert tool_calls_count >= 2, (
        f"Expected at least 2 valid tool calls (outside thinking tags), but got {tool_calls_count}"
    )
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990

    print("✓ Complex streaming test completed:")
    print("  - ✓ Tools inside thinking tags correctly ignored")
    print("  - ✓ Two tool groups outside thinking tags correctly parsed")
    print("  - ✓ Content and tool call streaming correctly handled")
    print("  - ✓ Buffering mechanism works correctly")


def test_streaming_character_by_character_output(minimax_tool_parser):
    """Test character-by-character streaming output to simulate real streaming scenarios."""
    # Reset streaming state
    reset_streaming_state(minimax_tool_parser)

    # Complete text that will be streamed character by character
    complete_text = """I'll help you with the weather analysis. <think>Let me think about this. <tool_calls>
{"name": "internal_analysis", "arguments": {"type": "thinking"}}
</tool_calls>This tool should be ignored.</think>

Now I'll get the weather information for you. <tool_calls>
{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}
{"name": "calculate_area", "arguments": {"shape": "rectangle", "dimensions": {"width": 10, "height": 5}}}
</tool_calls>Here are the results."""

    print("\n=== Starting character-by-character streaming test ===")
    print(f"Complete text length: {len(complete_text)} characters")

    # Track the streaming results
    content_fragments = []
    tool_calls_detected = []

    # Stream character by character
    for i in range(1, len(complete_text) + 1):
        current_text = complete_text[:i]
991
992
        previous_text = complete_text[: i - 1] if i > 1 else ""
        delta_text = complete_text[i - 1 : i]
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010

        # Show progress every 50 characters
        if i % 50 == 0 or i == len(complete_text):
            print(f"Progress: {i}/{len(complete_text)} characters")

        # Call the streaming parser
        result = minimax_tool_parser.extract_tool_calls_streaming(
            previous_text=previous_text,
            current_text=current_text,
            delta_text=delta_text,
            previous_token_ids=[],
            current_token_ids=[],
            delta_token_ids=[],
            request=None,
        )

        # Collect results
        if result is not None:
1011
            if hasattr(result, "content") and result.content:
1012
1013
1014
                content_fragments.append(result.content)
                # Log important content fragments
                if any(
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
                    keyword in result.content
                    for keyword in [
                        "<think>",
                        "</think>",
                        "<tool_calls>",
                        "</tool_calls>",
                    ]
                ):
                    print(f"  Char {i}: Content fragment: {repr(result.content)}")

            if hasattr(result, "tool_calls") and result.tool_calls:
1026
1027
                for tool_call in result.tool_calls:
                    tool_info = {
1028
1029
1030
1031
1032
1033
1034
                        "character_position": i,
                        "function_name": tool_call.function.name
                        if tool_call.function
                        else None,
                        "arguments": tool_call.function.arguments
                        if tool_call.function
                        else None,
1035
1036
                    }
                    tool_calls_detected.append(tool_info)
1037
                    print(f"  Char {i}: Tool call detected: {tool_call.function.name}")
1038
                    if tool_call.function.arguments:
1039
                        print(f"    Arguments: {repr(tool_call.function.arguments)}")
1040
1041
1042
1043
1044
1045
1046

    # Verify results
    print("\n=== Streaming Test Results ===")
    print(f"Total content fragments: {len(content_fragments)}")
    print(f"Total tool calls detected: {len(tool_calls_detected)}")

    # Reconstruct content from fragments
1047
    reconstructed_content = "".join(content_fragments)
1048
1049
1050
    print(f"Reconstructed content length: {len(reconstructed_content)}")

    # Verify thinking tags content is preserved
1051
1052
1053
1054
1055
1056
    assert "<think>" in reconstructed_content, (
        "Opening thinking tag should be preserved in content"
    )
    assert "</think>" in reconstructed_content, (
        "Closing thinking tag should be preserved in content"
    )
1057
1058
1059

    # Verify that tool calls inside thinking tags are NOT extracted as actual tool calls
    thinking_tool_calls = [
1060
        tc for tc in tool_calls_detected if tc["function_name"] == "internal_analysis"
1061
    ]
1062
1063
1064
    assert len(thinking_tool_calls) == 0, (
        f"Tool calls inside thinking tags should be ignored, but found: {thinking_tool_calls}"
    )
1065
1066
1067

    # Verify that real tool calls outside thinking tags ARE extracted
    weather_tool_calls = [
1068
        tc for tc in tool_calls_detected if tc["function_name"] == "get_current_weather"
1069
1070
    ]
    area_tool_calls = [
1071
        tc for tc in tool_calls_detected if tc["function_name"] == "calculate_area"
1072
1073
    ]
    print(tool_calls_detected)
1074
1075
1076
1077
    assert len(weather_tool_calls) > 0, (
        "get_current_weather tool call should be detected"
    )
    assert len(area_tool_calls) > 0, "calculate_area tool call should be detected"
1078
1079

    # Verify tool call arguments are properly streamed
1080
1081
1082
1083
    weather_args_found = any(
        tc["arguments"] for tc in weather_tool_calls if tc["arguments"]
    )
    area_args_found = any(tc["arguments"] for tc in area_tool_calls if tc["arguments"])
1084
1085
1086
1087
1088

    print(f"Weather tool call with arguments: {weather_args_found}")
    print(f"Area tool call with arguments: {area_args_found}")

    # Verify content before and after tool calls
1089
1090
1091
1092
1093
1094
    assert "I'll help you with the weather analysis." in reconstructed_content, (
        "Initial content should be preserved"
    )
    assert "Here are the results." in reconstructed_content, (
        "Final content should be preserved"
    )
1095
1096
1097
1098
1099

    # Verify that <tool_calls> and </tool_calls> tags are not included in the final content
    # (they should be filtered out when not inside thinking tags)
    content_outside_thinking = reconstructed_content
    # Remove thinking tag content to check content outside
1100
1101
1102
1103
1104
1105
1106
    if "<think>" in content_outside_thinking and "</think>" in content_outside_thinking:
        start_think = content_outside_thinking.find("<think>")
        end_think = content_outside_thinking.find("</think>") + len("</think>")
        content_outside_thinking = (
            content_outside_thinking[:start_think]
            + content_outside_thinking[end_think:]
        )
1107
1108

    # Outside thinking tags, tool_calls tags should be filtered
1109
1110
1111
    tool_calls_in_content = content_outside_thinking.count("<tool_calls>")
    assert tool_calls_in_content == 0, (
        f"<tool_calls> tags should be filtered from content outside thinking tags, but found {tool_calls_in_content}"
1112
    )
1113
1114

    print("\n=== Character-by-character streaming test completed successfully ===")
1115
1116
1117
1118
1119
1120
1121
    print("✓ Tool calls inside thinking tags correctly ignored")
    print("✓ Tool calls outside thinking tags correctly detected")
    print("✓ Content properly streamed and reconstructed")
    print("✓ Tool call tags properly filtered from content")
    print("✓ Character-level streaming works correctly")


1122
def test_streaming_character_by_character_simple_tool_call(minimax_tool_parser):
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
    """Test character-by-character streaming for a simple tool call scenario."""
    # Reset streaming state
    reset_streaming_state(minimax_tool_parser)

    # Simple tool call text
    simple_text = 'Let me check the weather. <tool_calls>\n{"name": "get_weather", "arguments": {"city": "NYC"}}\n</tool_calls>'

    print("\n=== Simple character-by-character test ===")
    print(f"Text: {repr(simple_text)}")

    content_parts = []
    tool_name_sent = False
    tool_args_sent = False

    for i in range(1, len(simple_text) + 1):
        current_text = simple_text[:i]
1139
1140
        previous_text = simple_text[: i - 1] if i > 1 else ""
        delta_text = simple_text[i - 1 : i]
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152

        result = minimax_tool_parser.extract_tool_calls_streaming(
            previous_text=previous_text,
            current_text=current_text,
            delta_text=delta_text,
            previous_token_ids=[],
            current_token_ids=[],
            delta_token_ids=[],
            request=None,
        )

        if result:
1153
            if hasattr(result, "content") and result.content:
1154
1155
1156
1157
1158
                content_parts.append(result.content)
                print(
                    f"  Char {i} ({repr(delta_text)}): Content: {repr(result.content)}"
                )

1159
            if hasattr(result, "tool_calls") and result.tool_calls:
1160
1161
1162
                for tool_call in result.tool_calls:
                    if tool_call.function and tool_call.function.name:
                        tool_name_sent = True
1163
                        print(f"  Char {i}: Tool name: {tool_call.function.name}")
1164
1165
1166
1167
1168
1169
1170
                    if tool_call.function and tool_call.function.arguments:
                        tool_args_sent = True
                        print(
                            f"  Char {i}: Tool args: {repr(tool_call.function.arguments)}"
                        )

    # Verify basic expectations
1171
    reconstructed_content = "".join(content_parts)
1172
1173
1174
1175
    print(f"Final reconstructed content: {repr(reconstructed_content)}")

    assert tool_name_sent, "Tool name should be sent during streaming"
    assert tool_args_sent, "Tool arguments should be sent during streaming"
1176
1177
1178
    assert "Let me check the weather." in reconstructed_content, (
        "Initial content should be preserved"
    )
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197

    print("✓ Simple character-by-character test passed")


def test_streaming_character_by_character_with_buffering(minimax_tool_parser):
    """Test character-by-character streaming with edge cases that trigger buffering."""
    # Reset streaming state
    reset_streaming_state(minimax_tool_parser)

    # Text that includes potential buffering scenarios
    buffering_text = 'Hello world<tool_calls>\n{"name": "test"}\n</tool_calls>done'

    print("\n=== Buffering character-by-character test ===")
    print(f"Text: {repr(buffering_text)}")

    all_content = []

    for i in range(1, len(buffering_text) + 1):
        current_text = buffering_text[:i]
1198
1199
        previous_text = buffering_text[: i - 1] if i > 1 else ""
        delta_text = buffering_text[i - 1 : i]
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210

        result = minimax_tool_parser.extract_tool_calls_streaming(
            previous_text=previous_text,
            current_text=current_text,
            delta_text=delta_text,
            previous_token_ids=[],
            current_token_ids=[],
            delta_token_ids=[],
            request=None,
        )

1211
        if result and hasattr(result, "content") and result.content:
1212
1213
1214
            all_content.append(result.content)
            print(f"  Char {i} ({repr(delta_text)}): {repr(result.content)}")

1215
    final_content = "".join(all_content)
1216
1217
1218
1219
    print(f"Final content: {repr(final_content)}")

    # The parser should handle the edge case where </tool_calls> appears before <tool_calls>
    assert "Hello" in final_content, "Initial 'Hello' should be preserved"
1220
1221
1222
    assert "world" in final_content, (
        "Content after false closing tag should be preserved"
    )
1223
1224
1225
    assert "done" in final_content, "Final content should be preserved"

    print("✓ Buffering character-by-character test passed")