Skip to content

Commit 484f054

Browse files
committed
disable tool server initialization if no tool in request
Summary: First part of fixing #25697 we can first fix this error ``` RuntimeError: Attempted to exit cancel scope in a different task than it was entered in ``` by not initializing the tool servers if the request doesn't need tools. Test Plan: - non stream request with tools still gets tools - stream without tools no longer outputs the error - stream with tools still works (but it still outputs the error) Differential Revision: D83387320 Signed-off-by: Andrew Xia <[email protected]>
1 parent 4778b42 commit 484f054

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

vllm/entrypoints/openai/serving_responses.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,19 @@ def _make_request_with_harmony(
442442

443443
return messages, [prompt_token_ids], [engine_prompt]
444444

445+
async def _initialize_tool_sessions(self, request: ResponsesRequest,
446+
context: ConversationContext,
447+
exit_stack: AsyncExitStack):
448+
# we should only initialize the tool session if the request needs tools
449+
if len(request.tools) == 0:
450+
return
451+
mcp_tools = {
452+
tool.server_label: tool
453+
for tool in request.tools if tool.type == "mcp"
454+
}
455+
await context.init_tool_sessions(self.tool_server, exit_stack,
456+
request.request_id, mcp_tools)
457+
445458
async def responses_full_generator(
446459
self,
447460
request: ResponsesRequest,
@@ -458,12 +471,8 @@ async def responses_full_generator(
458471

459472
async with AsyncExitStack() as exit_stack:
460473
try:
461-
mcp_tools = {
462-
tool.server_label: tool
463-
for tool in request.tools if tool.type == "mcp"
464-
}
465-
await context.init_tool_sessions(self.tool_server, exit_stack,
466-
request.request_id, mcp_tools)
474+
await self._initialize_tool_sessions(request, context,
475+
exit_stack)
467476
async for _ in result_generator:
468477
pass
469478
except asyncio.CancelledError:
@@ -1647,12 +1656,10 @@ def _increment_sequence_number_and_return(
16471656
async with AsyncExitStack() as exit_stack:
16481657
processer = None
16491658
if self.use_harmony:
1650-
mcp_tools = {
1651-
tool.server_label: tool
1652-
for tool in request.tools if tool.type == "mcp"
1653-
}
1654-
await context.init_tool_sessions(self.tool_server, exit_stack,
1655-
request.request_id, mcp_tools)
1659+
# TODO: in streaming, we noticed this bug:
1660+
# https://github.com/vllm-project/vllm/issues/25697
1661+
await self._initialize_tool_sessions(request, context,
1662+
exit_stack)
16561663
processer = self._process_harmony_streaming_events
16571664
else:
16581665
processer = self._process_simple_streaming_events

0 commit comments

Comments
 (0)