The correct way to install LangChain with OpenAI support is using the extras syntax: pip install langchain[openai]. Other options are invalid or install unrelated packages.
from langchain import OpenAI llm = OpenAI() print(llm("Hello, LangChain!"))
The OpenAI class is a LangChain wrapper for OpenAI's language model. Calling it with a prompt returns the model's generated text. So the code prints the model's response.
LangChain expects the OpenAI API key to be set in the environment variable OPENAI_API_KEY. Setting it via os.environ is the correct approach. Other options are invalid or unsupported.
ModuleNotFoundError: No module named 'langchain_openai'. What is the most likely cause?from langchain import OpenAI llm = OpenAI()
A ModuleNotFoundError means Python cannot find the package. This usually means LangChain was not installed in the environment you are using.
OPENAI_API_KEY is not set?
```python
from langchain import OpenAI
llm = OpenAI()
llm("Test prompt")
```from langchain import OpenAI llm = OpenAI() llm("Test prompt")
If the API key is missing, the OpenAI client raises an authentication error when making a request. LangChain does not provide a default key or silent failure.