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.entrypoints.openai.tool_parsers.minimax_tool_parser import MinimaxToolParser
16
from vllm.tokenizers import get_tokenizer
17

18
19
pytestmark = pytest.mark.cpu_test

20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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)


34
35
36
@pytest.fixture
def sample_tools():
    return [
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
66
67
        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"},
                    },
                },
            },
        ),
68
69
70
    ]


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

76
77
78
    for actual_tool_call, expected_tool_call in zip(
        actual_tool_calls, expected_tool_calls
    ):
79
80
81
82
83
84
85
86
87
88
        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(
89
90
        model_output, request=None
    )  # type: ignore[arg-type]
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    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>""",
            [
111
112
113
114
115
116
117
118
119
120
121
122
                ToolCall(
                    function=FunctionCall(
                        name="get_current_weather",
                        arguments=json.dumps(
                            {
                                "city": "Dallas",
                                "state": "TX",
                                "unit": "fahrenheit",
                            }
                        ),
                    )
                )
123
124
125
126
127
128
129
130
131
            ],
            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>""",
            [
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
                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",
                            }
                        ),
                    )
                ),
156
157
158
159
160
161
162
163
            ],
            None,
        ),
        (
            """I'll help you check the weather. <tool_calls>
{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}
</tool_calls>""",
            [
164
165
166
167
168
169
170
171
172
173
174
175
                ToolCall(
                    function=FunctionCall(
                        name="get_current_weather",
                        arguments=json.dumps(
                            {
                                "city": "Seattle",
                                "state": "WA",
                                "unit": "celsius",
                            }
                        ),
                    )
                )
176
177
178
179
180
181
182
183
            ],
            "I'll help you check the weather.",
        ),
        (
            """<tool_calls>
{"name": "get_current_weather", "arguments": {"city": "New York", "state": "NY", "unit": "celsius"}}
</tool_calls>""",
            [
184
185
186
187
188
189
190
191
192
193
194
195
                ToolCall(
                    function=FunctionCall(
                        name="get_current_weather",
                        arguments=json.dumps(
                            {
                                "city": "New York",
                                "state": "NY",
                                "unit": "celsius",
                            }
                        ),
                    )
                )
196
197
198
199
200
201
202
            ],
            None,
        ),
        (
            """<tool_calls>
{"name": "get_current_weather", "arguments": {"city": "Boston", "state": "MA"}}""",
            [
203
204
205
206
207
208
209
210
211
212
213
                ToolCall(
                    function=FunctionCall(
                        name="get_current_weather",
                        arguments=json.dumps(
                            {
                                "city": "Boston",
                                "state": "MA",
                            }
                        ),
                    )
                )
214
215
216
217
218
            ],
            None,
        ),
    ],
)
219
220
221
def test_extract_tool_calls(
    minimax_tool_parser, model_output, expected_tool_calls, expected_content
):
222
    extracted_tool_calls = minimax_tool_parser.extract_tool_calls(
223
224
        model_output, request=None
    )  # type: ignore[arg-type]
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    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>"""

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

    # 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(
264
265
        model_output, request=None
    )  # type: ignore[arg-type]
266
267
268

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

    # 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(
290
291
        model_output, request=None
    )  # type: ignore[arg-type]
292
293
294
295
296

    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"
297
    assert extracted_tool_calls.tool_calls[1].function.name == "another_valid_tool"
298
299
300
301
302
303
304
305
306
307
308
309


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(
310
311
        model_output, request=None
    )  # type: ignore[arg-type]
312
313
314
315
316

    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"
317
    assert extracted_tool_calls.tool_calls[1].function.name == "another_valid_tool"
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
344
345


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
346
    if result is not None and hasattr(result, "tool_calls") and result.tool_calls:
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
        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,
    )

371
    if result is not None and hasattr(result, "content"):
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
        # 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
392
    assert hasattr(result, "content")
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
    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
418
    if result is not None and hasattr(result, "tool_calls") and result.tool_calls:
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
        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(
437
438
        model_output, request=None
    )  # type: ignore[arg-type]
439
440
441
442
443

    # 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467


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',
468
        '<tool_calls>\n{"name": "get_current_weather", "arguments": {"city": "Seattle", "state": "WA", "unit": "celsius"}}\n</tool_calls>',
469
470
471
472
473
474
475
    ]

    function_name_sent = False
    previous_args_content = ""

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

        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)}")

491
        if result is not None and hasattr(result, "tool_calls") and result.tool_calls:
492
493
494
495
496
497
            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
498
                print(f"Stage {i}: Function name sent: {tool_call.function.name}")
499
500
501
502

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

                # 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
509
510
511
                    assert args_fragment != previous_args_content, (
                        f"Fragment should be incremental, not cumulative: {args_fragment}"
                    )
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534

                    # 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
535
536
537
    call1_text = (
        '<tool_calls>\n{"name": "test_tool", "arguments": {"param1": "value1"}}'
    )
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
    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}")
555
    if result1 and hasattr(result1, "tool_calls") and result1.tool_calls:
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
        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}")
571
    if result2 and hasattr(result2, "tool_calls") and result2.tool_calls:
572
573
574
575
        for i, tc in enumerate(result2.tool_calls):
            print(f"  Tool call {i}: {tc}")

    # Verify the second call only returns the delta
576
    if result2 is not None and hasattr(result2, "tool_calls") and result2.tool_calls:
577
578
579
580
581
582
583
        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"'
584
585
586
587
            assert (
                ', "param2": "value2"}' in args_delta
                or '"param2": "value2"' in args_delta
            ), f"Expected delta containing param2, got: {args_delta}"
588
589

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

            # The delta should be relatively short (incremental, not cumulative)
595
596
597
598
            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}"
            )
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624

            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]] = [
        {
625
626
627
628
629
            "stage": "Token: <",
            "previous": "",
            "current": "<",
            "delta": "<",
            "expected_content": None,  # Should be buffered
630
631
        },
        {
632
633
634
635
636
            "stage": "Token: tool_calls>",
            "previous": "<",
            "current": "<tool_calls>",
            "delta": "tool_calls>",
            "expected_content": None,  # Complete tag, should not output
637
638
        },
        {
639
640
641
642
643
            "stage": "Regular content",
            "previous": "Hello",
            "current": "Hello world",
            "delta": " world",
            "expected_content": " world",  # Normal content should pass through
644
645
        },
        {
646
647
648
649
650
            "stage": "Content with end tag start",
            "previous": "Text",
            "current": "Text content</tool_",
            "delta": " content</tool_",
            "expected_content": " content",  # Content part output, </tool_ buffered
651
652
        },
        {
653
654
655
656
657
            "stage": "Complete end tag",
            "previous": "Text content</tool_",
            "current": "Text content</tool_calls>",
            "delta": "calls>",
            "expected_content": None,  # Complete close tag, should not output
658
659
660
661
662
663
664
665
666
667
        },
    ]

    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(
668
669
670
            previous_text=test_case["previous"],
            current_text=test_case["current"],
            delta_text=test_case["delta"],
671
672
673
674
675
676
677
678
679
            previous_token_ids=[],
            current_token_ids=[],
            delta_token_ids=[],
            request=None,
        )

        print(f"Result: {result}")

        # Check expected content
680
681
        if test_case["expected_content"] is None:
            assert result is None or not getattr(result, "content", None), (
682
                f"Stage {i}: Expected no content, got {result}"
683
            )
684
685
            print("✓ No content output as expected")
        else:
686
            assert result is not None and hasattr(result, "content"), (
687
                f"Stage {i}: Expected content, got {result}"
688
689
            )
            assert result.content == test_case["expected_content"], (
690
                f"Stage {i}: Expected content {test_case['expected_content']}, got {result.content}"
691
            )
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
            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]] = [
        {
712
713
714
715
716
            "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
717
718
        },
        {
719
720
721
722
723
            "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
724
725
        },
        {
726
727
728
729
730
731
            "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>
        },
732
733
734
735
736
737
738
739
740
    ]

    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(
741
742
743
            previous_text=test_case["previous"],
            current_text=test_case["current"],
            delta_text=test_case["delta"],
744
745
746
747
748
749
750
751
752
            previous_token_ids=[],
            current_token_ids=[],
            delta_token_ids=[],
            request=None,
        )

        print(f"Result: {result}")

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

        # Check tool calls
768
769
770
771
772
773
        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}"
774
775

            tool_call = result.tool_calls[0]
776
            assert tool_call.function.name == "real_tool", (
777
                f"Expected real_tool, got {tool_call.function.name}"
778
            )
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
            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]] = [
        {
804
805
806
807
808
809
            "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,
810
811
        },
        {
812
813
814
815
816
817
            "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,
818
819
        },
        {
820
821
822
823
824
825
            "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,
826
827
        },
        {
828
829
830
831
832
833
            "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
834
835
        },
        {
836
837
838
839
840
841
            "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,
842
843
        },
        {
844
845
846
847
848
849
            "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,
850
851
        },
        {
852
853
854
855
856
857
858
            "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",
859
860
        },
        {
861
862
863
864
865
866
867
            "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",
868
869
        },
        {
870
871
872
873
874
875
876
            "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,
        },
877
878
879
880
881
882
883
884
885
886
887
888
889
    ]

    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(
890
891
892
            previous_text=test_case["previous"],
            current_text=test_case["current"],
            delta_text=test_case["delta"],
893
894
895
896
897
898
899
900
901
            previous_token_ids=[],
            current_token_ids=[],
            delta_token_ids=[],
            request=None,
        )

        print(f"Result: {result}")

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

        # Check tool calls
917
918
919
920
921
922
        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
        )
923
924

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

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

937
                assert found_tool_call is not None, (
938
                    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]}"
939
                )
940
941
942
                print(f"✓ Tool call correct: {found_tool_call.function.name}")

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

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

    # Verify overall results
    print("\n=== Test Summary ===")
    print(f"Total tool calls count: {tool_calls_count}")
957
958
959
    assert tool_calls_count >= 2, (
        f"Expected at least 2 valid tool calls (outside thinking tags), but got {tool_calls_count}"
    )
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
991
992

    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]
993
994
        previous_text = complete_text[: i - 1] if i > 1 else ""
        delta_text = complete_text[i - 1 : i]
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012

        # 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:
1013
            if hasattr(result, "content") and result.content:
1014
1015
1016
                content_fragments.append(result.content)
                # Log important content fragments
                if any(
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
                    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:
1028
1029
                for tool_call in result.tool_calls:
                    tool_info = {
1030
1031
1032
1033
1034
1035
1036
                        "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,
1037
1038
                    }
                    tool_calls_detected.append(tool_info)
1039
                    print(f"  Char {i}: Tool call detected: {tool_call.function.name}")
1040
                    if tool_call.function.arguments:
1041
                        print(f"    Arguments: {repr(tool_call.function.arguments)}")
1042
1043
1044
1045
1046
1047
1048

    # 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
1049
    reconstructed_content = "".join(content_fragments)
1050
1051
1052
    print(f"Reconstructed content length: {len(reconstructed_content)}")

    # Verify thinking tags content is preserved
1053
1054
1055
1056
1057
1058
    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"
    )
1059
1060
1061

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

    # Verify that real tool calls outside thinking tags ARE extracted
    weather_tool_calls = [
1070
        tc for tc in tool_calls_detected if tc["function_name"] == "get_current_weather"
1071
1072
    ]
    area_tool_calls = [
1073
        tc for tc in tool_calls_detected if tc["function_name"] == "calculate_area"
1074
1075
    ]
    print(tool_calls_detected)
1076
1077
1078
1079
    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"
1080
1081

    # Verify tool call arguments are properly streamed
1082
1083
1084
1085
    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"])
1086
1087
1088
1089
1090

    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
1091
1092
1093
1094
1095
1096
    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"
    )
1097
1098
1099
1100
1101

    # 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
1102
1103
1104
1105
1106
1107
1108
    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:]
        )
1109
1110

    # Outside thinking tags, tool_calls tags should be filtered
1111
1112
1113
    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}"
1114
    )
1115
1116

    print("\n=== Character-by-character streaming test completed successfully ===")
1117
1118
1119
1120
1121
1122
1123
    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")


1124
def test_streaming_character_by_character_simple_tool_call(minimax_tool_parser):
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
    """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]
