ImportError
ImportError
Stack trace
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) 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
Using old import path 'from langchain.chat_models import ChatOpenAI' which is deprecated
Change import to 'from langchain_openai import ChatOpenAI' as per new LangChain SDK v0.2+ structure
Importing LangChain classes from deprecated top-level modules instead of dedicated sub-packages
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
from langchain.chat_models import ChatOpenAI # Deprecated import causing ImportError
client = ChatOpenAI(model_name="gpt-4o-mini")
print(client) 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) 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.