An integration package connecting OpenAI and LangChain, providing classes for chat models (`ChatOpenAI`) and embeddings (`OpenAIEmbeddings`). It is actively maintained with frequent releases, typically minor versions every few months and patch releases more frequently, as part of the broader LangChain ecosystem.
pip install langchain-openaiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize `ChatOpenAI` and send a basic message. It assumes the `OPENAI_API_KEY` environment variable is set.
Update import statements from `from langchain...` to `from langchain_openai...` for OpenAI-specific components like `ChatOpenAI` and `OpenAIEmbeddings`.
Always ensure `langchain-core` is up-to-date by running `pip install -U langchain-openai langchain-core`.
For new Azure OpenAI integrations, use `langchain_openai.ChatOpenAI` with `base_url` set to your Azure endpoint (e.g., `https://{your-resource-name}.openai.azure.com/openai/v1/`) and provide the `api_key`. Migrate existing `AzureChatOpenAI` usage if a unified interface is desired.Set `check_embedding_ctx_length=False` in the `OpenAIEmbeddings` constructor (e.g., `OpenAIEmbeddings(model="your-model", base_url="https://your-api-endpoint.com", check_embedding_ctx_length=False)`).
Set the `LC_OUTPUT_VERSION` environment variable to `v0` or specify `output_version="v0"` when instantiating `ChatOpenAI` (e.g., `ChatOpenAI(model="gpt-4o-mini", output_version="v0")`).
Ensure the `OPENAI_API_KEY` environment variable is set with a valid OpenAI API key, or pass `api_key='your_key'` to the constructor of `ChatOpenAI` or `OpenAIEmbeddings`.
Ensure the `OPENAI_API_KEY` environment variable is set, or pass the `api_key` argument directly to the `ChatOpenAI` or `OpenAI` model constructor (e.g., `ChatOpenAI(model="gpt-4", api_key="YOUR_API_KEY")`).
Install the package using pip: `pip install langchain-openai`
Ensure your OpenAI API key is correctly set as an environment variable named `OPENAI_API_KEY` (e.g., `export OPENAI_API_KEY="your_key_here"` in your shell, or in a `.env` file loaded with `python-dotenv`). Alternatively, pass it directly to the `ChatOpenAI` or `OpenAIEmbeddings` constructor: `ChatOpenAI(api_key="your_key_here")`.
Upgrade your `openai` package to the latest version: `pip install --upgrade openai`. Ensure `langchain-openai` is also up-to-date: `pip install --upgrade langchain-openai`.
Rename your local `openai.py` file to something else (e.g., `my_openai_utils.py`) to avoid conflicts with the official `openai` package.