-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix(protocol): check_function_call_usage raises raw KeyError on malformed tool_choice dict #3514
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
Open
chuenchen309
wants to merge
1
commit into
mlc-ai:main
Choose a base branch
from
chuenchen309:fix/tool-choice-missing-name-keyerror
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+57
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import pytest | ||
|
|
||
| from mlc_llm.protocol.conversation_protocol import Conversation | ||
| from mlc_llm.protocol.error_protocol import BadRequestError | ||
| from mlc_llm.protocol.openai_api_protocol import ChatCompletionRequest | ||
|
|
||
| # test category "unittest" | ||
| pytestmark = [pytest.mark.unittest] | ||
|
|
||
|
|
||
| def _make_request(tool_choice): | ||
| return ChatCompletionRequest( | ||
| messages=[{"role": "user", "content": "hi"}], | ||
| model="test", | ||
| tools=[ | ||
| { | ||
| "type": "function", | ||
| "function": {"name": "foo", "description": "d", "parameters": {}}, | ||
| } | ||
| ], | ||
| tool_choice=tool_choice, | ||
| ) | ||
|
|
||
|
|
||
| def _make_conv(): | ||
| return Conversation(name="test", roles={"user": "user", "assistant": "assistant"}, seps=[" "]) | ||
|
|
||
|
|
||
| def test_check_function_call_usage_missing_function_name_raises_bad_request(): | ||
| """tool_choice is an unstructured Dict, so a request can omit the required | ||
| "name" field. This must raise the documented BadRequestError, not a raw | ||
| KeyError.""" | ||
| request = _make_request({"type": "function", "function": {}}) | ||
| with pytest.raises(BadRequestError): | ||
| request.check_function_call_usage(_make_conv()) | ||
|
|
||
|
|
||
| def test_check_function_call_usage_missing_type_raises_bad_request(): | ||
| """Same class of issue for a missing "type" key.""" | ||
| request = _make_request({"function": {"name": "foo"}}) | ||
| with pytest.raises(BadRequestError): | ||
| request.check_function_call_usage(_make_conv()) | ||
|
|
||
|
|
||
| def test_check_function_call_usage_selects_tool_on_valid_choice(): | ||
| request = _make_request({"type": "function", "function": {"name": "foo"}}) | ||
| conv_template = _make_conv() | ||
| request.check_function_call_usage(conv_template) | ||
| assert conv_template.use_function_calling is True |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check
len(tool_choice_function) > 1counts the number of keys in thetool_choice_functiondictionary (e.g.,{"name": "foo"}has a length of 1). It does not check the number of tools specified, astool_choice_functionis already a single dictionary representing one function choice. If a client sends any extra or custom keys in thefunctionobject, this check will raise aBadRequestErrorwith a misleading message ("Only one tool is supported when tool_choice is specified"). Sincetool_choiceas a dictionary can only specify a single function anyway, this check is redundant and should be removed.