Skip to content

vllm_mlx.tool_parsers.glm47_tool_parser

GLM-4.7 tool call parser for vllm-mlx.

View the complete module source at #L1-L184.

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.glm47_tool_parser

GLM-4.7 tool call parser for vllm-mlx.

Handles GLM-4.7-Flash style tool calling format. Based on vLLM's glm47_moe_tool_parser.py

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser

Glm47ToolParser(tokenizer: PreTrainedTokenizerBase | None = None)

Bases: ToolParser

Tool call parser for GLM-4.7 and GLM-4.7-Flash models.

Supports GLM-4.7 tool call format: function_name param1value1 param2value2

Used when --enable-auto-tool-choice --tool-call-parser glm47 are set.

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.glm47_tool_parser.Glm47ToolParser.SUPPORTS_NATIVE_TOOL_FORMAT class-attribute instance-attribute

SUPPORTS_NATIVE_TOOL_FORMAT = True

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser.TOOL_CALL_PATTERN class-attribute instance-attribute

TOOL_CALL_PATTERN = re.compile('<tool_call>(.*?)</tool_call>', re.DOTALL)

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser.FUNC_DETAIL_PATTERN class-attribute instance-attribute

FUNC_DETAIL_PATTERN = re.compile('<tool_call>\\s*([^\\n<]+?)(?:\\n|\\s*)(<arg_key>.*?)?</tool_call>', re.DOTALL)

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser.ARG_PATTERN class-attribute instance-attribute

ARG_PATTERN = re.compile('<arg_key>\\s*(.*?)\\s*</arg_key>\\s*<arg_value>(.*?)</arg_value>', re.DOTALL)

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser._deserialize

_deserialize(value: str) -> Any

Convert string value to appropriate Python type.

Uses json.loads for type coercion, falls back to raw string.

Source code in vllm_mlx/tool_parsers/glm47_tool_parser.py
def _deserialize(self, value: str) -> Any:
    """Convert string value to appropriate Python type.

    Uses json.loads for type coercion, falls back to raw string.
    """
    value = value.strip()

    try:
        return json.loads(value)
    except json.JSONDecodeError:
        return value

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser._get_tool_names

_get_tool_names(request: dict[str, Any] | None) -> set[str]

Extract valid tool names from the request.

Source code in vllm_mlx/tool_parsers/glm47_tool_parser.py
def _get_tool_names(self, request: dict[str, Any] | None) -> set[str]:
    """Extract valid tool names from the request."""
    if not request or "tools" not in request:
        return set()
    return {
        t.get("function", {}).get("name", "")
        for t in request.get("tools", [])
        if isinstance(t, dict)
    }

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser.extract_tool_calls

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

Extract tool calls from a complete GLM-4.7 model response.

Source code in vllm_mlx/tool_parsers/glm47_tool_parser.py
def extract_tool_calls(
    self, model_output: str, request: dict[str, Any] | None = None
) -> ExtractedToolCallInformation:
    """
    Extract tool calls from a complete GLM-4.7 model response.
    """
    tool_calls = []

    # Strip think tags using the base class method (handles both
    # full <think>...</think> and implicit ...</think> patterns)
    cleaned_text = self.strip_think_tags(model_output)

    # Get valid tool names for validation
    valid_names = self._get_tool_names(request)

    # Find all tool call blocks
    matches = self.FUNC_DETAIL_PATTERN.findall(cleaned_text)

    for match in matches:
        func_name = match[0].strip() if match[0] else ""
        args_section = match[1] if len(match) > 1 and match[1] else ""

        if not func_name:
            continue

        # Validate tool name against available tools if provided
        if valid_names and func_name not in valid_names:
            continue

        # Parse arguments
        arguments = {}
        if args_section:
            arg_matches = self.ARG_PATTERN.findall(args_section)
            for arg_key, arg_value in arg_matches:
                key = arg_key.strip()
                value = self._deserialize(arg_value)
                if key:
                    arguments[key] = value

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

    # When tool calls are found, don't return reasoning text as content
    # GLM often outputs thinking/reasoning before tool calls without <think> tags
    if tool_calls:
        return ExtractedToolCallInformation(
            tools_called=True,
            tool_calls=tool_calls,
            content=None,
        )
    else:
        return ExtractedToolCallInformation(
            tools_called=False, tool_calls=[], content=cleaned_text
        )

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser.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

Extract tool calls from streaming GLM-4.7 model output.

