ModuleNotFoundError: which package is missing
Why this matters
LangChain 1.2.x is organized as a monorepo with separate packages for different providers. Trying to import ChatOpenAI from langchain instead of langchain_openai will fail immediately, blocking your entire project. Understanding the package structure saves debugging time and prevents frustration on first setup.
Explanation
What's happening: LangChain split its codebase into a core package (langchain-core) and provider-specific packages (langchain-openai, langchain-anthropic, etc.) starting in version 0.1.0. Many imports that worked in older versions now live in different packages. How it works: When you write from langchain import ChatOpenAI, Python looks for ChatOpenAI in the langchain package's namespace. But in 1.2.x, ChatOpenAI only exists in langchain_openai. The langchain package doesn't re-export it (by design: to keep dependencies lightweight). When to use this: Every time you import an LLM, chat model, embedding, or tool from an external provider, you need the provider-specific package. Core abstractions (prompts, chains, LCEL patterns) come from langchain or langchain-core.
Analogy
Think of LangChain like a restaurant supply store that split into separate shops: one for plates (langchain-core), one for hamburger buns (langchain-openai), one for lettuce (langchain-anthropic). You can't go to the main office and ask for buns: you have to know which specialty shop sells them.
Code
import sys
import subprocess
# WRONG: This will fail
try:
from langchain import ChatOpenAI
print("✗ Unexpectedly succeeded (this code is from an old LangChain version)")
except ModuleNotFoundError as e:
print(f"❌ Error (expected): {e}")
# RIGHT: Install the correct package
print("\nInstalling langchain-openai...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", "langchain-openai"])
# NOW this works
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
llm = ChatOpenAI(model="gpt-4o-mini", api_key="sk-test-fake-key")
prompt = ChatPromptTemplate.from_template("Say hello to {name}")
chain = prompt | llm | StrOutputParser()
print(f"✓ Imports successful")
print(f"✓ ChatOpenAI instance created: {type(llm).__name__}")
print(f"✓ Chain created: {type(chain).__name__}") ❌ Error (expected): No module named 'langchain_openai' Installing langchain-openai... ✓ Imports successful ✓ ChatOpenAI instance created: ChatOpenAI ✓ Chain created: RunnableSequence
What just happened?
The code attempted the wrong import, caught the error, installed the missing package, then performed the correct import. All three components (ChatOpenAI, ChatPromptTemplate, StrOutputParser) are now in scope and instantiated without error.
Common gotcha
The gotcha is that langchain itself installs fine; you don't get an error until you try to import a provider-specific class. Many developers install only langchain, write code that imports from it, and don't realize the missing package until they try to run the script. The error message says 'No module named langchain_openai': not 'ChatOpenAI not found': which can be confusing at first.
Error recovery
ModuleNotFoundError: No module named 'langchain_openai'ModuleNotFoundError: No module named 'langchain_anthropic'ModuleNotFoundError: No module named 'langchain_community'ImportError: cannot import name 'ChatOpenAI' from 'langchain'Experienced dev note
The package split is a deliberate architectural choice: it lets users install only what they need (reducing dependency bloat) and lets provider packages update independently of core LangChain. But it breaks everyone's muscle memory from LangChain 0.0.x. The fix: always import base classes from langchain or langchain_core, and provider-specific classes from langchain-{provider}. As a senior developer, you'll want to use langchain-core imports in shared libraries: it has fewer transitive dependencies: and save provider imports for application code only.
Check your understanding
If you're building a RAG application that uses OpenAI for generation and Pinecone for vector storage, which packages do you need to install, and where does each import come from?
Show answer hint
A correct answer identifies at least three packages: langchain (or langchain-core for base classes), langchain-openai (for ChatOpenAI), and langchain-community (or langchain-pinecone if it exists). It also correctly maps ChatOpenAI to langchain_openai and Pinecone classes to their correct location.