Ari
Ari

Reputation: 766

ModuleNotFoundError: No module named 'langchain_openai'

Following LangChain docs in my Jupyter notebook with the following code :

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser


prompt = ChatPromptTemplate.from_template("Tell me a short joke about {topic}")
model = ChatOpenAI(model="gpt-3.5-turbo")
output_parser = StrOutputParser()

chain = prompt | model | output_parser

Docs say that pip install langchain installs all necessary modules, including langchain-community and langchain-core

However, I get this error:

ModuleNotFoundError: No module named 'langchain_openai'

Upvotes: 13

Views: 50251

Answers (6)

Hanch
Hanch

Reputation: 35

You probably have a typo in your requirements.txt The error message indicate 'langchain_openai' (underscore), wherease it should be 'langchain-openai (dash)

Upvotes: 0

WasimSafdar
WasimSafdar

Reputation: 1132

pip install -U langchain-openai

Then import from base solved it.

from langchain_openai.chat_models.base import ChatOpenAI

Upvotes: 0

dcsan
dcsan

Reputation: 12315

in theory you can use their migrate cli I have these scripts in my just file:

migrate-diff:
    poetry run langchain-cli migrate --diff .

migrate-apply: migrate-diff
    poetry run langchain-cli migrate .

however it usually doesn't fix anything. in fact it often does the opposite of what the docs say to do. 🤷

https://python.langchain.com/v0.2/docs/versions/v0_2/

what a self-inflicted mess LC is

Upvotes: -1

Barsha Chaudhary
Barsha Chaudhary

Reputation: 1

python3.11 -m pip install langchain-openai

I was using python3.9 and I tried installing this using python3.11 and it worked.

Upvotes: 0

dvdr00t
dvdr00t

Reputation: 179

In addition to Ari response, from LangChain version 0.0.10, the ChatOpenAI from the langchain-community package has been deprecated and it will be soon removed from that same package (see: Python API):

[docs]@deprecated(
    since="0.0.10", 
    removal="0.2.0", 
    alternative_import="langchain_openai.ChatOpenAI"
)
class ChatOpenAI(BaseChatModel):

The correct usage of the class can be found in the langchain-openai package, which (for some reasons...) does not come by default when installing LangChain from PyPI. The package is the following: OpenAI Integration.

You can directly install the package by running:

pip install langchain-openai

LangChain is continuously evolving so it is generally difficult to keep the pace of those changing. I suggest to always refer to the official documentation for understanding where to find classes and functions based on the specific version of LangChain installed.

Upvotes: 18

Ari
Ari

Reputation: 766

Following solution worked for me. I referred to the API docs.

from langchain_community.chat_models import ChatOpenAI
openai = ChatOpenAI(model_name="gpt-3.5-turbo")

Upvotes: 6

Related Questions