Home / Function/ convert_to_anthropic_tool() — langchain Function Reference

convert_to_anthropic_tool() — langchain Function Reference

Architecture documentation for the convert_to_anthropic_tool() function in chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  b9533de7_77f0_ac15_f27f_92372b897314["convert_to_anthropic_tool()"]
  a85819c7_917d_4c71_2864_a19e68947340["chat_models.py"]
  b9533de7_77f0_ac15_f27f_92372b897314 -->|defined in| a85819c7_917d_4c71_2864_a19e68947340
  b4ebc2e5_c582_39ab_6403_117a559ab366["bind_tools()"]
  b4ebc2e5_c582_39ab_6403_117a559ab366 -->|calls| b9533de7_77f0_ac15_f27f_92372b897314
  a484b53c_8c1c_5314_de44_ea0330c8aed2["with_structured_output()"]
  a484b53c_8c1c_5314_de44_ea0330c8aed2 -->|calls| b9533de7_77f0_ac15_f27f_92372b897314
  69a9ca1f_89ff_e8cc_0a13_8af61d334b1d["get_num_tokens_from_messages()"]
  69a9ca1f_89ff_e8cc_0a13_8af61d334b1d -->|calls| b9533de7_77f0_ac15_f27f_92372b897314
  style b9533de7_77f0_ac15_f27f_92372b897314 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/chat_models.py lines 1796–1849

def convert_to_anthropic_tool(
    tool: Mapping[str, Any] | type | Callable | BaseTool,
    *,
    strict: bool | None = None,
) -> AnthropicTool:
    """Convert a tool-like object to an Anthropic tool definition.

    Args:
        tool: A tool-like object to convert. Can be an Anthropic tool dict,
            a Pydantic model, a function, or a `BaseTool`.
        strict: If `True`, enables strict schema adherence for the tool.

            !!! note

                Requires Claude Sonnet 4.5 or Opus 4.1.

    Returns:
        `AnthropicTool` for custom/user-defined tools
    """
    if (
        isinstance(tool, BaseTool)
        and hasattr(tool, "extras")
        and isinstance(tool.extras, dict)
        and "provider_tool_definition" in tool.extras
    ):
        # Pass through built-in tool definitions
        return tool.extras["provider_tool_definition"]  # type: ignore[return-value]

    if isinstance(tool, dict) and all(
        k in tool for k in ("name", "description", "input_schema")
    ):
        # Anthropic tool format
        anthropic_formatted = AnthropicTool(tool)  # type: ignore[misc]
    else:
        oai_formatted = convert_to_openai_tool(tool, strict=strict)["function"]
        anthropic_formatted = AnthropicTool(
            name=oai_formatted["name"],
            input_schema=oai_formatted["parameters"],
        )
        if "description" in oai_formatted:
            anthropic_formatted["description"] = oai_formatted["description"]
        if "strict" in oai_formatted and isinstance(strict, bool):
            anthropic_formatted["strict"] = oai_formatted["strict"]
        # Select params from tool.extras
        if (
            isinstance(tool, BaseTool)
            and hasattr(tool, "extras")
            and isinstance(tool.extras, dict)
        ):
            for key, value in tool.extras.items():
                if key in _ANTHROPIC_EXTRA_FIELDS:
                    # all are populated top-level
                    anthropic_formatted[key] = value  # type: ignore[literal-required]
    return anthropic_formatted

Domain

Subdomains

Frequently Asked Questions

What does convert_to_anthropic_tool() do?
convert_to_anthropic_tool() is a function in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/chat_models.py.
Where is convert_to_anthropic_tool() defined?
convert_to_anthropic_tool() is defined in libs/partners/anthropic/langchain_anthropic/chat_models.py at line 1796.
What calls convert_to_anthropic_tool()?
convert_to_anthropic_tool() is called by 3 function(s): bind_tools, get_num_tokens_from_messages, with_structured_output.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free