vllm_mlx.tool_parsers.harmony_tool_parser¶
Harmony tool call parser for GPT-OSS models.
View the complete module source at #L1-L253.
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.harmony_tool_parser
¶
Harmony tool call parser for GPT-OSS models.
Harmony uses control tokens and channels for tool calling:
<|channel|>commentary to=functions.get_weather
<|constrain|>json
<|message|>{"location": "San Francisco"}
<|call|>
The final response is in the 'final' channel:
<|channel|>final
<|message|>The weather is 72F.
<|return|>
vllm_mlx.tool_parsers.harmony_tool_parser._COMMENTARY_BLOCK_PATTERN
module-attribute
¶
_COMMENTARY_BLOCK_PATTERN = re.compile('<\\|channel\\|>commentary\\s+to=functions\\.(\\w+)(?:\\s*<\\|constrain\\|>\\w+)?\\s*<\\|message\\|>((?:(?!<\\|channel\\|>).)*?)(?P<terminator><\\|call\\|>|<\\|end\\|>|<\\|return\\|>|<\\|start\\|>|<\\|channel\\|>|\\Z)', re.DOTALL)
vllm_mlx.tool_parsers.harmony_tool_parser._FINAL_BLOCK_PATTERN
module-attribute
¶
_FINAL_BLOCK_PATTERN = re.compile('<\\|channel\\|>final\\s*<\\|message\\|>(.*?)<\\|return\\|>', re.DOTALL)
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser
¶
Bases: ToolParser
Tool call parser for GPT-OSS models using Harmony format.
Harmony uses control tokens and 3 channels: - analysis: internal reasoning (handled by reasoning parser) - commentary: tool calls addressed with to=functions.{name} - final: user-facing response
Used when --enable-auto-tool-choice --tool-call-parser harmony are set.
Source code in vllm_mlx/tool_parsers/abstract_tool_parser.py
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser.SUPPORTS_NATIVE_TOOL_FORMAT
class-attribute
instance-attribute
¶
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser.extract_tool_calls
¶
extract_tool_calls(model_output: str, request: dict[str, Any] | None = None) -> ExtractedToolCallInformation
Extract tool calls from a complete Harmony model response.
Parses commentary channel blocks for tool calls and the final channel for the user-facing content.
Source code in vllm_mlx/tool_parsers/harmony_tool_parser.py
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser.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 Harmony model output.
A commentary block completes when an explicit terminator arrives (<|call|>, <|end|>, <|return|>, <|start|>) or when the model moves on to the <|channel|>final block; the completed call is emitted once (deduplicated by name + arguments). Final-channel content is emitted as regular content deltas and plain text passes through unchanged.
Source code in vllm_mlx/tool_parsers/harmony_tool_parser.py
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser.reset
¶
vllm_mlx.tool_parsers.harmony_tool_parser._generate_tool_id
¶
vllm_mlx.tool_parsers.harmony_tool_parser._strip_control_tokens
¶
Remove Harmony control tokens from text.
Source code in vllm_mlx/tool_parsers/harmony_tool_parser.py
vllm_mlx.tool_parsers.harmony_tool_parser._is_control_token
¶
Check if text is a Harmony control token.
Source code in vllm_mlx/tool_parsers/harmony_tool_parser.py
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.harmony_tool_parser._generate_tool_id · function
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.
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser · class
Tool call parser for GPT-OSS models using Harmony format.
Parameters
This callable has no explicit inputs.
Returns
- Constructs:
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser
Exceptions and behavior
Class HarmonyToolParser derives from ToolParser and declares 3 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser.extract_tool_calls · method
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser.extract_tool_calls(model_output: str, request: dict[str, Any] | None = None) -> ExtractedToolCallInformation
Extract tool calls from a complete Harmony 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=content);ExtractedToolCallInformation(tools_called=False, tool_calls=[], content=content)
Exceptions and behavior
Method HarmonyToolParser.extract_tool_calls calls _COMMENTARY_BLOCK_PATTERN.finditer, match.group, match.group(2).strip, json.loads; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser.extract_tool_calls_streaming · method
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser.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 Harmony 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:
{'content': delta_text};{'tool_calls': emitted};None
Exceptions and behavior
Method HarmonyToolParser.extract_tool_calls_streaming updates self._emitted_streaming_signatures; calls hasattr, set, any, self.extract_tool_calls; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.harmony_tool_parser.HarmonyToolParser.reset · method
Reset parser state for a new request.
Parameters
This callable has no explicit inputs.
Returns
- Type:
None
Exceptions and behavior
Method HarmonyToolParser.reset updates self._emitted_streaming_signatures; calls super().reset, super, set.
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.harmony_tool_parser._strip_control_tokens · function
Remove Harmony control tokens from text.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str - Direct return expressions:
result.strip()
Exceptions and behavior
Function _strip_control_tokens calls result.replace, re.sub, result.strip; returns result.strip().
No direct raise statement appears in this definition.
vllm_mlx.tool_parsers.harmony_tool_parser._is_control_token · function
Check if text is a Harmony control token.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
text |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
bool - Direct return expressions:
text.strip() in {'<|start|>', '<|end|>', '<|message|>', '<|channel|>', '<|constrain|>', '<|return|>', '<|call|>'}
Exceptions and behavior
Function _is_control_token calls text.strip; returns text.strip() in {'<|start|>', '<|end|>', '<|message|>', '<|channel|>', '<|constrain|>', '<|return|>', '<|call|>'}.
No direct raise statement appears in this definition.
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. | #L32-L34 |
HarmonyToolParser |
class | HarmonyToolParser() |
Tool call parser for GPT-OSS models using Harmony format. | #L57-L219 |
HarmonyToolParser.extract_tool_calls |
method | HarmonyToolParser.extract_tool_calls(model_output: str, request: dict[str, Any] \| None = None) -> ExtractedToolCallInformation |
Extract tool calls from a complete Harmony model response. | #L71-L140 |
HarmonyToolParser.extract_tool_calls_streaming |
method | HarmonyToolParser.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 Harmony model output. | #L142-L214 |
HarmonyToolParser.reset |
method | HarmonyToolParser.reset() -> None |
Reset parser state for a new request. | #L216-L219 |
_strip_control_tokens |
function | _strip_control_tokens(text: str) -> str |
Remove Harmony control tokens from text. | #L222-L240 |
_is_control_token |
function | _is_control_token(text: str) -> bool |
Check if text is a Harmony control token. | #L243-L253 |