Skip to main content

[Deprecated] Experimental Anthropic Tools Wrapper


The Anthropic API officially supports tool-calling so this workaround is no longer needed. Please use ChatAnthropic with langchain-anthropic>=0.1.5.

This notebook shows how to use an experimental wrapper around Anthropic that gives it tool calling and structured output capabilities. It follows Anthropicโ€™s guide here

The wrapper is available from the langchain-anthropic package, and it also requires the optional dependency defusedxml for parsing XML output from the llm.

Note: this is a beta feature that will be replaced by Anthropicโ€™s formal implementation of tool calling, but it is useful for testing and experimentation in the meantime.

%pip install -qU langchain-anthropic defusedxml
from langchain_anthropic.experimental import ChatAnthropicTools

Tool Bindingโ€‹

ChatAnthropicTools exposes a bind_tools method that allows you to pass in Pydantic models or BaseTools to the llm.

from langchain_core.pydantic_v1 import BaseModel

class Person(BaseModel):
name: str
age: int

model = ChatAnthropicTools(model="claude-3-opus-20240229").bind_tools(tools=[Person])
model.invoke("I am a 27 year old named Erick")
AIMessage(content='', additional_kwargs={'tool_calls': [{'function': {'name': 'Person', 'arguments': '{"name": "Erick", "age": "27"}'}, 'type': 'function'}]})

Structured Outputโ€‹

ChatAnthropicTools also implements the with_structured_output spec for extracting values. Note: this may not be as stable as with models that explicitly offer tool calling.

chain = ChatAnthropicTools(model="claude-3-opus-20240229").with_structured_output(
chain.invoke("I am a 27 year old named Erick")
Person(name='Erick', age=27)

Help us out by providing feedback on this documentation page: