Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langchain ai Langchain ChatAnthropicTools

From Leeroopedia
Knowledge Sources
Domains Tool Calling, Anthropic, XML Parsing
Last Updated 2026-02-11 00:00 GMT

Overview

The experimental module provides XML-based tool-calling support for Anthropic chat models, generating system prompts with tool descriptions and parsing XML tool-call responses.

Description

This module, located in langchain_anthropic.experimental, implements an experimental mechanism for tool calling with Anthropic models using an XML-based format. It includes functions to generate system messages that describe available tools in XML, parse XML responses back into structured tool call dictionaries, and handle type coercion for tool parameters. The module uses XML templates (SYSTEM_PROMPT_FORMAT, TOOL_FORMAT, TOOL_PARAMETER_FORMAT) to format tool definitions into a system prompt that instructs the model on how to invoke tools.

Usage

Import the functions from this module when integrating Anthropic models with tool-calling capabilities using the XML-based invocation format. This is considered experimental functionality.

Code Reference

Source Location

  • Repository: Langchain_ai_Langchain
  • File: libs/partners/anthropic/langchain_anthropic/experimental.py
  • Lines: 1-141

Signature

def get_system_message(tools: list[dict]) -> str: ...

def _get_type(parameter: dict[str, Any]) -> str: ...

def _xml_to_dict(t: Any) -> str | dict[str, Any]: ...

def _xml_to_function_call(invoke: Any, tools: list[dict]) -> dict[str, Any]: ...

def _xml_to_tool_calls(elem: Any, tools: list[dict]) -> list[dict[str, Any]]: ...

Import

from langchain_anthropic.experimental import get_system_message, _xml_to_tool_calls

I/O Contract

Inputs (get_system_message)

Name Type Required Description
tools list[dict] Yes List of tool definitions, each containing name, description, and parameters (with properties sub-dict).

Inputs (_xml_to_tool_calls)

Name Type Required Description
elem Any Yes An XML element containing invoke child elements.
tools list[dict] Yes The list of tool definitions to match against for type coercion.

Outputs

Name Type Description
get_system_message return str A formatted system message string describing available tools in XML format.
_xml_to_tool_calls return list[dict[str, Any]] List of tool call dictionaries, each with function (containing name and arguments) and type keys.

XML Templates

The module defines three XML template constants:

  • SYSTEM_PROMPT_FORMAT -- The outer template that wraps all tool descriptions and instructs the model on the invocation format.
  • TOOL_FORMAT -- Template for each individual tool description including name, description, and parameters.
  • TOOL_PARAMETER_FORMAT -- Template for each parameter within a tool, including name, type, and description.

Usage Examples

Basic Usage

from langchain_anthropic.experimental import get_system_message

tools = [
    {
        "name": "get_weather",
        "description": "Get the current weather for a location",
        "parameters": {
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state"
                }
            }
        }
    }
]

system_msg = get_system_message(tools)
# Returns a formatted XML system prompt describing the available tools

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment