Error database

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.

The message you saw
ImportError: cannot import name 'OpenAI' from 'langchain'

By Updated

The error

Output
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.

bash
pip install -U langchain-openai
python
from 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 importCurrent import
from langchain import OpenAIfrom langchain_openai import OpenAI
from langchain.chat_models import ChatOpenAIfrom langchain_openai import ChatOpenAI
from langchain.embeddings import OpenAIEmbeddingsfrom langchain_openai import OpenAIEmbeddings
from langchain.chat_models import ChatAnthropicfrom langchain_anthropic import ChatAnthropic
from langchain.vectorstores import Chromafrom langchain_chroma import Chroma
from langchain.vectorstores import FAISSfrom 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:

bash
pip install -U langchain langchain-core langchain-openai

How 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.