diff --git a/tests/entrypoints/openai/responses/test_parsable_context_unit.py b/tests/entrypoints/openai/responses/test_parsable_context_unit.py index 0398723184..710cd2d04c 100644 --- a/tests/entrypoints/openai/responses/test_parsable_context_unit.py +++ b/tests/entrypoints/openai/responses/test_parsable_context_unit.py @@ -6,10 +6,13 @@ These tests verify that ParsableContext correctly delegates to the unified Parser (via parse) and properly builds response output items. """ +import json from collections.abc import Sequence -from unittest.mock import MagicMock +from types import SimpleNamespace +from unittest.mock import AsyncMock, MagicMock import pytest +from openai.types.responses import ResponseFunctionToolCall from vllm.entrypoints.generate.base.protocol import ( DeltaMessage, @@ -17,6 +20,7 @@ from vllm.entrypoints.generate.base.protocol import ( FunctionCall, ToolCall, ) +from vllm.entrypoints.mcp.tool import HarmonyPythonTool from vllm.entrypoints.openai.responses.context import ParsableContext from vllm.entrypoints.openai.responses.protocol import ResponsesRequest from vllm.outputs import CompletionOutput, RequestOutput @@ -317,6 +321,65 @@ def test_process_extracts_tool_calls(): 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 # --------------------------------------------------------------------------- diff --git a/vllm/entrypoints/mcp/tool.py b/vllm/entrypoints/mcp/tool.py index d1f9130bdf..935f7f784d 100644 --- a/vllm/entrypoints/mcp/tool.py +++ b/vllm/entrypoints/mcp/tool.py @@ -173,7 +173,7 @@ class HarmonyPythonTool(Tool): processed = ResponseFunctionToolCallOutputItem( id=f"fco_{random_uuid()}", type="function_call_output", - call_id=f"call_{random_uuid()}", + call_id=last_msg.call_id, output=msg.content[0].text, status="completed", ) diff --git a/vllm/entrypoints/openai/responses/context.py b/vllm/entrypoints/openai/responses/context.py index 726a955a55..1a218f1692 100644 --- a/vllm/entrypoints/openai/responses/context.py +++ b/vllm/entrypoints/openai/responses/context.py @@ -24,7 +24,6 @@ from vllm import envs from vllm.entrypoints.chat_utils import ( ChatTemplateContentFormatOption, ) -from vllm.entrypoints.generate.base.protocol import FunctionCall from vllm.entrypoints.mcp.tool import Tool from vllm.entrypoints.mcp.tool_server import ToolServer from vllm.entrypoints.openai.parser.harmony_utils import render_for_completion @@ -440,7 +439,9 @@ class ParsableContext(ConversationContext): return False 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]: self.called_tools.add("python") if isinstance(tool_session, Tool): @@ -455,7 +456,7 @@ class ParsableContext(ConversationContext): message = ResponseFunctionToolCallOutputItem( id=f"mcpo_{random_uuid()}", type="function_call_output", - call_id=f"call_{random_uuid()}", + call_id=last_msg.call_id, output=result_str, status="completed", ) @@ -463,7 +464,9 @@ class ParsableContext(ConversationContext): return [message] 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]: self.called_tools.add("browser") if isinstance(tool_session, Tool): @@ -481,7 +484,7 @@ class ParsableContext(ConversationContext): message = ResponseFunctionToolCallOutputItem( id=f"fco_{random_uuid()}", type="function_call_output", - call_id=f"call_{random_uuid()}", + call_id=last_msg.call_id, output=result_str, status="completed", ) @@ -489,8 +492,10 @@ class ParsableContext(ConversationContext): return [message] async def call_container_tool( - self, tool_session: Union["ClientSession", Tool], last_msg: Message - ) -> list[Message]: + self, + tool_session: Union["ClientSession", Tool], + last_msg: ResponseFunctionToolCall, + ) -> list[ResponseInputOutputItem]: """Call container tool. Expect this to be run in a stateful docker with command line terminal. The official container tool would at least @@ -523,7 +528,7 @@ class ParsableContext(ConversationContext): message = ResponseFunctionToolCallOutputItem( id=f"fco_{random_uuid()}", type="function_call_output", - call_id=f"call_{random_uuid()}", + call_id=last_msg.call_id, output=result_str, status="completed", )