Skip to content

vllm.entrypoints.openai.tool_parsers.minimax_m2_tool_parser ¶

logger module-attribute ¶

logger = init_logger(__name__)

MinimaxM2ToolParser ¶

Bases: ToolParser

Source code in vllm/entrypoints/openai/tool_parsers/minimax_m2_tool_parser.py
 29
 30
 31
 32
 33
 34
 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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
@ToolParserManager.register_module("minimax_m2")
class MinimaxM2ToolParser(ToolParser):
    def __init__(self, tokenizer: AnyTokenizer):
        super().__init__(tokenizer)

        self.prev_tool_call_arr: list[dict] = []

        # Sentinel tokens
        self.tool_call_start_token: str = "<minimax:tool_call>"
        self.tool_call_end_token: str = "</minimax:tool_call>"
        self.invoke_start_prefix: str = "<invoke name="
        self.invoke_end_token: str = "</invoke>"
        self.parameter_prefix: str = "<parameter name="
        self.parameter_end_token: str = "</parameter>"

        # Streaming state variables
        self.current_tool_name_sent: bool = False
        # Override base class type - we use string IDs for tool calls
        self.current_tool_id: str | None = None  # type: ignore
        self.streamed_args_for_tool: list[str] = []
        self.is_tool_call_started: bool = False
        self.failed_count: int = 0

        # Initialize streaming state variables
        self.current_tool_index: int = 0
        self.invoke_index: int = 0
        self.header_sent: bool = False
        self.current_function_name: str | None = None
        self.current_param_name: str | None = None
        self.current_param_value: str = ""
        self.param_count: int = 0
        self.in_param: bool = False
        self.in_function: bool = False
        self.accumulated_text: str = ""
        self.json_started: bool = False
        self.json_closed: bool = False
        self.accumulated_params: dict = {}
        self.streaming_request: ChatCompletionRequest | None = None

        # Enhanced streaming state - reset for each new message
        self._reset_streaming_state()

        # Regex patterns for complete parsing
        self.tool_call_complete_regex = re.compile(
            r"<minimax:tool_call>(.*?)</minimax:tool_call>", re.DOTALL
        )
        self.invoke_complete_regex = re.compile(
            r"<invoke name=(.*?)</invoke>", re.DOTALL
        )
        self.parameter_complete_regex = re.compile(
            r"<parameter name=(.*?)</parameter>", re.DOTALL
        )

        if not self.model_tokenizer:
            raise ValueError(
                "The model tokenizer must be passed to the ToolParser "
                "constructor during construction."
            )

        self.tool_call_start_token_id = self.vocab.get(self.tool_call_start_token)
        self.tool_call_end_token_id = self.vocab.get(self.tool_call_end_token)

        if self.tool_call_start_token_id is None or self.tool_call_end_token_id is None:
            raise RuntimeError(
                "MiniMax M2 Tool parser could not locate tool call start/end "
                "tokens in the tokenizer!"
            )

        logger.info(
            "vLLM Successfully import tool parser %s !", self.__class__.__name__
        )

    def _generate_tool_call_id(self) -> str:
        """Generate a unique tool call ID."""
        return f"call_{uuid.uuid4().hex[:24]}"

    def _reset_streaming_state(self):
        """Reset all streaming state."""
        self.current_tool_index = 0
        self.invoke_index = 0
        self.is_tool_call_started = False
        self.header_sent = False
        self.current_tool_id = None
        self.current_function_name = None
        self.current_param_name = None
        self.current_param_value = ""
        self.param_count = 0
        self.in_param = False
        self.in_function = False
        self.accumulated_text = ""
        self.json_started = False
        self.json_closed = False
        # Store accumulated parameters for type conversion
        self.accumulated_params = {}
        self.streaming_request = None
        # Clear previous tool call history to avoid state pollution
        self.prev_tool_call_arr.clear()

    def _extract_name(self, name_str: str) -> str:
        """Extract name from quoted string."""
        name_str = name_str.strip()
        if (
            name_str.startswith('"')
            and name_str.endswith('"')
            or name_str.startswith("'")
            and name_str.endswith("'")
        ):
            return name_str[1:-1]
        return name_str

    def _convert_param_value(self, value: str, param_type: str) -> Any:
        """Convert parameter value to the correct type."""
        if value.lower() == "null":
            return None

        param_type = param_type.lower()
        if param_type in ["string", "str", "text"]:
            return value
        elif param_type in ["integer", "int"]:
            try:
                return int(value)
            except (ValueError, TypeError):
                return value
        elif param_type in ["number", "float"]:
            try:
                val = float(value)
                return val if val != int(val) else int(val)
            except (ValueError, TypeError):
                return value
        elif param_type in ["boolean", "bool"]:
            return value.lower() in ["true", "1"]
        elif param_type in ["object", "array"]:
            try:
                return json.loads(value)
            except json.JSONDecodeError:
                return value
        else:
            # Try JSON parse first, fallback to string
            try:
                return json.loads(value)
            except json.JSONDecodeError:
                return value

    def _parse_single_invoke(
        self, invoke_str: str, tools: list | None
    ) -> ToolCall | None:
        """Parse a single <invoke> block."""
        # Extract function name
        name_match = re.search(r"^([^>]+)", invoke_str)
        if not name_match:
            return None

        function_name = self._extract_name(name_match.group(1))

        # Get parameter configuration
        param_config = {}
        if tools:
            for tool in tools:
                if (
                    hasattr(tool, "function")
                    and tool.function.name == function_name
                    and hasattr(tool.function, "parameters")
                ):
                    params = tool.function.parameters
                    if isinstance(params, dict) and "properties" in params:
                        param_config = params["properties"]
                    break

        # Extract parameters
        param_dict = {}
        for match in self.parameter_complete_regex.findall(invoke_str):
            param_match = re.search(r"^([^>]+)>(.*)", match, re.DOTALL)
            if param_match:
                param_name = self._extract_name(param_match.group(1))
                param_value = param_match.group(2).strip()
                if param_value.startswith("\n"):
                    param_value = param_value[1:]
                if param_value.endswith("\n"):
                    param_value = param_value[:-1]

                # Get parameter type
                param_type = "string"
                if (
                    param_name in param_config
                    and isinstance(param_config[param_name], dict)
                    and "type" in param_config[param_name]
                ):
                    param_type = param_config[param_name]["type"]

                # Convert value
                param_dict[param_name] = self._convert_param_value(
                    param_value, param_type
                )

        return ToolCall(
            type="function",
            function=FunctionCall(
                name=function_name,
                arguments=json.dumps(param_dict, ensure_ascii=False),
            ),
        )

    def extract_tool_calls(
        self,
        model_output: str,
        request: ChatCompletionRequest,
    ) -> ExtractedToolCallInformation:
        """Extract tool calls from complete model output (non-streaming)."""
        # Quick check
        if self.tool_call_start_token not in model_output:
            return ExtractedToolCallInformation(
                tools_called=False, tool_calls=[], content=model_output
            )

        try:
            tool_calls = []

            # Find all complete tool_call blocks
            for tool_call_match in self.tool_call_complete_regex.findall(model_output):
                # Find all invokes within this tool_call
                for invoke_match in self.invoke_complete_regex.findall(tool_call_match):
                    tool_call = self._parse_single_invoke(
                        invoke_match, request.tools if request else None
                    )
                    if tool_call:
                        tool_calls.append(tool_call)

            if not tool_calls:
                return ExtractedToolCallInformation(
                    tools_called=False, tool_calls=[], content=model_output
                )

            # Update prev_tool_call_arr
            self.prev_tool_call_arr.clear()
            for tool_call in tool_calls:
                self.prev_tool_call_arr.append(
                    {
                        "name": tool_call.function.name,
                        "arguments": tool_call.function.arguments,
                    }
                )

            # Extract content before first tool call
            first_tool_idx = model_output.find(self.tool_call_start_token)
            content = model_output[:first_tool_idx] if first_tool_idx > 0 else None

            return ExtractedToolCallInformation(
                tools_called=True, tool_calls=tool_calls, content=content
            )

        except Exception:
            logger.exception("Error extracting tool calls")
            return ExtractedToolCallInformation(
                tools_called=False, tool_calls=[], content=model_output
            )

    def extract_tool_calls_streaming(
        self,
        previous_text: str,
        current_text: str,
        delta_text: str,
        previous_token_ids: Sequence[int],  # pylint: disable=unused-argument
        current_token_ids: Sequence[int],  # pylint: disable=unused-argument
        delta_token_ids: Sequence[int],
        request: ChatCompletionRequest,
    ) -> DeltaMessage | None:
        """Extract tool calls from streaming model output."""

        # Store request for type conversion
        if not previous_text or self.tool_call_start_token in delta_text:
            self._reset_streaming_state()
            self.streaming_request = request

        # If no delta text, return None unless it's an EOS token after tools
        if not delta_text:
            # Check if this is an EOS token after all tool calls are complete
            if delta_token_ids and self.tool_call_end_token_id not in delta_token_ids:
                # Count complete tool calls
                complete_calls = len(
                    self.tool_call_complete_regex.findall(current_text)
                )

                # If we have completed tool calls and populated prev_tool_call_arr
                if complete_calls > 0 and len(self.prev_tool_call_arr) > 0:
                    # Check if all tool calls are closed
                    open_calls = current_text.count(
                        self.tool_call_start_token
                    ) - current_text.count(self.tool_call_end_token)
                    if open_calls == 0:
                        # Return empty delta for finish_reason processing
                        return DeltaMessage(content="")
                elif not self.is_tool_call_started and current_text:
                    # This is a regular content response that's now complete
                    return DeltaMessage(content="")
            return None

        # Update accumulated text
        self.accumulated_text = current_text

        # Check if we need to advance to next tool
        if self.json_closed and not self.in_function:
            # Check if this tool call has ended
            invoke_ends = current_text.count(self.invoke_end_token)
            if invoke_ends > self.current_tool_index:
                # This tool has ended, advance to next
                self.current_tool_index += 1
                self.header_sent = False
                self.param_count = 0
                self.json_started = False
                self.json_closed = False
                self.in_function = False  # Now we can safely set this to False
                self.accumulated_params = {}
                # Continue processing next tool
                return None

        # Handle normal content before tool calls
        if not self.is_tool_call_started:
            # Check if tool call is starting
            if (
                self.tool_call_start_token_id in delta_token_ids
                or self.tool_call_start_token in delta_text
            ):
                self.is_tool_call_started = True
                # Return any content before the tool call
                if self.tool_call_start_token in delta_text:
                    content_before = delta_text[
                        : delta_text.index(self.tool_call_start_token)
                    ]
                    if content_before:
                        return DeltaMessage(content=content_before)
                return None
            else:
                # Check if we're between tool calls - skip whitespace
                if (
                    current_text.rstrip().endswith(self.tool_call_end_token)
                    and delta_text.strip() == ""
                ):
                    # We just ended a tool call, skip whitespace
                    return None
                # Normal content, no tool call
                return DeltaMessage(content=delta_text)

        # Check if we're between tool calls (waiting for next one)
        invoke_starts_count = current_text.count(self.invoke_start_prefix)
        if self.current_tool_index >= invoke_starts_count:
            # We're past all tool calls, shouldn't be here
            return None

        # Find the current tool call portion
        invoke_start_positions: list[int] = []
        idx = 0
        while True:
            idx = current_text.find(self.invoke_start_prefix, idx)
            if idx == -1:
                break
            invoke_start_positions.append(idx)
            idx += len(self.invoke_start_prefix)

        if self.current_tool_index >= len(invoke_start_positions):
            # No more tool calls to process yet
            return None

        invoke_start_idx = invoke_start_positions[self.current_tool_index]
        # Find where this tool call ends (or current position if not ended yet)
        invoke_end_idx = current_text.find(self.invoke_end_token, invoke_start_idx)
        if invoke_end_idx == -1:
            tool_text = current_text[invoke_start_idx:]
        else:
            tool_text = current_text[
                invoke_start_idx : invoke_end_idx + len(self.invoke_end_token)
            ]

        # Looking for function header
        if not self.header_sent:
            if self.invoke_start_prefix in tool_text:
                func_start = tool_text.find(self.invoke_start_prefix) + len(
                    self.invoke_start_prefix
                )
                # Find the end quote for the function name
                func_end = tool_text.find(">", func_start)

                if func_end != -1:
                    # Found complete function name
                    function_name_raw = tool_text[func_start:func_end]
                    self.current_function_name = self._extract_name(function_name_raw)
                    self.current_tool_id = self._generate_tool_call_id()
                    self.header_sent = True
                    self.in_function = True

                    # Add to prev_tool_call_arr immediately when we detect a tool call
                    # Each tool call should be recorded regardless of function name
                    # Ensure we don't add the same tool call index multiple times
                    if len(self.prev_tool_call_arr) <= self.current_tool_index:
                        self.prev_tool_call_arr.append(
                            {
                                "name": self.current_function_name,
                                "arguments": "{}",  # Placeholder, will be updated later
                            }
                        )

                    # Send header with function info
                    return DeltaMessage(
                        tool_calls=[
                            DeltaToolCall(
                                index=self.current_tool_index,
                                id=self.current_tool_id,
                                function=DeltaFunctionCall(
                                    name=self.current_function_name, arguments=""
                                ),
                                type="function",
                            )
                        ]
                    )
            return None

        # We've sent header, now handle function body
        if self.in_function:
            # Send opening brace if not sent yet
            if self.in_function and not self.json_started:
                self.json_started = True
                return DeltaMessage(
                    tool_calls=[
                        DeltaToolCall(
                            index=self.current_tool_index,
                            function=DeltaFunctionCall(arguments="{"),
                        )
                    ]
                )

            # Make sure json_started is set if we're processing parameters
            if not self.json_started:
                self.json_started = True

            # Check for function end in accumulated text
            if not self.json_closed and self.invoke_end_token in tool_text:
                # Count total parameters in the tool text
                total_param_count = tool_text.count(self.parameter_prefix)

                # Only close JSON if all parameters have been processed
                if self.param_count >= total_param_count:
                    # Close JSON
                    self.json_closed = True

                    # Extract complete tool call
                    # Find the invoke content
                    invoke_start = tool_text.find(self.invoke_start_prefix) + len(
                        self.invoke_start_prefix
                    )
                    invoke_content_end = tool_text.find(
                        self.invoke_end_token, invoke_start
                    )
                    if invoke_content_end != -1:
                        invoke_content = tool_text[invoke_start:invoke_content_end]
                        # Parse to get the complete arguments
                        try:
                            parsed_tool = self._parse_single_invoke(
                                invoke_content,
                                self.streaming_request.tools
                                if self.streaming_request
                                else None,
                            )
                            if parsed_tool and self.current_tool_index < len(
                                self.prev_tool_call_arr
                            ):
                                # Update existing entry in prev_tool_call_arr
                                args = parsed_tool.function.arguments
                                self.prev_tool_call_arr[self.current_tool_index][
                                    "arguments"
                                ] = args
                        except Exception:
                            pass  # Ignore parsing errors during streaming

                    result = DeltaMessage(
                        tool_calls=[
                            DeltaToolCall(
                                index=self.current_tool_index,
                                function=DeltaFunctionCall(arguments="}"),
                            )
                        ]
                    )

                    # Reset state for next tool
                    self.json_closed = True
                    self.in_function = False
                    self.accumulated_params = {}

                    logger.debug("[M2_STREAMING] Tool call completed")

                    return result
                else:
                    # Don't close JSON yet, continue processing parameters
                    return None

            # Look for parameters
            # Find all parameter starts
            param_starts = []
            idx = 0
            while True:
                idx = tool_text.find(self.parameter_prefix, idx)
                if idx == -1:
                    break
                param_starts.append(idx)
                idx += len(self.parameter_prefix)

            # Check if we should start a new parameter
            if (
                not self.in_param
                and self.param_count < len(param_starts)
                and len(param_starts) > self.param_count
            ):
                # Process the next parameter
                param_idx = param_starts[self.param_count]
                param_start = param_idx + len(self.parameter_prefix)
                remaining = tool_text[param_start:]

                if ">" in remaining:
                    # We have the complete parameter name
                    name_end = remaining.find(">")
                    param_name_raw = remaining[:name_end]
                    self.current_param_name = self._extract_name(param_name_raw)

                    # Find the parameter value
                    value_start = param_start + name_end + 1
                    value_text = tool_text[value_start:]
                    if value_text.startswith("\n"):
                        value_text = value_text[1:]

                    # Find where this parameter ends
                    param_end_idx = value_text.find(self.parameter_end_token)
                    if param_end_idx == -1:
                        # No closing tag, look for next parameter or function end
                        next_param_idx = value_text.find(self.parameter_prefix)
                        func_end_idx = value_text.find(self.invoke_end_token)

                        if next_param_idx != -1 and (
                            func_end_idx == -1 or next_param_idx < func_end_idx
                        ):
                            param_end_idx = next_param_idx
                        elif func_end_idx != -1:
                            param_end_idx = func_end_idx
                        else:
                            # Neither found, check if tool call is complete
                            if self.invoke_end_token in tool_text:
                                # Tool call and parameter is complete
                                param_end_idx = len(value_text)
                            else:
                                # Still streaming, wait for more content
                                return None

                    if param_end_idx != -1:
                        # Complete parameter found
                        param_value = value_text[:param_end_idx]
                        if param_value.endswith("\n"):
                            param_value = param_value[:-1]

                        # Store raw value for later processing
                        self.accumulated_params[self.current_param_name] = param_value

                        # Get parameter configuration for type conversion
                        param_config = {}
                        if self.streaming_request and self.streaming_request.tools:
                            for tool in self.streaming_request.tools:
                                if (
                                    hasattr(tool, "function")
                                    and tool.function.name == self.current_function_name
                                    and hasattr(tool.function, "parameters")
                                ):
                                    params = tool.function.parameters
                                    if (
                                        isinstance(params, dict)
                                        and "properties" in params
                                    ):
                                        param_config = params["properties"]
                                    break

                        # Get parameter type
                        param_type = "string"
                        if (
                            self.current_param_name in param_config
                            and isinstance(param_config[self.current_param_name], dict)
                            and "type" in param_config[self.current_param_name]
                        ):
                            param_type = param_config[self.current_param_name]["type"]

                        # Convert param value to appropriate type
                        converted_value = self._convert_param_value(
                            param_value, param_type
                        )

                        # Build JSON fragment based on the converted type
                        # Use json.dumps to properly serialize the value
                        serialized_value = json.dumps(
                            converted_value, ensure_ascii=False
                        )

                        if self.param_count == 0:
                            json_fragment = (
                                f'"{self.current_param_name}": {serialized_value}'
                            )
                        else:
                            json_fragment = (
                                f', "{self.current_param_name}": {serialized_value}'
                            )

                        self.param_count += 1

                        return DeltaMessage(
                            tool_calls=[
                                DeltaToolCall(
                                    index=self.current_tool_index,
                                    function=DeltaFunctionCall(arguments=json_fragment),
                                )
                            ]
                        )

        return None

accumulated_params instance-attribute ¶

accumulated_params: dict = {}

accumulated_text instance-attribute ¶

accumulated_text: str = ''

current_function_name instance-attribute ¶

current_function_name: str | None = None

current_param_name instance-attribute ¶

current_param_name: str | None = None

current_param_value instance-attribute ¶

current_param_value: str = ''

current_tool_id instance-attribute ¶

current_tool_id: str | None = None

current_tool_index instance-attribute ¶

current_tool_index: int = 0

current_tool_name_sent instance-attribute ¶

current_tool_name_sent: bool = False

failed_count instance-attribute ¶

failed_count: int = 0

header_sent instance-attribute ¶

header_sent: bool = False

in_function instance-attribute ¶

in_function: bool = False

in_param instance-attribute ¶

in_param: bool = False

invoke_complete_regex instance-attribute ¶

invoke_complete_regex = compile(
    "<invoke name=(.*?)</invoke>", DOTALL
)

invoke_end_token instance-attribute ¶

invoke_end_token: str = '</invoke>'

invoke_index instance-attribute ¶

invoke_index: int = 0

invoke_start_prefix instance-attribute ¶

invoke_start_prefix: str = '<invoke name='

is_tool_call_started instance-attribute ¶

is_tool_call_started: bool = False

json_closed instance-attribute ¶

json_closed: bool = False

json_started instance-attribute ¶

json_started: bool = False

param_count instance-attribute ¶

param_count: int = 0

parameter_complete_regex instance-attribute ¶

parameter_complete_regex = compile(
    "<parameter name=(.*?)</parameter>", DOTALL
)

parameter_end_token instance-attribute ¶

parameter_end_token: str = '</parameter>'

parameter_prefix instance-attribute ¶

parameter_prefix: str = '<parameter name='

prev_tool_call_arr instance-attribute ¶

prev_tool_call_arr: list[dict] = []

streamed_args_for_tool instance-attribute ¶

streamed_args_for_tool: list[str] = []

streaming_request instance-attribute ¶

streaming_request: ChatCompletionRequest | None = None

tool_call_complete_regex instance-attribute ¶

tool_call_complete_regex = compile(
    "<minimax:tool_call>(.*?)</minimax:tool_call>", DOTALL
)

tool_call_end_token instance-attribute ¶

tool_call_end_token: str = '</minimax:tool_call>'

tool_call_end_token_id instance-attribute ¶

tool_call_end_token_id = get(tool_call_end_token)

tool_call_start_token instance-attribute ¶

tool_call_start_token: str = '<minimax:tool_call>'

tool_call_start_token_id instance-attribute ¶

