[Bugfix][Responses API] Preserve built-in tool output call IDs (#55596)

Signed-off-by: CorgiBoyG <111257566+CorgiBoyG@users.noreply.github.com>
Co-authored-by: Flora Feng <4florafeng@gmail.com>
This commit is contained in:
RongJie G
2026-09-29 22:45:14 +00:00
committed by GitHub
co-authored by Flora Feng
parent c37f86e572
commit 0755e69e75
3 changed files with 78 additions and 10 deletions
@@ -6,10 +6,13 @@ These tests verify that ParsableContext correctly delegates to the unified
Parser (via parse) and properly builds response output items. Parser (via parse) and properly builds response output items.
""" """
import json
from collections.abc import Sequence from collections.abc import Sequence
from unittest.mock import MagicMock from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest import pytest
from openai.types.responses import ResponseFunctionToolCall
from vllm.entrypoints.generate.base.protocol import ( from vllm.entrypoints.generate.base.protocol import (
DeltaMessage, DeltaMessage,
@@ -17,6 +20,7 @@ from vllm.entrypoints.generate.base.protocol import (
FunctionCall, FunctionCall,
ToolCall, ToolCall,
) )
from vllm.entrypoints.mcp.tool import HarmonyPythonTool
from vllm.entrypoints.openai.responses.context import ParsableContext from vllm.entrypoints.openai.responses.context import ParsableContext
from vllm.entrypoints.openai.responses.protocol import ResponsesRequest from vllm.entrypoints.openai.responses.protocol import ResponsesRequest
from vllm.outputs import CompletionOutput, RequestOutput from vllm.outputs import CompletionOutput, RequestOutput
@@ -317,6 +321,65 @@ def test_process_extracts_tool_calls():
assert tool_item.status == "completed" assert tool_item.status == "completed"
@pytest.mark.parametrize(
("tool_name", "session_name", "dispatched_name", "arguments"),
[
("code_interpreter", "python", "python", {"code": "print(42)"}),
("web_search_preview", "browser", "search", {"query": "vLLM"}),
("container.exec", "container", "exec", {"cmd": ["pwd"]}),
],
)
@pytest.mark.asyncio
async def test_builtin_tool_output_preserves_function_call_id(
tool_name, session_name, dispatched_name, arguments
):
"""Built-in tool outputs remain correlated with their originating call."""
tool_session = MagicMock()
tool_session.call_tool = AsyncMock(
return_value=SimpleNamespace(content=[SimpleNamespace(text="result")])
)
context = _make_context(None, available_tools=[session_name])
tool_call = ResponseFunctionToolCall(
id=f"fc_{session_name}",
call_id=f"call_{session_name}",
type="function_call",
name=tool_name,
arguments=json.dumps(arguments),
)
context.response_messages.append(tool_call)
context._tool_sessions[session_name] = tool_session
output = await context.call_tool()
tool_session.call_tool.assert_awaited_once_with(dispatched_name, arguments)
assert output[0].call_id == tool_call.call_id
@pytest.mark.asyncio
async def test_local_python_tool_output_preserves_function_call_id():
"""Local code-interpreter output remains correlated with its call."""
async def process(_):
yield SimpleNamespace(content=[SimpleNamespace(text="result")])
python_tool = object.__new__(HarmonyPythonTool)
python_tool.python_tool = MagicMock(process=process)
context = _make_context(None, available_tools=["python"])
tool_call = ResponseFunctionToolCall(
id="fc_python",
call_id="call_python",
type="function_call",
name="code_interpreter",
arguments='{"code": "print(42)"}',
)
context.response_messages.append(tool_call)
context._tool_sessions["python"] = python_tool
output = await context.call_tool()
assert output[0].call_id == tool_call.call_id
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Tests: finish_reason tracking # Tests: finish_reason tracking
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
+1 -1
View File
@@ -173,7 +173,7 @@ class HarmonyPythonTool(Tool):
processed = ResponseFunctionToolCallOutputItem( processed = ResponseFunctionToolCallOutputItem(
id=f"fco_{random_uuid()}", id=f"fco_{random_uuid()}",
type="function_call_output", type="function_call_output",
call_id=f"call_{random_uuid()}", call_id=last_msg.call_id,
output=msg.content[0].text, output=msg.content[0].text,
status="completed", status="completed",
) )
+13 -8
View File
@@ -24,7 +24,6 @@ from vllm import envs
from vllm.entrypoints.chat_utils import ( from vllm.entrypoints.chat_utils import (
ChatTemplateContentFormatOption, ChatTemplateContentFormatOption,
) )
from vllm.entrypoints.generate.base.protocol import FunctionCall
from vllm.entrypoints.mcp.tool import Tool from vllm.entrypoints.mcp.tool import Tool
from vllm.entrypoints.mcp.tool_server import ToolServer from vllm.entrypoints.mcp.tool_server import ToolServer
from vllm.entrypoints.openai.parser.harmony_utils import render_for_completion from vllm.entrypoints.openai.parser.harmony_utils import render_for_completion
@@ -440,7 +439,9 @@ class ParsableContext(ConversationContext):
return False return False
async def call_python_tool( async def call_python_tool(
self, tool_session: Union["ClientSession", Tool], last_msg: FunctionCall self,
tool_session: Union["ClientSession", Tool],
last_msg: ResponseFunctionToolCall,
) -> list[ResponseInputOutputItem]: ) -> list[ResponseInputOutputItem]:
self.called_tools.add("python") self.called_tools.add("python")
if isinstance(tool_session, Tool): if isinstance(tool_session, Tool):
@@ -455,7 +456,7 @@ class ParsableContext(ConversationContext):
message = ResponseFunctionToolCallOutputItem( message = ResponseFunctionToolCallOutputItem(
id=f"mcpo_{random_uuid()}", id=f"mcpo_{random_uuid()}",
type="function_call_output", type="function_call_output",
call_id=f"call_{random_uuid()}", call_id=last_msg.call_id,
output=result_str, output=result_str,
status="completed", status="completed",
) )
@@ -463,7 +464,9 @@ class ParsableContext(ConversationContext):
return [message] return [message]
async def call_search_tool( async def call_search_tool(
self, tool_session: Union["ClientSession", Tool], last_msg: FunctionCall self,
tool_session: Union["ClientSession", Tool],
last_msg: ResponseFunctionToolCall,
) -> list[ResponseInputOutputItem]: ) -> list[ResponseInputOutputItem]:
self.called_tools.add("browser") self.called_tools.add("browser")
if isinstance(tool_session, Tool): if isinstance(tool_session, Tool):
@@ -481,7 +484,7 @@ class ParsableContext(ConversationContext):
message = ResponseFunctionToolCallOutputItem( message = ResponseFunctionToolCallOutputItem(
id=f"fco_{random_uuid()}", id=f"fco_{random_uuid()}",
type="function_call_output", type="function_call_output",
call_id=f"call_{random_uuid()}", call_id=last_msg.call_id,
output=result_str, output=result_str,
status="completed", status="completed",
) )
@@ -489,8 +492,10 @@ class ParsableContext(ConversationContext):
return [message] return [message]
async def call_container_tool( async def call_container_tool(
self, tool_session: Union["ClientSession", Tool], last_msg: Message self,
) -> list[Message]: tool_session: Union["ClientSession", Tool],
last_msg: ResponseFunctionToolCall,
) -> list[ResponseInputOutputItem]:
"""Call container tool. Expect this to be run in a stateful docker """Call container tool. Expect this to be run in a stateful docker
with command line terminal. with command line terminal.
The official container tool would at least The official container tool would at least
@@ -523,7 +528,7 @@ class ParsableContext(ConversationContext):
message = ResponseFunctionToolCallOutputItem( message = ResponseFunctionToolCallOutputItem(
id=f"fco_{random_uuid()}", id=f"fco_{random_uuid()}",
type="function_call_output", type="function_call_output",
call_id=f"call_{random_uuid()}", call_id=last_msg.call_id,
output=result_str, output=result_str,
status="completed", status="completed",
) )