Skip to content

Commit

Permalink
[Bugfix] Internal Server Error when tool_choice is incorrect. (vllm-p…
Browse files Browse the repository at this point in the history
…roject#10567)

Signed-off-by: Varun Shenoy <[email protected]>
Signed-off-by: Maxime Fournioux <[email protected]>
  • Loading branch information
shenoyvvarun authored and mfournioux committed Nov 28, 2024
1 parent b55bd44 commit e656b06
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
14 changes: 14 additions & 0 deletions tests/entrypoints/openai/test_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,20 @@ async def test_inconsistent_tool_choice_and_tools(client: openai.AsyncOpenAI,
"name": "nondefined_function_name"
}
})
with pytest.raises(openai.BadRequestError):
await client.chat.completions.create(
model=MODEL_NAME,
messages=messages,
max_completion_tokens=1000,
tools=[{
"type": "function",
"function": {
"name": "dummy_function_name",
"description": "This is a dummy function",
"parameters": sample_json_schema
}
}],
tool_choice={})


@pytest.mark.asyncio
Expand Down
12 changes: 6 additions & 6 deletions vllm/entrypoints/openai/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,17 +478,17 @@ def check_tool_usage(cls, data):
# it matches a valid tool
if isinstance(data["tool_choice"], dict):
valid_tool = False
specified_function = data["tool_choice"]["function"]
specified_function = data["tool_choice"].get("function")
if not specified_function:
raise ValueError(
"Incorrectly formatted `tool_choice`. Should be like "
"`{\"type\": \"function\","
"Expected field `function` in `tool_choice`."
" Correct usage: `{\"type\": \"function\","
" \"function\": {\"name\": \"my_function\"}}`")
specified_function_name = specified_function["name"]
specified_function_name = specified_function.get("name")
if not specified_function_name:
raise ValueError(
"Incorrectly formatted `tool_choice`. Should be like "
"`{\"type\": \"function\", "
"Expected field `name` in `function` in `tool_choice`."
"Correct usage: `{\"type\": \"function\", "
"\"function\": {\"name\": \"my_function\"}}`")
for tool in data["tools"]:
if tool["function"]["name"] == specified_function_name:
Expand Down

0 comments on commit e656b06

Please sign in to comment.