A high-throughput and memory-efficient inference and serving engine for LLMs
Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git
Synced Nov 15, 03:30 UTC
[Frontend] Expand tools even if tool_choice="none" (#17177)
Signed-off-by: okada shintarou <okada@preferred.jp>
Shintarou Okada committed
Jul 2, 2025 at 01:47 UTC
3d19d47d91b1d06a24a1bd7b6f0626a09cc18dce
5 files changed
+43
-6
docs/features/tool_calling.md
+8
@@ -99,6 +99,14 @@ vLLM supports the `tool_choice='required'` option in the chat completion API. Si
99
100
When tool_choice='required' is set, the model is guaranteed to generate one or more tool calls based on the specified tool list in the `tools` parameter. The number of tool calls depends on the user's query. The output format strictly follows the schema defined in the `tools` parameter.
101
102
+## None Function Calling
103
+
104
+vLLM supports the `tool_choice='none'` option in the chat completion API. When this option is set, the model will not generate any tool calls and will respond with regular text content only, even if tools are defined in the request.
105
+
106
+By default, when `tool_choice='none'` is specified, vLLM excludes tool definitions from the prompt to optimize context usage. To include tool definitions even with `tool_choice='none'`, use the `--expand-tools-even-if-tool-choice-none` option.
107
+
108
+Note: This behavior will change in v0.10.0, where tool definitions will be included by default even with `tool_choice='none'`.
109
+
110
## Automatic Function Calling
111
112
To enable this feature, you should set the following flags:
vllm/entrypoints/openai/api_server.py
+2
@@ -1280,6 +1280,8 @@ async def init_app_state(
1280
chat_template_content_format=args.chat_template_content_format,
1281
return_tokens_as_token_ids=args.return_tokens_as_token_ids,
1282
enable_auto_tools=args.enable_auto_tool_choice,
1283
+ expand_tools_even_if_tool_choice_none=args.
1284
+ expand_tools_even_if_tool_choice_none,
1285
tool_parser=args.tool_call_parser,
1286
reasoning_parser=args.reasoning_parser,
1287
enable_prompt_tokens_details=args.enable_prompt_tokens_details,
vllm/entrypoints/openai/cli_args.py
+11
@@ -223,6 +223,17 @@ def make_arg_parser(parser: FlexibleArgumentParser) -> FlexibleArgumentParser:
223
default=False,
224
help="Enable auto tool choice for supported models. Use "
225
"``--tool-call-parser`` to specify which parser to use.")
226
+ parser.add_argument(
227
+ "--expand-tools-even-if-tool-choice-none",
228
+ action="store_true",
229
+ default=False,
230
+ deprecated=True,
231
+ help="Include tool definitions in prompts "
232
+ "even when tool_choice='none'. "
233
+ "This is a transitional option that will be removed in v0.10.0. "
234
+ "In v0.10.0, tool definitions will always be included regardless of "
235
+ "tool_choice setting. Use this flag now to test the new behavior "
236
+ "before the breaking change.")
237
238
valid_tool_parsers = ToolParserManager.tool_parsers.keys()
239
parser.add_argument(
vllm/entrypoints/openai/protocol.py
+1
-3
@@ -686,10 +686,8 @@ class ChatCompletionRequest(OpenAIBaseModel):
686
if "tool_choice" not in data and data.get("tools"):
687
data["tool_choice"] = "auto"
688
689
- # if "tool_choice" is "none" -- ignore tools if present
689
+ # if "tool_choice" is "none" -- no validation is needed for tools
690
if "tool_choice" in data and data["tool_choice"] == "none":
691
- # ensure that no tools are present
692
- data.pop("tools", None)
691
return data
692
693
# if "tool_choice" is specified -- validation
vllm/entrypoints/openai/serving_chat.py
+21
-3
@@ -62,6 +62,7 @@ class OpenAIServingChat(OpenAIServing):
62
return_tokens_as_token_ids: bool = False,
63
reasoning_parser: str = "",
64
enable_auto_tools: bool = False,
65
+ expand_tools_even_if_tool_choice_none: bool = False,
66
tool_parser: Optional[str] = None,
67
enable_prompt_tokens_details: bool = False,
68
enable_force_include_usage: bool = False,
@@ -110,6 +111,8 @@ class OpenAIServingChat(OpenAIServing):
111
raise TypeError("Error: --enable-auto-tool-choice requires "
112
f"tool_parser:'{tool_parser}' which has not "
113
"been registered") from e
114
+ self.expand_tools_even_if_tool_choice_none = (
115
+ expand_tools_even_if_tool_choice_none)
116
117
self.enable_prompt_tokens_details = enable_prompt_tokens_details
118
self.enable_force_include_usage = enable_force_include_usage
@@ -175,9 +178,24 @@ class OpenAIServingChat(OpenAIServing):
178
"--enable-auto-tool-choice and --tool-call-parser to be set"
179
)
180
178
- tool_dicts = None if request.tools is None else [
179
- tool.model_dump() for tool in request.tools
180
- ]
181
+ if request.tools is None:
182
+ tool_dicts = None
183
+ elif (request.tool_choice == "none"
184
+ and not self.expand_tools_even_if_tool_choice_none):
185
+ if len(request.tools) > 0:
186
+ logger.warning_once(
187
+ "Tools are specified but tool_choice is set to 'none' "
188
+ "and --expand-tools-even-if-tool-choice-none is not "
189
+ "enabled. Tool definitions will be excluded from the "
190
+ "prompt. This behavior will change in vLLM v0.10 where "
191
+ "tool definitions will be included by default even "
192
+ "with tool_choice='none'. To adopt the new behavior "
193
+ "now, use --expand-tools-even-if-tool-choice-none. "
194
+ "To suppress this warning, either remove tools from "
195
+ "the request or set tool_choice to a different value.")
196
+ tool_dicts = None
197
+ else:
198
+ tool_dicts = [tool.model_dump() for tool in request.tools]
199
200
(
201
conversation,