ImportError
ImportError: cannot import name 'messages' from 'anthropic' (version deprecated)
Stack trace
Traceback (most recent call last):
File "app.py", line 5, in <module>
from anthropic import messages
ImportError: cannot import name 'messages' from 'anthropic' (unknown location) Why it happens
Anthropic released SDK v0.20+ with breaking changes including new import paths and method signatures. Using older versions or deprecated import patterns causes ImportError or attribute errors because the package structure changed.
Detection
Check your installed Anthropic SDK version with pip list or pip show anthropic and verify your import statements against the latest SDK documentation before runtime.
Causes & fixes
Using deprecated import syntax like 'from anthropic import messages' from older SDK versions
Update your code to use the new SDK v0.20+ import pattern: 'client.messages.create(...)' after instantiating 'client = Anthropic()'
Installed Anthropic SDK version is older than 0.20 which lacks new API methods
Upgrade the Anthropic SDK package to version 0.20 or later using 'pip install --upgrade anthropic'
Mixing old and new Anthropic SDK code patterns causing attribute or import errors
Refactor all Anthropic API calls to follow the new SDK v0.20+ usage consistently throughout your codebase
Code: broken vs fixed
from anthropic import messages # deprecated import causing ImportError
client = anthropic.Anthropic()
response = messages.create(model="claude-2", prompt="Hello") # triggers error import os
from anthropic import Anthropic
client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"]) # fixed: use new client instantiation
response = client.messages.create(model="claude-3-5-haiku-20241022", messages=[{"role": "user", "content": "Hello"}]) # fixed: new method call
print(response) Workaround
If you cannot upgrade immediately, pin your Anthropic SDK to the last compatible version and avoid using new API calls until you can refactor your code.
Prevention
Regularly update your dependencies and review SDK changelogs for breaking changes; adopt the latest SDK usage patterns to avoid deprecated import errors.