vllm_mlx.utils.harmony_render¶
Harmony-format prompt rendering for GPT-OSS via openai-harmony.
View the complete module source at #L1-L303.
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.utils.harmony_render
¶
Harmony-format prompt rendering for GPT-OSS via openai-harmony.
GPT-OSS models are trained with OpenAI's harmony wire format (channeled
<|start|>assistant<|channel|>commentary ...<|call|> tool calls,
<|start|>functions.X to=assistant<|channel|>commentary<|message|>...
tool results, etc.). Rendering harmony correctly from OpenAI-style chat
messages is delicate: prior assistant tool_calls must arrive at the
template as structural objects, not the bracket-text fallback that
api.utils.extract_multimodal_content() produces for non-native parsers.
This module bypasses the Jinja chat template entirely for harmony-active
engines: it converts the OpenAI-format messages (plus tools) to an
openai_harmony.Conversation and asks the library — the canonical
renderer maintained by OpenAI — to serialize it. That sidesteps both the
text-flattening upstream and any template-vs-training-format drift.
The library is an optional dependency. HAS_HARMONY reflects import
success so the rest of the engine can fall back to apply_chat_template
when the package is absent.
See https://github.com/waybarrios/vllm-mlx/issues/568 for the original report and the patch shape Thump604 outlined.
vllm_mlx.utils.harmony_render._harmony_encoding
cached
¶
Load the harmony encoding once and reuse it across requests.
load_harmony_encoding reads the harmony tokenizer assets, so calling
it on every render_messages invocation would add latency to the
per-request prompt build. Callers reach this only after the HAS_HARMONY
guard in render_messages, so _oh is never None here.
Source code in vllm_mlx/utils/harmony_render.py
vllm_mlx.utils.harmony_render.is_harmony_parser_name
¶
Return True when the active --tool-call-parser is a harmony alias.
HarmonyToolParser registers under both "harmony" and "gpt-oss".
Source code in vllm_mlx/utils/harmony_render.py
vllm_mlx.utils.harmony_render._build_tools
¶
Source code in vllm_mlx/utils/harmony_render.py
vllm_mlx.utils.harmony_render._content_to_text
¶
Flatten OpenAI content (str | list[dict]) to plain text.
Source code in vllm_mlx/utils/harmony_render.py
vllm_mlx.utils.harmony_render._convert_message
¶
Convert one OpenAI-format message to one or more openai_harmony.Message.
A single assistant turn can carry multiple tool_calls; harmony represents
each as its own commentary-channel message addressed to functions.X.
Prior reasoning lives in an analysis-channel message that precedes the
tool calls.
Source code in vllm_mlx/utils/harmony_render.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
vllm_mlx.utils.harmony_render._resolve_tool_names
¶
Stamp name=functions.X on each role=tool message by tracing back
the most recent assistant tool_call_id -> function name.
Source code in vllm_mlx/utils/harmony_render.py
vllm_mlx.utils.harmony_render.render_messages
¶
render_messages(messages: list[dict], tools: list[dict] | None = None, reasoning_effort: str | None = None) -> str
Render OpenAI-format messages as a harmony-format prompt string.
Raises RuntimeError if openai-harmony is not importable; callers
should pre-check with :data:HAS_HARMONY and fall back to
tokenizer.apply_chat_template when False.
Parameters:
-
messages(list[dict]) –OpenAI chat-completions messages.
-
tools(list[dict] | None, default:None) –OpenAI-format tools list (each item
{"type":"function","function":{...}}). -
reasoning_effort(str | None, default:None) –"low","medium", or"high". Defaults to medium.
Returns:
-
str–Decoded harmony prompt with the trailing
<|start|>assistant -
str–marker ready for the model to begin generation.
Source code in vllm_mlx/utils/harmony_render.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
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.utils.harmony_render._harmony_encoding · function
Load the harmony encoding once and reuse it across requests.
Parameters
This callable has no explicit inputs.
Returns
- Type:
Any - Direct return expressions:
_oh.load_harmony_encoding(_oh.HarmonyEncodingName.HARMONY_GPT_OSS)
Exceptions and behavior
Function _harmony_encoding calls _oh.load_harmony_encoding; returns _oh.load_harmony_encoding(_oh.HarmonyEncodingName.HARMONY_GPT_OSS).
No direct raise statement appears in this definition.
vllm_mlx.utils.harmony_render.is_harmony_parser_name · function
Return True when the active --tool-call-parser is a harmony alias.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
parser_name |
str \| None |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
bool - Direct return expressions:
parser_name in {'harmony', 'gpt-oss'}
Exceptions and behavior
Function is_harmony_parser_name returns parser_name in {'harmony', 'gpt-oss'}.
No direct raise statement appears in this definition.
vllm_mlx.utils.harmony_render._build_tools · function
Function _build_tools calls t.get, fn.get, tool_descs.append, _oh.ToolDescription.new; has 2 explicit return paths.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
tools |
list[dict] \| None |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
list[Any] | None - Direct return expressions:
None;tool_descs or None
Exceptions and behavior
Function _build_tools calls t.get, fn.get, tool_descs.append, _oh.ToolDescription.new; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.utils.harmony_render._content_to_text · function
Flatten OpenAI content (str | list[dict]) to plain text.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
content |
Any |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str - Direct return expressions:
'';content;'\n'.join(parts);str(content)
Exceptions and behavior
Function _content_to_text calls isinstance, item.get, parts.append, '\n'.join; has 4 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.utils.harmony_render._convert_message · function
Convert one OpenAI-format message to one or more openai_harmony.Message.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
msg |
dict |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
list[Any] - Direct return expressions:
[];out
Exceptions and behavior
Function _convert_message calls msg.get, _content_to_text, out.append, _oh.Message.from_role_and_content; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.utils.harmony_render._resolve_tool_names · function
Stamp name=functions.X on each role=tool message by tracing back the most recent assistant tool_call_id -> function name.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
messages |
list[dict] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
list[dict] - Direct return expressions:
out
Exceptions and behavior
Function _resolve_tool_names calls isinstance, out.append, m.get, tc.get; returns out.
No direct raise statement appears in this definition.
vllm_mlx.utils.harmony_render.render_messages · function
vllm_mlx.utils.harmony_render.render_messages(messages: list[dict], tools: list[dict] | None = None, reasoning_effort: str | None = None) -> str
Render OpenAI-format messages as a harmony-format prompt string.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
messages |
list[dict] |
yes |
none |
OpenAI chat-completions messages. |
tools |
list[dict] \| None |
no |
None |
OpenAI-format tools list (each item {"type":"function","function":{...}}). |
reasoning_effort |
str \| None |
no |
None |
"low", "medium", or "high". Defaults to medium. |
Returns
- Type:
str - Direct return expressions:
enc.decode(token_ids)
Exceptions and behavior
Function render_messages calls RuntimeError, _resolve_tool_names, isinstance, other_msgs.append; can raise RuntimeError; returns enc.decode(token_ids).
Directly raised exceptions: RuntimeError.
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 |
|---|---|---|---|---|
_harmony_encoding |
function | _harmony_encoding() -> Any |
Load the harmony encoding once and reuse it across requests. | #L45-L53 |
is_harmony_parser_name |
function | is_harmony_parser_name(parser_name: str \| None) -> bool |
Return True when the active --tool-call-parser is a harmony alias. | #L56-L61 |
_build_tools |
function | _build_tools(tools: list[dict] \| None) -> list[Any] \| None |
Function _build_tools calls t.get, fn.get, tool_descs.append, _oh.ToolDescription.new; has 2 explicit return paths. |
#L64-L80 |
_content_to_text |
function | _content_to_text(content: Any) -> str |
Flatten OpenAI content (str | list[dict]) to plain text. | #L83-L97 |
_convert_message |
function | _convert_message(msg: dict) -> list[Any] |
Convert one OpenAI-format message to one or more openai_harmony.Message. |
#L100-L182 |
_resolve_tool_names |
function | _resolve_tool_names(messages: list[dict]) -> list[dict] |
Stamp name=functions.X on each role=tool message by tracing back the most recent assistant tool_call_id -> function name. |
#L185-L214 |
render_messages |
function | render_messages(messages: list[dict], tools: list[dict] \| None = None, reasoning_effort: str \| None = None) -> str |
Render OpenAI-format messages as a harmony-format prompt string. | #L217-L303 |