Skip to content

vllm.parser.muse

Muse Spark parser: recipient-routed messages parsed by one state machine.

Muse Spark writes an assistant turn as a sequence of messages::

to=self<|message|>...reasoning...<|eom|><|start|>assistant
to=get_weather<|message|><atem:function_calls>
<atem:invoke name="get_weather">
<atem:parameter name="city">Paris</atem:parameter>
</atem:invoke>
</atem:function_calls><|eom|><|start|>assistant
to=user<|message|>...final answer...<|eot|>

The recipient before <|message|> routes each body: self or none is reasoning, user and commentary are content, developer is dropped, and any other recipient is a tool. The model may write to=NAME<|message|> right after a body without <|eom|>, so a trailing to=NAME in a body is held back.

Classes:

MuseParser

Bases: ParserEngine

Methods:

Source code in vllm/parser/muse.py
class MuseParser(ParserEngine):
    _streaming_engine_cls = MuseStreamingParserEngine

    def __init__(
        self,
        tokenizer: TokenizerLike,
        tools: list[Tool] | None = None,
        **kwargs,
    ) -> None:
        kwargs.setdefault("parser_engine_config", muse_config())
        super().__init__(tokenizer, tools, **kwargs)
        self._has_reasoning = True
        self._reasoning_ended = False
        self._arg_converter = self._convert_args
        self._parameter_types = ParameterTypesCache()
        vocab = self.vocab
        self._message_ids = (vocab.get(MESSAGE),)
        self._start_id = vocab.get(START)
        self._boundary_ids = tuple(
            vocab.get(token) for token in (START, MESSAGE, EOM, EOT)
        )

    def is_reasoning_end(self, input_ids: Sequence[int]) -> bool:
        pos = len(input_ids)
        while (pos := rfind_tokens(input_ids, self._message_ids, stop=pos)) >= 0:
            ended = self._header_ends_reasoning(input_ids, pos)
            if ended is not None:
                return ended
        return False

    def is_reasoning_end_streaming(
        self, input_ids: Sequence[int], delta_ids: Iterable[int]
    ) -> bool:
        pos = len(input_ids)
        start = max(pos - len(list(delta_ids)), 0)
        while (pos := rfind_tokens(input_ids, self._message_ids, start, pos)) >= 0:
            if self._header_ends_reasoning(input_ids, pos):
                return True
        return False

    def _header_ends_reasoning(self, input_ids: Sequence[int], pos: int) -> bool | None:
        """True for a tool message, False for another role's message, else None."""
        start = max(pos - _MAX_HEADER_TOKENS, 0)
        boundary = rfind_tokens(input_ids, self._boundary_ids, start, pos)
        begin = boundary + 1 if boundary >= 0 else start
        header = self.model_tokenizer.decode(list(input_ids[begin:pos]))
        if (
            boundary >= 0
            and input_ids[boundary] == self._start_id
            and not header.startswith("assistant")
        ):
            return False
        return True if _route_header(header)[0] == "TOOL_HEADER" else None

    def extract_content_ids(self, input_ids: list[int]) -> list[int]:
        return []

    def count_reasoning_tokens(self, token_ids: Sequence[int]) -> int:
        """Count reasoning-body tokens from the ids alone.

        Counting streamed text events undercounts when the detokenizer holds
        text back (stop strings, partial UTF-8) while ids advance.
        """
        count = 0
        header_start = 0
        in_reasoning = False
        for i, token_id in enumerate(token_ids):
            if token_id in self._message_ids:
                header = self.model_tokenizer.decode(list(token_ids[header_start:i]))
                in_reasoning = _route_header(header)[0] == "REASONING_HEADER"
                header_start = i + 1
            elif token_id in self._boundary_ids:
                in_reasoning = False
                header_start = i + 1
            elif in_reasoning:
                count += 1
        return count

    def _handle_tool_name(self, event: SemanticEvent) -> None:
        self._tool_slots[event.tool_index].name += self._registered_name(event.value)

    def _registered_name(self, name: str) -> str:
        """Map `NAME.<suffix>` back to a registered bare `NAME`.

        The chat template renders a bare tool as `"NAME.*"`, so the model may
        fill in any suffix (`Bash.Bash`, `Bash.command`, `Glob.`). The doubled
        form always collapses; other suffixes collapse only when no registered
        name lives under `NAME.`, where the suffix may name a missing sibling.
        """
        if not self._tools or find_tool_name(self._tools, name):
            return name
        head, sep, tail = name.partition(".")
        if not sep or not find_tool_name(self._tools, head):
            return name
        names = (
            tool.function.name
            if isinstance(tool, ChatCompletionToolsParam)
            else tool.name
            for tool in get_function_tools(self._tools)
        )
        if head != tail and any(n.startswith(f"{head}.") for n in names):
            return name
        return head

    def _convert_args(self, raw_args: str, partial: bool) -> str:
        match = _INVOKE_RE.match(raw_args)
        if match is None:
            return "{}"
        properties, extra = self._parameter_types.for_tools(self._tools).get(
            self._registered_name(match.group(1)), ({}, None)
        )
        arguments = {
            key: convert_value(raw, properties.get(key, extra), _decode_value)
            for key, raw in _PARAMETER_RE.findall(match.group(2))
        }
        return json.dumps(arguments, ensure_ascii=False)