1141
1142
        previous_text = simple_text[: i - 1] if i > 1 else ""
        delta_text = simple_text[i - 1 : i]
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154

        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:
1155
            if hasattr(result, "content") and result.content:
1156
1157
1158
1159
1160
                content_parts.append(result.content)
                print(
                    f"  Char {i} ({repr(delta_text)}): Content: {repr(result.content)}"
                )

1161
            if hasattr(result, "tool_calls") and result.tool_calls:
1162
1163
1164
                for tool_call in result.tool_calls:
                    if tool_call.function and tool_call.function.name:
                        tool_name_sent = True
1165
                        print(f"  Char {i}: Tool name: {tool_call.function.name}")
1166
1167
1168
1169
1170
1171
1172
                    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
1173
    reconstructed_content = "".join(content_parts)
1174
1175
1176
1177
    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"
1178
1179
1180
    assert "Let me check the weather." in reconstructed_content, (
        "Initial content should be preserved"
    )
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199

    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]
1200
1201
        previous_text = buffering_text[: i - 1] if i > 1 else ""
        delta_text = buffering_text[i - 1 : i]
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212

        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,
        )

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

1217
    final_content = "".join(all_content)
1218
1219
1220
1221
    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"
1222
1223
1224
    assert "world" in final_content, (
        "Content after false closing tag should be preserved"
    )
1225
1226
1227
    assert "done" in final_content, "Final content should be preserved"

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