Skip to content

Add anthropic style of function schema#45167

Open
xu-song wants to merge 2 commits intohuggingface:mainfrom
xu-song:patch-5
Open

Add anthropic style of function schema#45167
xu-song wants to merge 2 commits intohuggingface:mainfrom
xu-song:patch-5

Conversation

@xu-song
Copy link
Copy Markdown
Contributor

@xu-song xu-song commented Apr 1, 2026

What does this PR do?

This PR adds support for Anthropic's JSON function style:

{"name": "...", "description": "...", "input_schema": {...}}

Usage Example - Anthropic Demo

import anthropic
from transformers.utils import get_json_schema

def multiply(a: float, b: float) -> float:
    """
    A function that multiplies two numbers

    Args:
        a: The first number to multiply
        b: The second number to multiply
    """
    return a * b

anthropic_tools = [get_json_schema(multiply, style="anthropic")]  #

client = anthropic.Anthropic()
response = client.messages.create(
    model="claude",
    max_tokens=1024,
    tools=anthropic_tools,
    messages=messages,
)

Related Implementations

vLLM supports Anthropic format

    def _convert_anthropic_to_openai_request(
        cls, anthropic_request: AnthropicMessagesRequest | AnthropicCountTokensRequest
    ) -> ChatCompletionRequest:
        """Convert Anthropic message format to OpenAI format"""
        ...
        cls._convert_tools(anthropic_request, req)
        return req


    def _convert_tools(
        cls,
        anthropic_request: AnthropicMessagesRequest | AnthropicCountTokensRequest,
        req: ChatCompletionRequest,
    ) -> None:
        """Convert Anthropic tools to OpenAI format"""
        ...
        for tool in anthropic_request.tools:
            tools.append(
                ChatCompletionToolsParam.model_validate(
                    {
                        "type": "function",
                        "function": {
                            "name": tool.name,
                            "description": tool.description,
                            "parameters": tool.input_schema,
                        },
                    }
                )
            )

Code Agent Policy

  • I confirm that this is not a pure code agent PR.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 1, 2026

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=45167&sha=0c93cc

@Rocketknight1
Copy link
Copy Markdown
Member

Hmm, mixed about this! It's useful, but I worry it might cause confusion - we're trying to enforce a single standard tool format for our chat templates, so the "anthropic" mode would only be useful for users who just want to use transformers to format tool schemas for Claude, and I'd have to yell at anyone who writes a chat template that expects it 😅

@xu-song
Copy link
Copy Markdown
Contributor Author

xu-song commented Apr 3, 2026

Most Jinja templates require OpenAI-style tools, like MiniMax-M2.5

{%- for tool in tool_list -%}
<tool>{{ tool.function \| tojson(ensure_ascii=False) }}</tool>
{% endfor -%}

It is not recommended to directly support Anthropic tools within the apply_chat_template function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants