Skip to content

vllm_mlx.tool_parsers.minimax_tool_parser

MiniMax tool call parser for vllm-mlx.

View the complete module source at #L1-L178.

API details

Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.

vllm_mlx.tool_parsers.minimax_tool_parser

MiniMax tool call parser for vllm-mlx.

Parses the MiniMax-M2 native XML tool call format: param-value

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser

MiniMaxToolParser(tokenizer: PreTrainedTokenizerBase | None = None)

Bases: ToolParser

Parser for MiniMax-M2 tool call format.

Format

value

Source code in vllm_mlx/tool_parsers/abstract_tool_parser.py
def __init__(self, tokenizer: PreTrainedTokenizerBase | None = None):
    """
    Initialize the tool parser.

    Args:
        tokenizer: The tokenizer for the model (optional, some parsers need it)
    """
    self.model_tokenizer = tokenizer
    # State for streaming parsing
    self.current_tool_id: int = -1
    self.prev_tool_call_arr: list[dict] = []

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser.TOOL_CALL_BLOCK class-attribute instance-attribute

TOOL_CALL_BLOCK = re.compile('<minimax:tool_call>(.*?)</minimax:tool_call>', re.DOTALL)

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser.INVOKE_PATTERN class-attribute instance-attribute

INVOKE_PATTERN = re.compile('<invoke\\s+name="([^"]+)">(.*?)</invoke>', re.DOTALL)

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser.PARAM_PATTERN class-attribute instance-attribute

PARAM_PATTERN = re.compile('<parameter\\s+name="([^"]+)">(.*?)</parameter>', re.DOTALL)

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser.THINK_PATTERN class-attribute instance-attribute

THINK_PATTERN = re.compile('<think>.*?</think>', re.DOTALL)

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser._extract_invokes

_extract_invokes(text: str) -> list[dict[str, Any]]

Extract tool calls from invoke elements, with or without wrapper.

Source code in vllm_mlx/tool_parsers/minimax_tool_parser.py
def _extract_invokes(self, text: str) -> list[dict[str, Any]]:
    """Extract tool calls from invoke elements, with or without wrapper."""
    tool_calls: list[dict[str, Any]] = []
    invokes = self.INVOKE_PATTERN.findall(text)
    for func_name, params_block in invokes:
        params = self.PARAM_PATTERN.findall(params_block)
        # Skip bare <invoke> tags without parameters (hallucinated junk)
        if not params:
            continue
        arguments = {}
        for p_name, p_value in params:
            p_value = p_value.strip()
            try:
                arguments[p_name] = json.loads(p_value)
            except (json.JSONDecodeError, ValueError):
                arguments[p_name] = p_value

        tool_calls.append(
            {
                "id": generate_tool_id(),
                "name": func_name.strip(),
                "arguments": json.dumps(arguments, ensure_ascii=False),
            }
        )
    return tool_calls

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser.extract_tool_calls

extract_tool_calls(model_output: str, request: dict[str, Any] | None = None) -> ExtractedToolCallInformation

Extract wrapped or bare MiniMax invoke elements from complete output.

Source code in vllm_mlx/tool_parsers/minimax_tool_parser.py
def extract_tool_calls(
    self, model_output: str, request: dict[str, Any] | None = None
) -> ExtractedToolCallInformation:
    """Extract wrapped or bare MiniMax invoke elements from complete output."""

    # Try wrapped format first: <minimax:tool_call>...<invoke>...</minimax:tool_call>
    blocks = self.TOOL_CALL_BLOCK.findall(model_output)
    if blocks:
        tool_calls: list[dict[str, Any]] = []
        for block in blocks:
            tool_calls.extend(self._extract_invokes(block))

        cleaned = self.TOOL_CALL_BLOCK.sub("", model_output).strip()
        cleaned = self.THINK_PATTERN.sub("", cleaned).strip()
        cleaned = re.sub(r"\[e~\[.*$", "", cleaned).strip()

        return ExtractedToolCallInformation(
            tools_called=bool(tool_calls),
            tool_calls=tool_calls,
            content=cleaned if cleaned else None,
        )

    # Fallback: bare <invoke> without <minimax:tool_call> wrapper
    # (model sometimes emits tool calls inside <think> without wrapper)
    tool_calls = self._extract_invokes(model_output)
    if tool_calls:
        # Strip matched invoke blocks and thinking from content
        cleaned = self.INVOKE_PATTERN.sub("", model_output).strip()
        cleaned = self.THINK_PATTERN.sub("", cleaned).strip()
        cleaned = re.sub(r"\[e~\[.*$", "", cleaned).strip()
        # Remove leftover closing tags
        cleaned = cleaned.replace("</invoke>", "").strip()

        return ExtractedToolCallInformation(
            tools_called=True,
            tool_calls=tool_calls,
            content=cleaned if cleaned else None,
        )

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

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser._has_tool_start

_has_tool_start(text: str) -> bool

