vllm.tool_parsers.kimi_k3_tool_parser
¶
Tool-call parser for the Kimi K3 (XTML) chat format.
This turns the generated XTML response and tools channels back into
OpenAI-compatible content and tool_calls.
K3 assistant tool calls live in a nested tools channel::
<|open|>tools<|sep|>
<|open|>call tool="python" index="1"<|sep|>
<|open|>argument key="code" type="string"<|sep|>print(1)<|close|>argument<|sep|>
<|open|>argument key="opts" type="object"<|sep|>{"a":1}<|close|>argument<|sep|>
<|close|>call<|sep|>
<|close|>tools<|sep|>
where <|open|>, <|close|>, <|sep|> are dedicated special tokens. The plain
reply lives in a sibling response channel which is unwrapped into content.
response always precedes tools (see the protocol channel grammar).
Argument decoding mirrors the template's type tagging (inverse encoding): * type="string" -> value is the RAW text, no unescaping (template emits it raw) * other types -> value is JSON-decoded (number/boolean/null/object/array)
Attribute values (tool=, index=, key=, type=) ARE escaped on the
encode side (& -> &, " -> "), so _attrs reverses
them on decode (" before & -- reverse of encode order).
Known limitation: because string argument and response bodies are emitted raw,
a value that literally contains <|close|>argument<|sep|> or
<|close|>response<|sep|> is indistinguishable from a real closing marker.
Classes:
KimiK3ToolParser
¶
Bases: ToolParser
_decode_call(attrs, body)
¶
Decode one call block into a ToolCall.
attrs is the text between <|open|>call and <|sep|> (carries
tool= / index=); body is the sequence of argument blocks.
Each argument is re-typed per its type= tag: strings pass through
raw, everything else is JSON-decoded (falling back to raw text if the
JSON is malformed, so a partial stream never raises). Returns None
when no tool name is present (an empty/garbage block is dropped).
_stream_tool_calls(text)
¶
Stream calls from the tools channel without waiting for call close.
String values are raw XTML text, so they can be JSON-escaped and sent as they arrive. Other values are JSON-decoded and normalized like the non-streaming path, so they are sent once their argument closes.
_strip_response_content(text)
¶
Strip XTML response/message markers from generated response text.
In chat serving, <|open|>response<|sep|> is often part of the prompt
generation prefix, so the model output may only contain the body plus
<|close|>response<|sep|>. Handle both that consumed-prefix shape and a
complete <|open|>response<|sep|>... <|close|>response<|sep|> wrapper.