tool_call_start_token_id = get(tool_call_start_token)

__init__ ¶

__init__(tokenizer: AnyTokenizer)
Source code in vllm/entrypoints/openai/tool_parsers/minimax_m2_tool_parser.py
def __init__(self, tokenizer: AnyTokenizer):
    super().__init__(tokenizer)

    self.prev_tool_call_arr: list[dict] = []

    # Sentinel tokens
    self.tool_call_start_token: str = "<minimax:tool_call>"
    self.tool_call_end_token: str = "</minimax:tool_call>"
    self.invoke_start_prefix: str = "<invoke name="
    self.invoke_end_token: str = "</invoke>"
    self.parameter_prefix: str = "<parameter name="
    self.parameter_end_token: str = "</parameter>"

    # Streaming state variables
    self.current_tool_name_sent: bool = False
    # Override base class type - we use string IDs for tool calls
    self.current_tool_id: str | None = None  # type: ignore
    self.streamed_args_for_tool: list[str] = []
    self.is_tool_call_started: bool = False
    self.failed_count: int = 0

    # Initialize streaming state variables
    self.current_tool_index: int = 0
    self.invoke_index: int = 0
    self.header_sent: bool = False
    self.current_function_name: str | None = None
    self.current_param_name: str | None = None
    self.current_param_value: str = ""
    self.param_count: int = 0
    self.in_param: bool = False
    self.in_function: bool = False
    self.accumulated_text: str = ""
    self.json_started: bool = False
    self.json_closed: bool = False
    self.accumulated_params: dict = {}
    self.streaming_request: ChatCompletionRequest | None = None

    # Enhanced streaming state - reset for each new message
    self._reset_streaming_state()

    # Regex patterns for complete parsing
    self.tool_call_complete_regex = re.compile(
        r"<minimax:tool_call>(.*?)</minimax:tool_call>", re.DOTALL
    )
    self.invoke_complete_regex = re.compile(
        r"<invoke name=(.*?)</invoke>", re.DOTALL
    )
    self.parameter_complete_regex = re.compile(
        r"<parameter name=(.*?)</parameter>", re.DOTALL
    )

    if not self.model_tokenizer:
        raise ValueError(
            "The model tokenizer must be passed to the ToolParser "
            "constructor during construction."
        )

    self.tool_call_start_token_id = self.vocab.get(self.tool_call_start_token)
    self.tool_call_end_token_id = self.vocab.get(self.tool_call_end_token)

    if self.tool_call_start_token_id is None or self.tool_call_end_token_id is None:
        raise RuntimeError(
            "MiniMax M2 Tool parser could not locate tool call start/end "
            "tokens in the tokenizer!"
        )

    logger.info(
        "vLLM Successfully import tool parser %s !", self.__class__.__name__
    )

_convert_param_value ¶

_convert_param_value(value: str, param_type: str) -> Any

Convert parameter value to the correct type.

Source code in vllm/entrypoints/openai/tool_parsers/minimax_m2_tool_parser.py
def _convert_param_value(self, value: str, param_type: str) -> Any:
    """Convert parameter value to the correct type."""
    if value.lower() == "null":
        return None

    param_type = param_type.lower()
    if param_type in ["string", "str", "text"]:
        return value
    elif param_type in ["integer", "int"]:
        try:
            return int(value)
        except (ValueError, TypeError):
            return value
    elif param_type in ["number", "float"]:
        try:
            val = float(value)
            return val if val != int(val) else int(val)
        except (ValueError, TypeError):
            return value
    elif param_type in ["boolean", "bool"]:
        return value.lower() in ["true", "1"]
    elif param_type in ["object", "array"]:
        try:
            return json.loads(value)
        except json.JSONDecodeError:
            return value
    else:
        # Try JSON parse first, fallback to string
        try:
            return json.loads(value)
        except json.JSONDecodeError:
            return value

_extract_name ¶

_extract_name(name_str: str) -> str

Extract name from quoted string.

Source code in vllm/entrypoints/openai/tool_parsers/minimax_m2_tool_parser.py
def _extract_name(self, name_str: str) -> str:
    """Extract name from quoted string."""
    name_str = name_str.strip()
    if (
        name_str.startswith('"')
        and name_str.endswith('"')
        or name_str.startswith("'")
        and name_str.endswith("'")
    ):
        return name_str[1:-1]
    return name_str

_generate_tool_call_id ¶

_generate_tool_call_id() -> str

Generate a unique tool call ID.

Source code in vllm/entrypoints/openai/tool_parsers/minimax_m2_tool_parser.py
def _generate_tool_call_id(self) -> str:
    """Generate a unique tool call ID."""
    return f"call_{uuid.uuid4().hex[:24]}"

