High severity beginner · Fix: 2-5 min

ImportError

ImportError: cannot import name 'messages' from 'anthropic' (version deprecated)

What this error means
This error occurs when using an outdated Anthropic SDK version that no longer supports the old import or method signatures.

Stack trace

traceback
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)
QUICK FIX
Upgrade the Anthropic SDK to v0.20+ and update imports to use 'client.messages.create(...)' instead of deprecated patterns.

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

1

Using deprecated import syntax like 'from anthropic import messages' from older SDK versions

✓ Fix

Update your code to use the new SDK v0.20+ import pattern: 'client.messages.create(...)' after instantiating 'client = Anthropic()'

2

Installed Anthropic SDK version is older than 0.20 which lacks new API methods

✓ Fix

Upgrade the Anthropic SDK package to version 0.20 or later using 'pip install --upgrade anthropic'

3

Mixing old and new Anthropic SDK code patterns causing attribute or import errors

✓ Fix

Refactor all Anthropic API calls to follow the new SDK v0.20+ usage consistently throughout your codebase

Code: broken vs fixed

Broken - triggers the error
python
from anthropic import messages  # deprecated import causing ImportError

client = anthropic.Anthropic()
response = messages.create(model="claude-2", prompt="Hello")  # triggers error
Fixed - works correctly
python
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)
Updated to Anthropic SDK v0.20+ usage by importing Anthropic class, instantiating client with API key from environment, and calling 'client.messages.create' with correct parameters.

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.

Python 3.9+ · anthropic >=0.20.0 · tested on 0.20.0
Verified 2026-04 · claude-3-5-haiku-20241022
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.