TypeError
TypeError: from_anthropic() got an unexpected keyword argument 'tool_use'
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
instructor_instance = Instructor.from_anthropic(tool_use=True)
TypeError: from_anthropic() got an unexpected keyword argument 'tool_use' Why it happens
The from_anthropic method in the instructor library does not accept a 'tool_use' keyword argument. This usually happens when developers try to enable Anthropic's tool use feature incorrectly or with outdated method signatures.
Detection
Catch TypeError exceptions when calling from_anthropic and log the exact arguments passed to identify unsupported parameters before the app crashes.
Causes & fixes
Passing 'tool_use' argument to from_anthropic which does not support it
Remove the 'tool_use' argument from the from_anthropic call and configure tool use separately via Anthropic client or prompt.
Using outdated instructor library version that lacks tool use support
Upgrade the instructor library to the latest version that supports Anthropic tool use properly or follow updated usage docs.
Confusing Anthropic SDK's tool use parameter with instructor's from_anthropic method parameters
Use Anthropic SDK client directly to enable tool use, and instantiate instructor without unsupported keyword arguments.
Code: broken vs fixed
from instructor import Instructor
# This line causes the error
instructor_instance = Instructor.from_anthropic(tool_use=True)
print(instructor_instance) import os
from instructor import Instructor
# Removed unsupported 'tool_use' argument
instructor_instance = Instructor.from_anthropic()
print(instructor_instance)
# Set environment variables or configure Anthropic client separately for tool use Workaround
Wrap the from_anthropic call in try/except TypeError, then instantiate without 'tool_use' and configure Anthropic tool use separately via the Anthropic SDK client.
Prevention
Consult the latest instructor and Anthropic SDK documentation to use supported parameters only and separate concerns between instructor instantiation and Anthropic client configuration.