_header_ends_reasoning(input_ids, pos)

True for a tool message, False for another role's message, else None.

Source code in vllm/parser/muse.py
def _header_ends_reasoning(self, input_ids: Sequence[int], pos: int) -> bool | None:
    """True for a tool message, False for another role's message, else None."""
    start = max(pos - _MAX_HEADER_TOKENS, 0)
    boundary = rfind_tokens(input_ids, self._boundary_ids, start, pos)
    begin = boundary + 1 if boundary >= 0 else start
    header = self.model_tokenizer.decode(list(input_ids[begin:pos]))
    if (
        boundary >= 0
        and input_ids[boundary] == self._start_id
        and not header.startswith("assistant")
    ):
        return False
    return True if _route_header(header)[0] == "TOOL_HEADER" else None

_registered_name(name)

Map NAME.<suffix> back to a registered bare NAME.

The chat template renders a bare tool as "NAME.*", so the model may fill in any suffix (Bash.Bash, Bash.command, Glob.). The doubled form always collapses; other suffixes collapse only when no registered name lives under NAME., where the suffix may name a missing sibling.

Source code in vllm/parser/muse.py
def _registered_name(self, name: str) -> str:
    """Map `NAME.<suffix>` back to a registered bare `NAME`.

    The chat template renders a bare tool as `"NAME.*"`, so the model may
    fill in any suffix (`Bash.Bash`, `Bash.command`, `Glob.`). The doubled
    form always collapses; other suffixes collapse only when no registered
    name lives under `NAME.`, where the suffix may name a missing sibling.
    """
    if not self._tools or find_tool_name(self._tools, name):
        return name
    head, sep, tail = name.partition(".")
    if not sep or not find_tool_name(self._tools, head):
        return name
    names = (
        tool.function.name
        if isinstance(tool, ChatCompletionToolsParam)
        else tool.name
        for tool in get_function_tools(self._tools)
    )
    if head != tail and any(n.startswith(f"{head}.") for n in names):
        return name
    return head

count_reasoning_tokens(token_ids)

Count reasoning-body tokens from the ids alone.

Counting streamed text events undercounts when the detokenizer holds text back (stop strings, partial UTF-8) while ids advance.

Source code in vllm/parser/muse.py
def count_reasoning_tokens(self, token_ids: Sequence[int]) -> int:
    """Count reasoning-body tokens from the ids alone.

    Counting streamed text events undercounts when the detokenizer holds
    text back (stop strings, partial UTF-8) while ids advance.
    """
    count = 0
    header_start = 0
    in_reasoning = False
    for i, token_id in enumerate(token_ids):
        if token_id in self._message_ids:
            header = self.model_tokenizer.decode(list(token_ids[header_start:i]))
            in_reasoning = _route_header(header)[0] == "REASONING_HEADER"
            header_start = i + 1
        elif token_id in self._boundary_ids:
            in_reasoning = False
            header_start = i + 1
        elif in_reasoning:
            count += 1
    return count