Skip to content

fix validation: Only set tool_choice auto if at least one tool is provided #8568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions tests/tool_use/test_chat_completion_request_validations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import pytest

from vllm.entrypoints.openai.protocol import ChatCompletionRequest


def test_chat_completion_request_with_no_tools():
# tools key is not present
request = ChatCompletionRequest.model_validate({
'messages': [{
'role': 'user',
'content': 'Hello'
}],
'model':
'facebook/opt-125m',
})
assert request.tool_choice == 'none'

# tools key is None
request = ChatCompletionRequest.model_validate({
'messages': [{
'role': 'user',
'content': 'Hello'
}],
'model':
'facebook/opt-125m',
'tools':
None
})
assert request.tool_choice == 'none'

# tools key present but empty
request = ChatCompletionRequest.model_validate({
'messages': [{
'role': 'user',
'content': 'Hello'
}],
'model':
'facebook/opt-125m',
'tools': []
})
assert request.tool_choice == 'none'


def test_chat_completion_request_with_tool_choice_but_no_tools():
with pytest.raises(ValueError,
match="When using `tool_choice`, `tools` must be set."):
ChatCompletionRequest.model_validate({
'messages': [{
'role': 'user',
'content': 'Hello'
}],
'model':
'facebook/opt-125m',
'tool_choice':
'auto'
})

with pytest.raises(ValueError,
match="When using `tool_choice`, `tools` must be set."):
ChatCompletionRequest.model_validate({
'messages': [{
'role': 'user',
'content': 'Hello'
}],
'model':
'facebook/opt-125m',
'tool_choice':
'auto',
'tools':
None
})
2 changes: 1 addition & 1 deletion vllm/entrypoints/openai/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def check_tool_usage(cls, data):

# if "tool_choice" is not specified but tools are provided,
# default to "auto" tool_choice
if "tool_choice" not in data and "tools" in data:
if "tool_choice" not in data and data.get("tools"):
data["tool_choice"] = "auto"

# if "tool_choice" is specified -- validation
Expand Down
Loading