[Frontend] Support Tool Calling with both tool_choice='required' and $defs. (#20629)

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
This commit is contained in:
Chauncey
2025-07-10 13:56:35 +08:00
committed by GitHub
parent ad6c2e1a0b
commit 8f2720def9
2 changed files with 56 additions and 0 deletions
+21
View File
@@ -707,6 +707,24 @@ class ChatCompletionRequest(OpenAIBaseModel):
"required": ["name", "parameters"]
}
def get_tool_schema_defs(
tools: list[ChatCompletionToolsParam]) -> dict:
all_defs = dict[str, dict[str, Any]]()
for tool in tools:
if tool.function.parameters is None:
continue
defs = tool.function.parameters.pop("$defs", {})
for def_name, def_schema in defs.items():
if def_name in all_defs and all_defs[
def_name] != def_schema:
raise ValueError(
f"Tool definition '{def_name}' has "
"multiple schemas, which is not "
"supported.")
else:
all_defs[def_name] = def_schema
return all_defs
json_schema = {
"type": "array",
"minItems": 1,
@@ -715,6 +733,9 @@ class ChatCompletionRequest(OpenAIBaseModel):
"anyOf": [get_tool_schema(tool) for tool in self.tools]
}
}
json_schema_defs = get_tool_schema_defs(self.tools)
if json_schema_defs:
json_schema["$defs"] = json_schema_defs
return json_schema
return None