ImportError: cannot import name 'OpenAI' from 'langchain'
Model classes moved out of the langchain package into langchain-openai and friends. Install the partner package and update the import path.
Updated
The error
ImportError: cannot import name 'OpenAI' from 'langchain' (/usr/lib/python3.11/site-packages/langchain/__init__.py)
Depending on version, the same break appears as cannot import name 'ChatOpenAI', or as a helpful refusal telling you which package to install.
What it means
In old LangChain, provider classes lived in the main package: from langchain import OpenAI, from langchain.chat_models import ChatOpenAI. The restructuring moved every provider integration out — OpenAI classes now live in the langchain-openai partner package. Your import path points at where the class used to be.
Why it happens
You are running code from the pre-split era (most tutorials, courses and older Stack Overflow answers) on a current LangChain install. Nothing is broken; the map changed.
How to fix it
1. Install the partner package and update the import.
pip install -U langchain-openaifrom langchain_openai import ChatOpenAI, OpenAI, OpenAIEmbeddings
llm = ChatOpenAI(model="gpt-4o-mini")Note the underscore in the module (langchain_openai) versus the hyphen in the pip name (langchain-openai).
2. Use the translation table for the common moves.
| Old import | Current import |
|---|---|
from langchain import OpenAI | from langchain_openai import OpenAI |
from langchain.chat_models import ChatOpenAI | from langchain_openai import ChatOpenAI |
from langchain.embeddings import OpenAIEmbeddings | from langchain_openai import OpenAIEmbeddings |
from langchain.chat_models import ChatAnthropic | from langchain_anthropic import ChatAnthropic |
from langchain.vectorstores import Chroma | from langchain_chroma import Chroma |
from langchain.vectorstores import FAISS | from langchain_community.vectorstores import FAISS |
Each partner package installs separately (langchain-anthropic, langchain-chroma, and so on).
3. Modernise the deprecated class names while you are in there. Old OpenAI completion-style usage maps to chat models today; new code should reach for ChatOpenAI (or init_chat_model) unless you specifically need a completion endpoint.
4. If the error names a class that should exist, check the family versions. A stale langchain-core alongside a new langchain-openai can produce import-time failures inside the packages themselves:
pip install -U langchain langchain-core langchain-openaiHow to prevent it
When copying LangChain examples, glance at the imports before running: anything importing providers from bare langchain is pre-split code and will need the table above. Keep the family upgraded together, and prefer the official docs' quickstart imports as the source of truth.
Related errors
- No module named 'langchain_community' — the sibling break, with the package-split background
- openai.ChatCompletion is no longer supported — the same era-break one layer down
- Missing some input keys (PromptTemplate)