[Bugfix] Hoist $defs/definitions in Cohere parser tool schema composition (#49602)
Signed-off-by: Václav Čadek <vaclavcadek@gmail.com> Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor Agent
parent
78fc1e6274
commit
6cbbea8721
@@ -127,22 +127,19 @@ def collect_tool_schema(tool_schema: list[CohereNormalizedTool]) -> str:
|
||||
tool_dictionary: dict[str, str] = {}
|
||||
for tool in tool_schema:
|
||||
tool_name = tool["name"]
|
||||
tool_parameters = json.dumps(tool["parameters"])
|
||||
json_schema = f"""{{
|
||||
"type": "object",
|
||||
"properties": {{
|
||||
"tool_call_id": {{
|
||||
"type": "string",
|
||||
"pattern": "^[0-9]+$"
|
||||
}},
|
||||
"tool_name": {{
|
||||
"type": "string",
|
||||
"const": "{tool_name}"
|
||||
}},
|
||||
"parameters": {tool_parameters}
|
||||
}}
|
||||
}}"""
|
||||
tool_grammar = str(xgr.Grammar.from_json_schema(json_schema))
|
||||
tool_parameters = dict(tool["parameters"])
|
||||
json_schema: dict[str, Any] = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"tool_call_id": {"type": "string", "pattern": "^[0-9]+$"},
|
||||
"tool_name": {"type": "string", "const": tool_name},
|
||||
"parameters": tool_parameters,
|
||||
},
|
||||
}
|
||||
for defs_key in ("$defs", "definitions"):
|
||||
if defs_key in tool_parameters:
|
||||
json_schema[defs_key] = tool_parameters.pop(defs_key)
|
||||
tool_grammar = str(xgr.Grammar.from_json_schema(json.dumps(json_schema)))
|
||||
for match in re.findall(r"\b(\w+)\s*::=", tool_grammar):
|
||||
tool_grammar = re.sub(
|
||||
rf"\b{re.escape(match)}\b", tool_name + match, tool_grammar
|
||||
|
||||
Reference in New Issue
Block a user