_parse_single_invoke ¶

_parse_single_invoke(
    invoke_str: str, tools: list | None
) -> ToolCall | None

Parse a single block.

Source code in vllm/entrypoints/openai/tool_parsers/minimax_m2_tool_parser.py
def _parse_single_invoke(
    self, invoke_str: str, tools: list | None
) -> ToolCall | None:
    """Parse a single <invoke> block."""
    # Extract function name
    name_match = re.search(r"^([^>]+)", invoke_str)
    if not name_match:
        return None

    function_name = self._extract_name(name_match.group(1))

    # Get parameter configuration
    param_config = {}
    if tools:
        for tool in tools:
            if (
                hasattr(tool, "function")
                and tool.function.name == function_name
                and hasattr(tool.function, "parameters")
            ):
                params = tool.function.parameters
                if isinstance(params, dict) and "properties" in params:
                    param_config = params["properties"]
                break

    # Extract parameters
    param_dict = {}
    for match in self.parameter_complete_regex.findall(invoke_str):
        param_match = re.search(r"^([^>]+)>(.*)", match, re.DOTALL)
        if param_match:
            param_name = self._extract_name(param_match.group(1))
            param_value = param_match.group(2).strip()
            if param_value.startswith("\n"):
                param_value = param_value[1:]
            if param_value.endswith("\n"):
                param_value = param_value[:-1]

            # Get parameter type
            param_type = "string"
            if (
                param_name in param_config
                and isinstance(param_config[param_name], dict)
                and "type" in param_config[param_name]
            ):
                param_type = param_config[param_name]["type"]

            # Convert value
            param_dict[param_name] = self._convert_param_value(
                param_value, param_type
            )

    return ToolCall(
        type="function",
        function=FunctionCall(
            name=function_name,
            arguments=json.dumps(param_dict, ensure_ascii=False),
        ),
    )

_reset_streaming_state ¶

_reset_streaming_state()

Reset all streaming state.

Source code in vllm/entrypoints/openai/tool_parsers/minimax_m2_tool_parser.py
def _reset_streaming_state(self):
    """Reset all streaming state."""
    self.current_tool_index = 0
    self.invoke_index = 0
    self.is_tool_call_started = False
    self.header_sent = False
    self.current_tool_id = None
    self.current_function_name = None
    self.current_param_name = None
    self.current_param_value = ""
    self.param_count = 0
    self.in_param = False
    self.in_function = False
    self.accumulated_text = ""
    self.json_started = False
    self.json_closed = False
    # Store accumulated parameters for type conversion
    self.accumulated_params = {}
    self.streaming_request = None
    # Clear previous tool call history to avoid state pollution
    self.prev_tool_call_arr.clear()

extract_tool_calls ¶

extract_tool_calls(
    model_output: str, request: ChatCompletionRequest
) -> ExtractedToolCallInformation

Extract tool calls from complete model output (non-streaming).

Source code in vllm/entrypoints/openai/tool_parsers/minimax_m2_tool_parser.py
def extract_tool_calls(
    self,
    model_output: str,
    request: ChatCompletionRequest,
) -> ExtractedToolCallInformation:
    """Extract tool calls from complete model output (non-streaming)."""
    # Quick check
    if self.tool_call_start_token not in model_output:
        return ExtractedToolCallInformation(
            tools_called=False, tool_calls=[], content=model_output
        )

    try:
        tool_calls = []

        # Find all complete tool_call blocks
        for tool_call_match in self.tool_call_complete_regex.findall(model_output):
            # Find all invokes within this tool_call
            for invoke_match in self.invoke_complete_regex.findall(tool_call_match):
                tool_call = self._parse_single_invoke(
                    invoke_match, request.tools if request else None
                )
                if tool_call:
                    tool_calls.append(tool_call)

        if not tool_calls:
            return ExtractedToolCallInformation(
                tools_called=False, tool_calls=[], content=model_output
            )

        # Update prev_tool_call_arr
        self.prev_tool_call_arr.clear()
        for tool_call in tool_calls:
            self.prev_tool_call_arr.append(
                {
                    "name": tool_call.function.name,
                    "arguments": tool_call.function.arguments,
                }
            )

        # Extract content before first tool call
        first_tool_idx = model_output.find(self.tool_call_start_token)
        content = model_output[:first_tool_idx] if first_tool_idx > 0 else None

        return ExtractedToolCallInformation(
            tools_called=True, tool_calls=tool_calls, content=content
        )

    except Exception:
        logger.exception("Error extracting tool calls")
        return ExtractedToolCallInformation(
            tools_called=False, tool_calls=[], content=model_output
        )

extract_tool_calls_streaming ¶

extract_tool_calls_streaming(
    previous_text: str,
    current_text: str,
    delta_text: str,
    previous_token_ids: Sequence[int],
    current_token_ids: Sequence[int],
    delta_token_ids: Sequence[int],
    request: ChatCompletionRequest,
) -> DeltaMessage | None

Extract tool calls from streaming model output.

