Skip to content

vllm.entrypoints.openai.parser.harmony_utils

Functions:

_use_legacy_tool_call_headers(token_ids)

Keep prior assistant tool calls in the format expected by GPT-OSS.

oss-harmony 0.0.10 changed a rendered tool-call header from::

assistant to=python<|channel|>commentary code

to::

assistant<|channel|>commentary to=python <|constrain|>code

GPT-OSS can echo the latter as a malformed header with the recipient in both positions. Rewrite only that unambiguous token-level header shape, while retaining oss-harmony's embedded vocabulary and parser.

auto_drop_analysis_messages(msgs)

Harmony models expect the analysis messages (representing raw chain of thought) to be dropped after an assistant message to the final channel is produced from the reasoning of those messages.

The openai-harmony library does this if the very last assistant message is to the final channel, but it does not handle the case where we're in longer multi-turn conversations and the client gave us reasoning content from previous turns of the conversation with multiple assistant messages to the final channel in the conversation.

So, we find the index of the last assistant message to the final channel and drop all analysis messages that precede it, leaving only the analysis messages that are relevant to the current part of the conversation.

build_harmony_preamble(*, instructions=None, tools=None, reasoning_effort=None, browser_description=None, python_description=None, container_description=None, with_custom_tools=False)

Build the standard Harmony system/developer prefix for a request.

extract_instructions_from_messages(messages)

Peel a leading system/developer Chat Completion or Responses message and flatten its instruction text.

flatten_input_text_content(content)

Extract text parts from a Chat Completion or Responses API content field and flatten them into a single string. Returns None if no text content is found.

has_custom_tools(tool_types)

Checks if the given tool types are custom tools (i.e. any tool other than MCP builtin tools)

is_function_recipient(recipient, allowed_function_tool_names=None)

Check whether recipient refers to a function tool call.

The optional allowed_function_tool_names parameter is used by the Responses API to distinguish bare function-call recipients (missing the functions. prefix) from MCP tool calls. When provided, a bare recipient is only treated as a function call if it appears in the set. The Chat Completions path omits this parameter so that all bare recipients are accepted as function calls (the heuristic fallback).

parse_chat_input_to_harmony_message(chat_msg, tool_id_names=None)

Parse a message from request.messages in the Chat Completion API to Harmony messages.

parse_chat_inputs_to_harmony_messages(chat_msgs)

Parse a list of messages from request.messages in the Chat Completion API to Harmony messages.