Check if text contains the start of a tool call block.

Source code in vllm_mlx/tool_parsers/minimax_tool_parser.py
def _has_tool_start(self, text: str) -> bool:
    """Check if text contains the start of a tool call block."""
    return "<minimax:tool_call>" in text or (
        '<invoke name="' in text and self.INVOKE_PATTERN.search(text) is not None
    )

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser._has_tool_end

_has_tool_end(current: str, previous: str) -> bool

Check if a tool call block just completed.

Source code in vllm_mlx/tool_parsers/minimax_tool_parser.py
def _has_tool_end(self, current: str, previous: str) -> bool:
    """Check if a tool call block just completed."""
    # If wrapped format is used, only trigger on the wrapper closing tag
    if "<minimax:tool_call>" in current:
        return (
            "</minimax:tool_call>" in current
            and "</minimax:tool_call>" not in previous
        )
    # Bare invoke: </invoke> just appeared
    if "</invoke>" in current and "</invoke>" not in previous:
        return True
    return False

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser.extract_tool_calls_streaming

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

Emit content deltas or a completed MiniMax tool-call delta.

Source code in vllm_mlx/tool_parsers/minimax_tool_parser.py
def extract_tool_calls_streaming(
    self,
    previous_text: str,
    current_text: str,
    delta_text: str,
    previous_token_ids: Sequence[int] | None = None,
    current_token_ids: Sequence[int] | None = None,
    delta_token_ids: Sequence[int] | None = None,
    request: dict[str, Any] | None = None,
) -> dict[str, Any] | None:
    """Emit content deltas or a completed MiniMax tool-call delta."""

    # Not inside a tool call block yet — pass content through
    if not self._has_tool_start(current_text):
        return {"content": delta_text}

    # Tool call block just completed
    if self._has_tool_end(current_text, previous_text):
        result = self.extract_tool_calls(current_text)
        if result.tools_called:
            return {
                "tool_calls": [
                    {
                        "index": i,
                        "id": tc["id"],
                        "type": "function",
                        "function": {
                            "name": tc["name"],
                            "arguments": tc["arguments"],
                        },
                    }
                    for i, tc in enumerate(result.tool_calls)
                ]
            }

    # Inside tool call block but not yet complete — suppress output
    return None

vllm_mlx.tool_parsers.minimax_tool_parser.generate_tool_id

generate_tool_id() -> str

Return a short OpenAI-compatible identifier for a parsed tool call.

Source code in vllm_mlx/tool_parsers/minimax_tool_parser.py
def generate_tool_id() -> str:
    """Return a short OpenAI-compatible identifier for a parsed tool call."""

    return f"call_{uuid.uuid4().hex[:8]}"

Complete contract reference

Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.

vllm_mlx.tool_parsers.minimax_tool_parser.generate_tool_id · function
vllm_mlx.tool_parsers.minimax_tool_parser.generate_tool_id() -> str

Return a short OpenAI-compatible identifier for a parsed tool call.

Parameters

This callable has no explicit inputs.

Returns

  • Type: str
  • Direct return expressions: f'call_{uuid.uuid4().hex[:8]}'

Exceptions and behavior

Function generate_tool_id calls uuid.uuid4; returns f'call_{uuid.uuid4().hex[:8]}'. No direct raise statement appears in this definition.

View source #L26-L29.

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser · class
vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser()

Parser for MiniMax-M2 tool call format.

Parameters

This callable has no explicit inputs.

Returns

  • Constructs: vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser

Exceptions and behavior

Class MiniMaxToolParser derives from ToolParser and declares 5 direct member(s). No direct raise statement appears in this definition.

View source #L33-L178.

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser._extract_invokes · method
vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser._extract_invokes(text: str) -> list[dict[str, Any]]

Extract tool calls from invoke elements, with or without wrapper.

Parameters

Name Type Required Default Description
text str yes none Required positional or keyword input.

Returns

  • Type: list[dict[str, Any]]
  • Direct return expressions: tool_calls

Exceptions and behavior

Method MiniMaxToolParser._extract_invokes calls self.INVOKE_PATTERN.findall, self.PARAM_PATTERN.findall, p_value.strip, json.loads; returns tool_calls. No direct raise statement appears in this definition.

View source #L54-L78.

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser.extract_tool_calls · method
vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser.extract_tool_calls(model_output: str, request: dict[str, Any] | None = None) -> ExtractedToolCallInformation

Extract wrapped or bare MiniMax invoke elements from complete output.

Parameters

Name Type Required Default Description
model_output str yes none Required positional or keyword input.
request dict[str, Any] \| None no None Optional positional or keyword input; defaults to None.

Returns

  • Type: ExtractedToolCallInformation
  • Direct return expressions: ExtractedToolCallInformation(tools_called=bool(tool_calls), tool_calls=tool_calls, content=cleaned if cleaned else None); ExtractedToolCallInformation(tools_called=True, tool_calls=tool_calls, content=cleaned if cleaned else None); ExtractedToolCallInformation(tools_called=False, tool_calls=[], content=model_output)

