"vscode:/vscode.git/clone" did not exist on "2d465ec7071e05d4bdd77770125e561065fc814a"
Unverified Commit 7d8ffb34 authored by Varun Vinayak Shenoy's avatar Varun Vinayak Shenoy Committed by GitHub
Browse files

[Bugfix] Internal Server Error when tool_choice is incorrect. (#10567)


Signed-off-by: default avatarVarun Shenoy <varun.vinayak.shenoy@oracle.com>
parent 4aba6e3d
...@@ -829,6 +829,20 @@ async def test_inconsistent_tool_choice_and_tools(client: openai.AsyncOpenAI, ...@@ -829,6 +829,20 @@ async def test_inconsistent_tool_choice_and_tools(client: openai.AsyncOpenAI,
"name": "nondefined_function_name" "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 @pytest.mark.asyncio
......
...@@ -478,17 +478,17 @@ class ChatCompletionRequest(OpenAIBaseModel): ...@@ -478,17 +478,17 @@ class ChatCompletionRequest(OpenAIBaseModel):
# it matches a valid tool # it matches a valid tool
if isinstance(data["tool_choice"], dict): if isinstance(data["tool_choice"], dict):
valid_tool = False valid_tool = False
specified_function = data["tool_choice"]["function"] specified_function = data["tool_choice"].get("function")
if not specified_function: if not specified_function:
raise ValueError( raise ValueError(
"Incorrectly formatted `tool_choice`. Should be like " "Expected field `function` in `tool_choice`."
"`{\"type\": \"function\"," " Correct usage: `{\"type\": \"function\","
" \"function\": {\"name\": \"my_function\"}}`") " \"function\": {\"name\": \"my_function\"}}`")
specified_function_name = specified_function["name"] specified_function_name = specified_function.get("name")
if not specified_function_name: if not specified_function_name:
raise ValueError( raise ValueError(
"Incorrectly formatted `tool_choice`. Should be like " "Expected field `name` in `function` in `tool_choice`."
"`{\"type\": \"function\", " "Correct usage: `{\"type\": \"function\", "
"\"function\": {\"name\": \"my_function\"}}`") "\"function\": {\"name\": \"my_function\"}}`")
for tool in data["tools"]: for tool in data["tools"]:
if tool["function"]["name"] == specified_function_name: if tool["function"]["name"] == specified_function_name:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment