From 75d28fd01ba8e818d3d1381704e067be1b944be6 Mon Sep 17 00:00:00 2001 From: Xu Song Date: Wed, 1 Apr 2026 22:50:02 +0800 Subject: [PATCH 1/2] Add anthropic style of function schema --- src/transformers/utils/chat_template_utils.py | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/transformers/utils/chat_template_utils.py b/src/transformers/utils/chat_template_utils.py index 3c5ec4acf8d3..e3105be890f8 100644 --- a/src/transformers/utils/chat_template_utils.py +++ b/src/transformers/utils/chat_template_utils.py @@ -243,7 +243,7 @@ def parse_google_format_docstring(docstring: str) -> tuple[str | None, dict | No return description, args_dict, returns -def get_json_schema(func: Callable) -> dict: +def get_json_schema(func: Callable, style: str = "openai") -> dict: """ This function generates a JSON schema for a given function, based on its docstring and type hints. This is mostly used for passing lists of tools to a chat template. The JSON schema contains the name and description of @@ -257,9 +257,14 @@ def get_json_schema(func: Callable) -> dict: Args: func: The function to generate a JSON schema for. + style: The style of the JSON schema to generate. Can be "openai" (default) or "anthropic". + - "openai": Returns schema wrapped in {"type": "function", "function": {...}} format + - "anthropic": Returns schema in {"name": "...", "description": "...", "input_schema": {...}} format Returns: - A dictionary containing the JSON schema for the function. + A dictionary containing the JSON schema for the function. The format depends on the style parameter: + - For "openai" style: {"type": "function", "function": {"name": "...", "description": "...", "parameters": {...}}} + - For "anthropic" style: {"name": "...", "description": "...", "input_schema": {...}} Examples: ```python @@ -375,10 +380,19 @@ def get_json_schema(func: Callable) -> dict: desc = enum_choices.string[: enum_choices.start()].strip() schema["description"] = desc - output = {"name": func_name, "description": main_doc, "parameters": json_schema} - if return_dict is not None: - output["return"] = return_dict - return {"type": "function", "function": output} + if style == "anthropic": + # Anthropic style uses 'input_schema' instead of 'parameters' and doesn't wrap in "function" key + return { + "name": func_name, + "description": main_doc, + "input_schema": json_schema + } + else: + # OpenAI style + output = {"name": func_name, "description": main_doc, "parameters": json_schema} + if return_dict is not None: + output["return"] = return_dict + return {"type": "function", "function": output} @lru_cache From 0c93cc4de3b432c04106a4a056ff1d3d14be0c7c Mon Sep 17 00:00:00 2001 From: Xu Song Date: Wed, 1 Apr 2026 22:59:26 +0800 Subject: [PATCH 2/2] Refactor return statement for anthropic style --- src/transformers/utils/chat_template_utils.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/transformers/utils/chat_template_utils.py b/src/transformers/utils/chat_template_utils.py index e3105be890f8..ce59b75d31ac 100644 --- a/src/transformers/utils/chat_template_utils.py +++ b/src/transformers/utils/chat_template_utils.py @@ -382,11 +382,7 @@ def get_json_schema(func: Callable, style: str = "openai") -> dict: if style == "anthropic": # Anthropic style uses 'input_schema' instead of 'parameters' and doesn't wrap in "function" key - return { - "name": func_name, - "description": main_doc, - "input_schema": json_schema - } + return {"name": func_name, "description": main_doc, "input_schema": json_schema} else: # OpenAI style output = {"name": func_name, "description": main_doc, "parameters": json_schema}