Skip to content

Commit 7706ec9

Browse files
chiragjnAlvant
authored andcommitted
fix validation: Only set tool_choice auto if at least one tool is provided (vllm-project#8568)
Signed-off-by: Alvant <alvasian@yandex.ru>
1 parent 9e33419 commit 7706ec9

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import pytest
2+
3+
from vllm.entrypoints.openai.protocol import ChatCompletionRequest
4+
5+
6+
def test_chat_completion_request_with_no_tools():
7+
# tools key is not present
8+
request = ChatCompletionRequest.model_validate({
9+
'messages': [{
10+
'role': 'user',
11+
'content': 'Hello'
12+
}],
13+
'model':
14+
'facebook/opt-125m',
15+
})
16+
assert request.tool_choice == 'none'
17+
18+
# tools key is None
19+
request = ChatCompletionRequest.model_validate({
20+
'messages': [{
21+
'role': 'user',
22+
'content': 'Hello'
23+
}],
24+
'model':
25+
'facebook/opt-125m',
26+
'tools':
27+
None
28+
})
29+
assert request.tool_choice == 'none'
30+
31+
# tools key present but empty
32+
request = ChatCompletionRequest.model_validate({
33+
'messages': [{
34+
'role': 'user',
35+
'content': 'Hello'
36+
}],
37+
'model':
38+
'facebook/opt-125m',
39+
'tools': []
40+
})
41+
assert request.tool_choice == 'none'
42+
43+
44+
def test_chat_completion_request_with_tool_choice_but_no_tools():
45+
with pytest.raises(ValueError,
46+
match="When using `tool_choice`, `tools` must be set."):
47+
ChatCompletionRequest.model_validate({
48+
'messages': [{
49+
'role': 'user',
50+
'content': 'Hello'
51+
}],
52+
'model':
53+
'facebook/opt-125m',
54+
'tool_choice':
55+
'auto'
56+
})
57+
58+
with pytest.raises(ValueError,
59+
match="When using `tool_choice`, `tools` must be set."):
60+
ChatCompletionRequest.model_validate({
61+
'messages': [{
62+
'role': 'user',
63+
'content': 'Hello'
64+
}],
65+
'model':
66+
'facebook/opt-125m',
67+
'tool_choice':
68+
'auto',
69+
'tools':
70+
None
71+
})

vllm/entrypoints/openai/protocol.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ def check_tool_usage(cls, data):
386386

387387
# if "tool_choice" is not specified but tools are provided,
388388
# default to "auto" tool_choice
389-
if "tool_choice" not in data and "tools" in data:
389+
if "tool_choice" not in data and data.get("tools"):
390390
data["tool_choice"] = "auto"
391391

392392
# if "tool_choice" is specified -- validation

0 commit comments

Comments
 (0)