Source code in vllm/entrypoints/openai/tool_parsers/minimax_m2_tool_parser.py
def extract_tool_calls_streaming(
    self,
    previous_text: str,
    current_text: str,
    delta_text: str,
    previous_token_ids: Sequence[int],  # pylint: disable=unused-argument
    current_token_ids: Sequence[int],  # pylint: disable=unused-argument
    delta_token_ids: Sequence[int],
    request: ChatCompletionRequest,
) -> DeltaMessage | None:
    """Extract tool calls from streaming model output."""

    # Store request for type conversion
    if not previous_text or self.tool_call_start_token in delta_text:
        self._reset_streaming_state()
        self.streaming_request = request

    # If no delta text, return None unless it's an EOS token after tools
    if not delta_text:
        # Check if this is an EOS token after all tool calls are complete
        if delta_token_ids and self.tool_call_end_token_id not in delta_token_ids:
            # Count complete tool calls
            complete_calls = len(
                self.tool_call_complete_regex.findall(current_text)
            )

            # If we have completed tool calls and populated prev_tool_call_arr
            if complete_calls > 0 and len(self.prev_tool_call_arr) > 0:
                # Check if all tool calls are closed
                open_calls = current_text.count(
                    self.tool_call_start_token
                ) - current_text.count(self.tool_call_end_token)
                if open_calls == 0:
                    # Return empty delta for finish_reason processing
                    return DeltaMessage(content="")
            elif not self.is_tool_call_started and current_text:
                # This is a regular content response that's now complete
                return DeltaMessage(content="")
        return None

    # Update accumulated text
    self.accumulated_text = current_text

    # Check if we need to advance to next tool
    if self.json_closed and not self.in_function:
        # Check if this tool call has ended
        invoke_ends = current_text.count(self.invoke_end_token)
        if invoke_ends > self.current_tool_index:
            # This tool has ended, advance to next
            self.current_tool_index += 1
            self.header_sent = False
            self.param_count = 0
            self.json_started = False
            self.json_closed = False
            self.in_function = False  # Now we can safely set this to False
            self.accumulated_params = {}
            # Continue processing next tool
            return None

    # Handle normal content before tool calls
    if not self.is_tool_call_started:
        # Check if tool call is starting
        if (
            self.tool_call_start_token_id in delta_token_ids
            or self.tool_call_start_token in delta_text
        ):
            self.is_tool_call_started = True
            # Return any content before the tool call
            if self.tool_call_start_token in delta_text:
                content_before = delta_text[
                    : delta_text.index(self.tool_call_start_token)
                ]
                if content_before:
                    return DeltaMessage(content=content_before)
            return None
        else:
            # Check if we're between tool calls - skip whitespace
            if (
                current_text.rstrip().endswith(self.tool_call_end_token)
                and delta_text.strip() == ""
            ):
                # We just ended a tool call, skip whitespace
                return None
            # Normal content, no tool call
            return DeltaMessage(content=delta_text)

    # Check if we're between tool calls (waiting for next one)
    invoke_starts_count = current_text.count(self.invoke_start_prefix)
    if self.current_tool_index >= invoke_starts_count:
        # We're past all tool calls, shouldn't be here
        return None

    # Find the current tool call portion
    invoke_start_positions: list[int] = []
    idx = 0
    while True:
        idx = current_text.find(self.invoke_start_prefix, idx)
        if idx == -1:
            break
        invoke_start_positions.append(idx)
        idx += len(self.invoke_start_prefix)

    if self.current_tool_index >= len(invoke_start_positions):
        # No more tool calls to process yet
        return None

    invoke_start_idx = invoke_start_positions[self.current_tool_index]
    # Find where this tool call ends (or current position if not ended yet)
    invoke_end_idx = current_text.find(self.invoke_end_token, invoke_start_idx)
    if invoke_end_idx == -1:
        tool_text = current_text[invoke_start_idx:]
    else:
        tool_text = current_text[
            invoke_start_idx : invoke_end_idx + len(self.invoke_end_token)
        ]

    # Looking for function header
    if not self.header_sent:
        if self.invoke_start_prefix in tool_text:
            func_start = tool_text.find(self.invoke_start_prefix) + len(
                self.invoke_start_prefix
            )
            # Find the end quote for the function name
            func_end = tool_text.find(">", func_start)

            if func_end != -1:
                # Found complete function name
                function_name_raw = tool_text[func_start:func_end]
                self.current_function_name = self._extract_name(function_name_raw)
                self.current_tool_id = self._generate_tool_call_id()
                self.header_sent = True
                self.in_function = True

                # Add to prev_tool_call_arr immediately when we detect a tool call
                # Each tool call should be recorded regardless of function name
                # Ensure we don't add the same tool call index multiple times
                if len(self.prev_tool_call_arr) <= self.current_tool_index:
                    self.prev_tool_call_arr.append(
                        {
                            "name": self.current_function_name,
                            "arguments": "{}",  # Placeholder, will be updated later
                        }
                    )

                # Send header with function info
                return DeltaMessage(
                    tool_calls=[
                        DeltaToolCall(
                            index=self.current_tool_index,
                            id=self.current_tool_id,
                            function=DeltaFunctionCall(
                                name=self.current_function_name, arguments=""
                            ),
                            type="function",
                        )
                    ]
                )
        return None

    # We've sent header, now handle function body
    if self.in_function:
        # Send opening brace if not sent yet
        if self.in_function and not self.json_started:
            self.json_started = True
            return DeltaMessage(
                tool_calls=[
                    DeltaToolCall(
                        index=self.current_tool_index,
                        function=DeltaFunctionCall(arguments="{"),
                    )
                ]
            )

        # Make sure json_started is set if we're processing parameters
        if not self.json_started:
            self.json_started = True

        # Check for function end in accumulated text
        if not self.json_closed and self.invoke_end_token in tool_text:
            # Count total parameters in the tool text
            total_param_count = tool_text.count(self.parameter_prefix)

            # Only close JSON if all parameters have been processed
            if self.param_count >= total_param_count:
                # Close JSON
                self.json_closed = True

                # Extract complete tool call
                # Find the invoke content
                invoke_start = tool_text.find(self.invoke_start_prefix) + len(
                    self.invoke_start_prefix
                )
                invoke_content_end = tool_text.find(
                    self.invoke_end_token, invoke_start
                )
                if invoke_content_end != -1:
                    invoke_content = tool_text[invoke_start:invoke_content_end]
                    # Parse to get the complete arguments
                    try:
                        parsed_tool = self._parse_single_invoke(
                            invoke_content,
                            self.streaming_request.tools
                            if self.streaming_request
                            else None,
                        )
                        if parsed_tool and self.current_tool_index < len(
                            self.prev_tool_call_arr
                        ):
                            # Update existing entry in prev_tool_call_arr
                            args = parsed_tool.function.arguments
                            self.prev_tool_call_arr[self.current_tool_index][
                                "arguments"
                            ] = args
                    except Exception:
                        pass  # Ignore parsing errors during streaming

                result = DeltaMessage(
                    tool_calls=[
                        DeltaToolCall(
                            index=self.current_tool_index,
                            function=DeltaFunctionCall(arguments="}"),
                        )
                    ]
                )

                # Reset state for next tool
                self.json_closed = True
                self.in_function = False
                self.accumulated_params = {}

                logger.debug("[M2_STREAMING] Tool call completed")

                return result
            else:
                # Don't close JSON yet, continue processing parameters
                return None

        # Look for parameters
        # Find all parameter starts
        param_starts = []
        idx = 0
        while True:
            idx = tool_text.find(self.parameter_prefix, idx)
            if idx == -1:
                break
            param_starts.append(idx)
            idx += len(self.parameter_prefix)

        # Check if we should start a new parameter
        if (
            not self.in_param
            and self.param_count < len(param_starts)
            and len(param_starts) > self.param_count
        ):
            # Process the next parameter
            param_idx = param_starts[self.param_count]
            param_start = param_idx + len(self.parameter_prefix)
            remaining = tool_text[param_start:]

            if ">" in remaining:
                # We have the complete parameter name
                name_end = remaining.find(">")
                param_name_raw = remaining[:name_end]
                self.current_param_name = self._extract_name(param_name_raw)

                # Find the parameter value
                value_start = param_start + name_end + 1
                value_text = tool_text[value_start:]
                if value_text.startswith("\n"):
                    value_text = value_text[1:]

                # Find where this parameter ends
                param_end_idx = value_text.find(self.parameter_end_token)
                if param_end_idx == -1:
                    # No closing tag, look for next parameter or function end
                    next_param_idx = value_text.find(self.parameter_prefix)
                    func_end_idx = value_text.find(self.invoke_end_token)

                    if next_param_idx != -1 and (
                        func_end_idx == -1 or next_param_idx < func_end_idx
                    ):
                        param_end_idx = next_param_idx
                    elif func_end_idx != -1:
                        param_end_idx = func_end_idx
                    else:
                        # Neither found, check if tool call is complete
                        if self.invoke_end_token in tool_text:
                            # Tool call and parameter is complete
                            param_end_idx = len(value_text)
                        else:
                            # Still streaming, wait for more content
                            return None

                if param_end_idx != -1:
                    # Complete parameter found
                    param_value = value_text[:param_end_idx]
                    if param_value.endswith("\n"):
                        param_value = param_value[:-1]

                    # Store raw value for later processing
                    self.accumulated_params[self.current_param_name] = param_value

                    # Get parameter configuration for type conversion
                    param_config = {}
                    if self.streaming_request and self.streaming_request.tools:
                        for tool in self.streaming_request.tools:
                            if (
                                hasattr(tool, "function")
                                and tool.function.name == self.current_function_name
                                and hasattr(tool.function, "parameters")
                            ):
                                params = tool.function.parameters
                                if (
                                    isinstance(params, dict)
                                    and "properties" in params
                                ):
                                    param_config = params["properties"]
                                break

                    # Get parameter type
                    param_type = "string"
                    if (
                        self.current_param_name in param_config
                        and isinstance(param_config[self.current_param_name], dict)
                        and "type" in param_config[self.current_param_name]
                    ):
                        param_type = param_config[self.current_param_name]["type"]

                    # Convert param value to appropriate type
                    converted_value = self._convert_param_value(
                        param_value, param_type
                    )

                    # Build JSON fragment based on the converted type
                    # Use json.dumps to properly serialize the value
                    serialized_value = json.dumps(
                        converted_value, ensure_ascii=False
                    )

                    if self.param_count == 0:
                        json_fragment = (
                            f'"{self.current_param_name}": {serialized_value}'
                        )
                    else:
                        json_fragment = (
                            f', "{self.current_param_name}": {serialized_value}'
                        )

                    self.param_count += 1

                    return DeltaMessage(
                        tool_calls=[
                            DeltaToolCall(
                                index=self.current_tool_index,
                                function=DeltaFunctionCall(arguments=json_fragment),
                            )
                        ]
                    )

    return None