Module qwen_xml

Module qwen_xml 

Source
Expand description

Qwen-style XML tool-call parser.

Rewrites embedded <tool_call>{json}</tool_call> markup — emitted by Qwen 2 / 2.5 / 3 family models in either the text or reasoning channel — into proper ToolCall values. Bytes outside of <tool_call> regions are forwarded verbatim on the channel they arrived on.

§Chunk safety

Both the open marker (<tool_call>, 11 bytes) and the close marker (</tool_call>, 12 bytes) may straddle SSE chunk boundaries. The parser holds back at most CLOSE_MARKER.len() - 1 = 11 bytes per channel as a lookahead buffer. The buffered bytes are flushed on the next push or at ToolCallParser::finish.

§Cross-channel handling

In practice a Qwen tool call appears entirely on one channel — either text (no reasoning split) or reasoning (when --reasoning-format is on). Each channel therefore maintains its own independent parser state (ChannelState) so that markup never crosses channels. The synthesised tool-call IDs share a single monotonic counter across both channels.

Structs§

ChannelState 🔒
Per-channel scanning state. The text and reasoning channels each own one of these; they never share buffers.
QwenXmlParser
Parser for the Qwen XML tool-call dialect. See module docs.

Enums§

Channel 🔒
Output channel selector — keeps scan channel-agnostic.

Constants§

CLOSE 🔒
Close marker for a Qwen tool call.
OPEN 🔒
Open marker for a Qwen tool call.

Functions§

finalize_tool_call 🔒
Parse the accumulated tool-call body and push the resulting ToolCall (or a NormalizationError) onto out.
forward 🔒
Append bytes to the channel-appropriate field of out.
parse_function_xml_body 🔒
Try to interpret body as the Hermes/Qwen3 inner-XML tool call: <function=NAME><parameter=KEY>VALUE</parameter>...</function>.
parse_json_body 🔒
Try to interpret body as a Qwen JSON tool call.
parse_param_value 🔒
Best-effort coercion of a <parameter> body to a JSON value. Falls back to a string literal when the body is not valid JSON.
partial_suffix_len 🔒
Largest n in [0, marker.len()) such that the last n bytes of buf are a prefix of marker. Used as the lookahead window for chunk-safe marker detection.