High severity beginner · Fix: 2-5 min

ImportError

ImportError

What this error means
LangChain ImportError occurs when importing from deprecated module paths that have been reorganized in recent versions.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 3, in <module>
    from langchain.chat_models import ChatOpenAI
ImportError: cannot import name 'ChatOpenAI' from 'langchain.chat_models' (unknown location)
QUICK FIX
Replace deprecated imports like 'from langchain.chat_models import ChatOpenAI' with 'from langchain_openai import ChatOpenAI'.

Why it happens

LangChain reorganized its module structure in versions after 0.1.0, moving classes like ChatOpenAI to new submodules. Importing from old deprecated paths causes ImportError because those modules no longer exist or have been renamed.

Detection

Run your app and watch for ImportError stack traces referencing missing modules or classes in LangChain. Use automated tests that import all used LangChain classes to catch broken imports early.

Causes & fixes

1

Using old import path 'from langchain.chat_models import ChatOpenAI' which is deprecated

✓ Fix

Change import to 'from langchain_openai import ChatOpenAI' as per new LangChain SDK v0.2+ structure

2

Importing LangChain classes from deprecated top-level modules instead of dedicated sub-packages

✓ Fix

Refer to the latest LangChain documentation and update all imports to the new package paths, e.g., 'langchain_openai' for OpenAI models

Code: broken vs fixed

Broken - triggers the error
python
from langchain.chat_models import ChatOpenAI  # Deprecated import causing ImportError

client = ChatOpenAI(model_name="gpt-4o-mini")
print(client)
Fixed - works correctly
python
import os
from langchain_openai import ChatOpenAI  # Fixed import path

os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "your_api_key_here")
client = ChatOpenAI(model_name="gpt-4o-mini")
print(client)
Updated import to 'langchain_openai' which is the correct module path in LangChain v0.2+ to avoid ImportError.

Workaround

If you cannot upgrade immediately, pin your LangChain version to a compatible older release that still supports the deprecated import paths.

Prevention

Regularly review LangChain release notes for breaking changes and update import paths accordingly; use automated import validation tests in CI pipelines.

Python 3.9+ · langchain >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

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