Exceptions and behavior

Method MiniMaxToolParser.extract_tool_calls calls self.TOOL_CALL_BLOCK.findall, tool_calls.extend, self._extract_invokes, self.TOOL_CALL_BLOCK.sub('', model_output).strip; has 3 explicit return paths. No direct raise statement appears in this definition.

View source #L80-L121.

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser._has_tool_start · method
vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser._has_tool_start(text: str) -> bool

Check if text contains the start of a tool call block.

Parameters

Name Type Required Default Description
text str yes none Required positional or keyword input.

Returns

  • Type: bool
  • Direct return expressions: '<minimax:tool_call>' in text or ('<invoke name="' in text and self.INVOKE_PATTERN.search(text) is not None)

Exceptions and behavior

Method MiniMaxToolParser._has_tool_start calls self.INVOKE_PATTERN.search; returns '<minimax:tool_call>' in text or ('<invoke name="' in text and self.INVOKE_PATTERN.search(text) is not None). No direct raise statement appears in this definition.

View source #L123-L127.

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser._has_tool_end · method
vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser._has_tool_end(current: str, previous: str) -> bool

Check if a tool call block just completed.

Parameters

Name Type Required Default Description
current str yes none Required positional or keyword input.
previous str yes none Required positional or keyword input.

Returns

  • Type: bool
  • Direct return expressions: '</minimax:tool_call>' in current and '</minimax:tool_call>' not in previous; True; False

Exceptions and behavior

Method MiniMaxToolParser._has_tool_end has 3 explicit return paths. No direct raise statement appears in this definition.

View source #L129-L140.

vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser.extract_tool_calls_streaming · method
vllm_mlx.tool_parsers.minimax_tool_parser.MiniMaxToolParser.extract_tool_calls_streaming(previous_text: str, current_text: str, delta_text: str, previous_token_ids: Sequence[int] | None = None, current_token_ids: Sequence[int] | None = None, delta_token_ids: Sequence[int] | None = None, request: dict[str, Any] | None = None) -> dict[str, Any] | None

Emit content deltas or a completed MiniMax tool-call delta.

Parameters

Name Type Required Default Description
previous_text str yes none Required positional or keyword input.
current_text str yes none Required positional or keyword input.
delta_text str yes none Required positional or keyword input.
previous_token_ids Sequence[int] \| None no None Optional positional or keyword input; defaults to None.
current_token_ids Sequence[int] \| None no None Optional positional or keyword input; defaults to None.
delta_token_ids Sequence[int] \| None no None Optional positional or keyword input; defaults to None.
request dict[str, Any] \| None no None Optional positional or keyword input; defaults to None.

Returns

  • Type: dict[str, Any] | None
  • Direct return expressions: {'content': delta_text}; {'tool_calls': [{'index': i, 'id': tc['id'], 'type': 'function', 'function': {'name': tc['name'], 'arguments': tc['argu…; None

Exceptions and behavior

Method MiniMaxToolParser.extract_tool_calls_streaming calls self._has_tool_start, self._has_tool_end, self.extract_tool_calls, enumerate; has 3 explicit return paths. No direct raise statement appears in this definition.

View source #L142-L178.

Complete symbol map

This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.

Symbol Kind Signature and inputs What it does Source
generate_tool_id function generate_tool_id() -> str Return a short OpenAI-compatible identifier for a parsed tool call. #L26-L29
MiniMaxToolParser class MiniMaxToolParser() Parser for MiniMax-M2 tool call format. #L33-L178
MiniMaxToolParser._extract_invokes method MiniMaxToolParser._extract_invokes(text: str) -> list[dict[str, Any]] Extract tool calls from invoke elements, with or without wrapper. #L54-L78
MiniMaxToolParser.extract_tool_calls method MiniMaxToolParser.extract_tool_calls(model_output: str, request: dict[str, Any] \| None = None) -> ExtractedToolCallInformation Extract wrapped or bare MiniMax invoke elements from complete output. #L80-L121
MiniMaxToolParser._has_tool_start method MiniMaxToolParser._has_tool_start(text: str) -> bool Check if text contains the start of a tool call block. #L123-L127
MiniMaxToolParser._has_tool_end method MiniMaxToolParser._has_tool_end(current: str, previous: str) -> bool Check if a tool call block just completed. #L129-L140
MiniMaxToolParser.extract_tool_calls_streaming method MiniMaxToolParser.extract_tool_calls_streaming(previous_text: str, current_text: str, delta_text: str, previous_token_ids: Sequence[int] \| None = None, current_token_ids: Sequence[int] \| None = None, delta_token_ids: Sequence[int] \| None = None, request: dict[str, Any] \| None = None) -> dict[str, Any] \| None Emit content deltas or a completed MiniMax tool-call delta. #L142-L178