Skip to content

vllm.parser.glm47_moe

GLM-4.7 parser for reasoning and tool calls.

GLM-4.7 uses XML-like tool calls::

<tool_call>func_name<arg_key>key</arg_key><arg_value>value</arg_value></tool_call>

The function name can be followed directly by the first <arg_key> tag, and tool calls may have no arguments.

Classes:

  • Glm47MoeParser –

    GLM-4.7 parser backed by the declarative parser engine.

Glm47MoeParser

Bases: ParserEngine

GLM-4.7 parser backed by the declarative parser engine.

Source code in vllm/parser/glm47_moe.py
class Glm47MoeParser(ParserEngine):
    """GLM-4.7 parser backed by the declarative parser engine."""

    def __init__(
        self,
        tokenizer: TokenizerLike,
        tools: list[Tool] | None = None,
        **kwargs,
    ) -> None:
        chat_kwargs = kwargs.get("chat_template_kwargs", {}) or {}
        thinking = chat_kwargs.get("thinking", None)
        enable_thinking = chat_kwargs.get("enable_thinking", None)
        if (thinking is False or enable_thinking is False) and _glm53_always_thinks(
            tokenizer
        ):
            logger.warning_once(
                "Ignoring enable_thinking/thinking: the GLM-5.3 chat template "
                "has no thinking switch, so reasoning is always on and "
                "disabling extraction would leak it into the content."
            )
            thinking = None
            enable_thinking = None
        self.thinking_enabled = (
            True
            if thinking is None and enable_thinking is None
            else bool(thinking) or bool(enable_thinking)
        )
        kwargs.setdefault(
            "parser_engine_config",
            glm47_moe_config(thinking=self.thinking_enabled),
        )
        super().__init__(tokenizer, tools, **kwargs)
        # Each streaming conversion resumes after the last closed pair.
        if self._arg_converter is _glm47_arg_converter:
            closed = _ClosedArgs()
            self._arg_converter = functools.partial(_glm47_arg_converter, closed=closed)
            self._arg_value_extender = functools.partial(
                _glm47_arg_value_extender, closed=closed
            )

    def _emit_name_delta(self, idx: int, deltas, name: str | None) -> None:
        if name is not None:
            name = name.strip()
        super()._emit_name_delta(idx, deltas, name)

    def _handle_tool_end(self, event, deltas) -> None:
        idx = event.tool_index
        if 0 <= idx < len(self._tool_slots):
            self._tool_slots[idx].name = self._tool_slots[idx].name.strip()
        super()._handle_tool_end(event, deltas)

    def extract_reasoning(
        self,
        model_output: str,
        request: ChatCompletionRequest | ResponsesRequest,
    ) -> tuple[str | None, str | None]:
        if not self.thinking_enabled:
            return None, model_output
        return super().extract_reasoning(model_output, request)

_ClosedArgs

Closed key/value pairs of a raw argument text, reused by longer texts.

Raw arguments only grow while a call streams, so a parse of a text that extends text resumes after its last closed value.

Source code in vllm/parser/glm47_moe.py
class _ClosedArgs:
    """Closed key/value pairs of a raw argument text, reused by longer texts.

    Raw arguments only grow while a call streams, so a parse of a text that
    extends `text` resumes after its last closed value.
    """

    __slots__ = ("text", "params", "outside")

    def __init__(self) -> None:
        self.text = ""  # raw text up to the end of the last closed value
        self.params: dict[str, object] = {}
        self.outside = ""  # text of ``self.text`` outside the closed pairs

_closed_args(text, pos)

Yield (start, key, value, end) for each closed argument from pos.

Source code in vllm/parser/glm47_moe.py
def _closed_args(text: str, pos: int) -> Iterator[tuple[int, str, str, int]]:
    """Yield `(start, key, value, end)` for each closed argument from `pos`."""
    while (arg := _find_arg(text, pos)) is not None:
        key_start, key_end, value_start = arg
        value_end = text.find(ARG_VALUE_END, value_start)
        if value_end < 0:
            return
        pos = value_end + len(ARG_VALUE_END)
        yield (
            key_start - len(ARG_KEY_START),
            text[key_start:key_end],
            text[value_start:value_end],
            pos,
        )

_find_arg(text, pos)

Locate the first argument whose key starts at or after pos.

Returns the start and end of the key and the start of the value. The key runs to the first </arg_key> that is followed by <arg_value>. When the first <arg_key> has no such end, no later one has either, so each call scans the text once.

Source code in vllm/parser/glm47_moe.py
def _find_arg(text: str, pos: int) -> tuple[int, int, int] | None:
    """Locate the first argument whose key starts at or after `pos`.

    Returns the start and end of the key and the start of the value. The key
    runs to the first `</arg_key>` that is followed by `<arg_value>`. When
    the first `<arg_key>` has no such end, no later one has either, so each
    call scans the text once.
    """
    start = text.find(ARG_KEY_START, pos)
    if start < 0:
        return None
    key_start = start + len(ARG_KEY_START)
    key_end = text.find(ARG_KEY_END, key_start)
    while key_end >= 0:
        value_start = _VALUE_START_RE.match(text, key_end + len(ARG_KEY_END))
        if value_start is not None:
            return key_start, key_end, value_start.end()
        key_end = text.find(ARG_KEY_END, key_end + 1)
    return None

_glm47_parse_args(raw_args, partial, closed=None)

Return the arguments and the key of the open trailing value, if any.

Source code in vllm/parser/glm47_moe.py
def _glm47_parse_args(
    raw_args: str, partial: bool, closed: _ClosedArgs | None = None
) -> tuple[dict[str, object], str | None]:
    """Return the arguments and the key of the open trailing value, if any."""
    params: dict[str, object] = {}
    outside: list[str] = []
    pos = 0
    if closed is not None and raw_args.startswith(closed.text):
        params.update(closed.params)
        outside.append(closed.outside)
        pos = len(closed.text)
    start = pos

    for arg_start, key, value, end in _closed_args(raw_args, pos):
        outside.append(raw_args[pos:arg_start])
        params[key.strip()] = value
        pos = end

    if closed is not None and pos > start:
        closed.text = raw_args[:pos]
        closed.params = dict(params)
        closed.outside = "".join(outside)

    open_key = None
    if partial:
        remaining = "".join(outside) + raw_args[pos:]
        arg = _find_arg(remaining, 0)
        if arg is not None:
            key_start, key_end, value_start = arg
            key = remaining[key_start:key_end].strip()
            if key:
                params[key] = remaining[value_start:]
                open_key = key

    return params, open_key