Source code in vllm_mlx/tool_parsers/glm47_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:
    """
    Extract tool calls from streaming GLM-4.7 model output.
    """
    # Skip thinking content in streaming
    if "<think>" in current_text and "</think>" not in current_text:
        return None

    # Once <tool_call> is detected, buffer everything until it closes.
    # Do NOT emit content deltas here, because if tool calls are found
    # the non-streaming path sets content=None (reasoning before the
    # tag should not leak as regular content).
    if "<tool_call>" in current_text:
        if "</tool_call>" in delta_text:
            result = self.extract_tool_calls(current_text, request)
            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)
                    ]
                }
        return None

    # No tool call detected yet; strip think tags and emit content
    clean_delta = self.strip_think_tags(delta_text)
    if clean_delta:
        return {"content": clean_delta}
    return None

vllm_mlx.tool_parsers.glm47_tool_parser.generate_tool_id

generate_tool_id() -> str

Generate a unique tool call ID.

Source code in vllm_mlx/tool_parsers/glm47_tool_parser.py
def generate_tool_id() -> str:
    """Generate a unique tool call ID."""
    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.glm47_tool_parser.generate_tool_id · function
vllm_mlx.tool_parsers.glm47_tool_parser.generate_tool_id() -> str

Generate a unique tool call ID.

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 #L22-L24.

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser · class
vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser()

Tool call parser for GLM-4.7 and GLM-4.7-Flash models.

Parameters

This callable has no explicit inputs.

Returns

  • Constructs: vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser

Exceptions and behavior

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

View source #L28-L184.

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser._deserialize · method
vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser._deserialize(value: str) -> Any

Convert string value to appropriate Python type.

Parameters

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

Returns

  • Type: Any
  • Direct return expressions: json.loads(value); value

Exceptions and behavior

Method Glm47ToolParser._deserialize calls value.strip, json.loads; has 2 explicit return paths. No direct raise statement appears in this definition.

View source #L57-L67.

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser._get_tool_names · method
vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser._get_tool_names(request: dict[str, Any] | None) -> set[str]

Extract valid tool names from the request.

Parameters

Name Type Required Default Description
request dict[str, Any] \| None yes none Required positional or keyword input.

Returns

  • Type: set[str]
  • Direct return expressions: set(); {t.get('function', {}).get('name', '') for t in request.get('tools', []) if isinstance(t, dict)}

Exceptions and behavior

Method Glm47ToolParser._get_tool_names calls set, t.get('function', {}).get, t.get, request.get; has 2 explicit return paths. No direct raise statement appears in this definition.

View source #L69-L77.

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

Extract tool calls from a complete GLM-4.7 model response.

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=True, tool_calls=tool_calls, content=None); ExtractedToolCallInformation(tools_called=False, tool_calls=[], content=cleaned_text)

Exceptions and behavior

Method Glm47ToolParser.extract_tool_calls calls self.strip_think_tags, self._get_tool_names, self.FUNC_DETAIL_PATTERN.findall, match[0].strip; has 2 explicit return paths. No direct raise statement appears in this definition.

View source #L79-L137.

vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser.extract_tool_calls_streaming · method
vllm_mlx.tool_parsers.glm47_tool_parser.Glm47ToolParser.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

Extract tool calls from streaming GLM-4.7 model output.

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: None; {'tool_calls': [{'index': i, 'id': tc['id'], 'type': 'function', 'function': {'name': tc['name'], 'arguments': tc['argu…; {'content': clean_delta}

Exceptions and behavior

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

View source #L139-L184.

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 Generate a unique tool call ID. #L22-L24
Glm47ToolParser class Glm47ToolParser() Tool call parser for GLM-4.7 and GLM-4.7-Flash models. #L28-L184
Glm47ToolParser._deserialize method Glm47ToolParser._deserialize(value: str) -> Any Convert string value to appropriate Python type. #L57-L67
Glm47ToolParser._get_tool_names method Glm47ToolParser._get_tool_names(request: dict[str, Any] \| None) -> set[str] Extract valid tool names from the request. #L69-L77
Glm47ToolParser.extract_tool_calls method Glm47ToolParser.extract_tool_calls(model_output: str, request: dict[str, Any] \| None = None) -> ExtractedToolCallInformation Extract tool calls from a complete GLM-4.7 model response. #L79-L137
Glm47ToolParser.extract_tool_calls_streaming method Glm47ToolParser.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 Extract tool calls from streaming GLM-4.7 model output. #L139-L184