-
-
Notifications
You must be signed in to change notification settings - Fork 15k
Fix gpt‑oss Harmony token leaks in tool names and streaming content #32587 #32633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
baonudesifeizhai
wants to merge
5
commits into
vllm-project:main
from
baonudesifeizhai:fixgpttoolchain
+91
−32
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |||||
| from collections.abc import Iterable, Sequence | ||||||
| from typing import Literal | ||||||
|
|
||||||
| import regex as re | ||||||
| from openai.types.responses import ( | ||||||
| ResponseFunctionToolCall, | ||||||
| ResponseOutputItem, | ||||||
|
|
@@ -68,6 +69,30 @@ | |||||
| "container", | ||||||
| } | ||||||
|
|
||||||
| _HARMONY_CONTROL_TOKEN_RE = re.compile(r"<\|[^>]*?\|>") | ||||||
| _INVALID_TOOL_NAME = "__invalid_tool__" | ||||||
|
|
||||||
|
|
||||||
| def sanitize_harmony_tool_name(name: str | None) -> str: | ||||||
| if not name: | ||||||
| return "" | ||||||
| cleaned = name.strip() | ||||||
| if "<|" not in cleaned: | ||||||
| return cleaned | ||||||
| prefix = cleaned.split("<|", 1)[0].strip() | ||||||
| if prefix: | ||||||
| return prefix | ||||||
| cleaned = _HARMONY_CONTROL_TOKEN_RE.sub("", cleaned).strip() | ||||||
| return cleaned or _INVALID_TOOL_NAME | ||||||
|
|
||||||
|
|
||||||
| def strip_harmony_control_tokens(text: str | None) -> str | None: | ||||||
| if text is None: | ||||||
| return None | ||||||
| if "<|" not in text: | ||||||
| return text | ||||||
| return _HARMONY_CONTROL_TOKEN_RE.sub("", text) | ||||||
|
|
||||||
|
|
||||||
| def has_custom_tools(tool_types: set[str]) -> bool: | ||||||
| """ | ||||||
|
|
@@ -220,7 +245,8 @@ def parse_response_input( | |||||
| elif response_msg["type"] == "function_call": | ||||||
| msg = Message.from_role_and_content(Role.ASSISTANT, response_msg["arguments"]) | ||||||
| msg = msg.with_channel("commentary") | ||||||
| msg = msg.with_recipient(f"functions.{response_msg['name']}") | ||||||
| tool_name = sanitize_harmony_tool_name(response_msg.get("name")) | ||||||
| msg = msg.with_recipient(f"functions.{tool_name}") | ||||||
| msg = msg.with_content_type("json") | ||||||
| else: | ||||||
| raise ValueError(f"Unknown input type: {response_msg['type']}") | ||||||
|
|
@@ -238,9 +264,8 @@ def parse_chat_inputs_to_harmony_messages(chat_msgs: list) -> list[Message]: | |||||
| # Collect tool id to name mappings for tool response recipient values | ||||||
| for chat_msg in chat_msgs: | ||||||
| for tool_call in chat_msg.get("tool_calls", []): | ||||||
| tool_id_names[tool_call.get("id")] = tool_call.get("function", {}).get( | ||||||
| "name" | ||||||
| ) | ||||||
| raw_name = tool_call.get("function", {}).get("name") | ||||||
| tool_id_names[tool_call.get("id")] = sanitize_harmony_tool_name(raw_name) | ||||||
|
|
||||||
| for chat_msg in chat_msgs: | ||||||
| msgs.extend(parse_chat_input_to_harmony_message(chat_msg, tool_id_names)) | ||||||
|
|
@@ -333,7 +358,7 @@ def parse_chat_input_to_harmony_message( | |||||
|
|
||||||
| for call in tool_calls: | ||||||
| func = call.get("function", {}) | ||||||
| name = func.get("name", "") | ||||||
| name = sanitize_harmony_tool_name(func.get("name")) | ||||||
| arguments = func.get("arguments", "") or "" | ||||||
| msg = Message.from_role_and_content(Role.ASSISTANT, arguments) | ||||||
| msg = msg.with_channel("commentary") | ||||||
|
|
@@ -348,7 +373,7 @@ def parse_chat_input_to_harmony_message( | |||||
| # Tool role message (tool output) | ||||||
| if role == "tool": | ||||||
| tool_call_id = chat_msg.get("tool_call_id", "") | ||||||
| name = tool_id_names.get(tool_call_id, "") | ||||||
| name = sanitize_harmony_tool_name(tool_id_names.get(tool_call_id, "")) | ||||||
| content = chat_msg.get("content", "") or "" | ||||||
| content = flatten_chat_text_content(content) | ||||||
|
|
||||||
|
|
@@ -410,7 +435,7 @@ def parse_input_to_harmony_message(chat_msg) -> list[Message]: | |||||
| msgs: list[Message] = [] | ||||||
| for call in tool_calls: | ||||||
| func = call.get("function", {}) | ||||||
| name = func.get("name", "") | ||||||
| name = sanitize_harmony_tool_name(func.get("name")) | ||||||
| arguments = func.get("arguments", "") or "" | ||||||
| msg = Message.from_role_and_content(Role.ASSISTANT, arguments) | ||||||
| msg = msg.with_channel("commentary") | ||||||
|
|
@@ -421,7 +446,7 @@ def parse_input_to_harmony_message(chat_msg) -> list[Message]: | |||||
|
|
||||||
| # Tool role message (tool output) | ||||||
| if role == "tool": | ||||||
| name = chat_msg.get("name", "") | ||||||
| name = sanitize_harmony_tool_name(chat_msg.get("name")) | ||||||
| content = chat_msg.get("content", "") or "" | ||||||
| content = flatten_chat_text_content(content) | ||||||
|
|
||||||
|
|
@@ -530,7 +555,7 @@ def _parse_browser_tool_call(message: Message, recipient: str) -> ResponseOutput | |||||
|
|
||||||
| def _parse_function_call(message: Message, recipient: str) -> list[ResponseOutputItem]: | ||||||
| """Parse function calls into function tool call items.""" | ||||||
| function_name = recipient.split(".")[-1] | ||||||
| function_name = sanitize_harmony_tool_name(recipient.split(".")[-1]) | ||||||
| output_items = [] | ||||||
| for content in message.content: | ||||||
| random_id = random_uuid() | ||||||
|
|
@@ -554,7 +579,10 @@ def _parse_reasoning_content(message: Message) -> list[ResponseOutputItem]: | |||||
| summary=[], | ||||||
| type="reasoning", | ||||||
| content=[ | ||||||
| ResponseReasoningTextContent(text=content.text, type="reasoning_text") | ||||||
| ResponseReasoningTextContent( | ||||||
| text=strip_harmony_control_tokens(content.text), | ||||||
| type="reasoning_text", | ||||||
| ) | ||||||
| ], | ||||||
| status=None, | ||||||
| ) | ||||||
|
|
@@ -567,7 +595,7 @@ def _parse_final_message(message: Message) -> ResponseOutputItem: | |||||
| contents = [] | ||||||
| for content in message.content: | ||||||
| output_text = ResponseOutputText( | ||||||
| text=content.text, | ||||||
| text=strip_harmony_control_tokens(content.text), | ||||||
| annotations=[], # TODO | ||||||
| type="output_text", | ||||||
| logprobs=None, # TODO | ||||||
|
|
@@ -681,13 +709,14 @@ def parse_remaining_state(parser: StreamableParser) -> list[ResponseOutputItem]: | |||||
|
|
||||||
| if current_recipient and parser.current_channel in ("commentary", "analysis"): | ||||||
| if current_recipient.startswith("functions."): | ||||||
| function_name = sanitize_harmony_tool_name(current_recipient.split(".")[-1]) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to another comment, using
Suggested change
|
||||||
| rid = random_uuid() | ||||||
| return [ | ||||||
| ResponseFunctionToolCall( | ||||||
| arguments=parser.current_content, | ||||||
| call_id=f"call_{rid}", | ||||||
| type="function_call", | ||||||
| name=current_recipient.split(".")[-1], | ||||||
| name=function_name, | ||||||
| id=f"fc_{rid}", | ||||||
| status="in_progress", | ||||||
| ) | ||||||
|
|
@@ -720,7 +749,8 @@ def parse_remaining_state(parser: StreamableParser) -> list[ResponseOutputItem]: | |||||
| type="reasoning", | ||||||
| content=[ | ||||||
| ResponseReasoningTextContent( | ||||||
| text=parser.current_content, type="reasoning_text" | ||||||
| text=strip_harmony_control_tokens(parser.current_content), | ||||||
| type="reasoning_text", | ||||||
| ) | ||||||
| ], | ||||||
| status=None, | ||||||
|
|
@@ -735,7 +765,8 @@ def parse_remaining_state(parser: StreamableParser) -> list[ResponseOutputItem]: | |||||
| type="reasoning", | ||||||
| content=[ | ||||||
| ResponseReasoningTextContent( | ||||||
| text=parser.current_content, type="reasoning_text" | ||||||
| text=strip_harmony_control_tokens(parser.current_content), | ||||||
| type="reasoning_text", | ||||||
| ) | ||||||
| ], | ||||||
| status=None, | ||||||
|
|
@@ -744,7 +775,7 @@ def parse_remaining_state(parser: StreamableParser) -> list[ResponseOutputItem]: | |||||
|
|
||||||
| if parser.current_channel == "final": | ||||||
| output_text = ResponseOutputText( | ||||||
| text=parser.current_content, | ||||||
| text=strip_harmony_control_tokens(parser.current_content), | ||||||
| annotations=[], # TODO | ||||||
| type="output_text", | ||||||
| logprobs=None, # TODO | ||||||
|
|
@@ -814,6 +845,8 @@ def parse_chat_output( | |||||
| final_content: str | None = "\n".join(final_texts) | ||||||
|
|
||||||
| # Return None instead of empty string since existing callers check for None | ||||||
| reasoning = strip_harmony_control_tokens(reasoning) | ||||||
| final_content = strip_harmony_control_tokens(final_content) | ||||||
| reasoning = reasoning or None | ||||||
| final_content = final_content or None | ||||||
|
|
||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic to extract the function name using
recipient.split(".")[-1]can be incorrect if the function name itself contains dots. For consistency with other parts of the codebase (e.g.,serving.py), it's better to slice the string after thefunctions.prefix. This will correctly handle function names that might contain dots.