{"id":654,"library":"langchain-openai","title":"OpenAI Integration for LangChain","description":"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.","status":"active","version":"1.1.12","language":"python","source_language":"en","source_url":"https://github.com/langchain-ai/langchain","tags":["langchain","openai","llm","embeddings","chat"],"install":[{"cmd":"pip install langchain-openai","lang":"bash","label":"Install package"}],"dependencies":[{"reason":"Core LangChain functionalities; required for all LangChain integrations.","package":"langchain-core","optional":false},{"reason":"Official OpenAI Python client library.","package":"openai","optional":false},{"reason":"Used for token counting with OpenAI models.","package":"tiktoken"}],"imports":[{"note":"As of LangChain v1, `ChatOpenAI` is part of the `langchain-openai` partner package, not `langchain.chat_models` (which is for older v0 usage).","wrong":"from langchain.chat_models import ChatOpenAI","symbol":"ChatOpenAI","correct":"from langchain_openai import ChatOpenAI"},{"note":"As of LangChain v1, `OpenAIEmbeddings` is part of the `langchain-openai` partner package, not `langchain.embeddings` (which is for older v0 usage).","wrong":"from langchain.embeddings import OpenAIEmbeddings","symbol":"OpenAIEmbeddings","correct":"from langchain_openai import OpenAIEmbeddings"}],"quickstart":{"code":"import os\nfrom langchain_openai import ChatOpenAI\nfrom langchain_core.messages import HumanMessage\n\n# Set your OpenAI API key as an environment variable (recommended)\n# Or pass it directly: ChatOpenAI(openai_api_key=\"your_api_key\")\n\n# Ensure OPENAI_API_KEY is set in your environment\nif not os.environ.get(\"OPENAI_API_KEY\"): \n    os.environ[\"OPENAI_API_KEY\"] = os.environ.get(\"OPENAI_API_KEY\", \"\") # Replace with actual key or prompt user\n    # For interactive use, you might use: \n    # import getpass \n    # os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"Enter your OpenAI API key: \")\n\n# Instantiate the ChatOpenAI model\nchat_model = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0.7)\n\n# Invoke the model\nresponse = chat_model.invoke([HumanMessage(content=\"Tell me a short story about a brave knight.\")])\n\nprint(response.content)","lang":"python","description":"This quickstart demonstrates how to initialize `ChatOpenAI` and send a basic message. It assumes the `OPENAI_API_KEY` environment variable is set."},"warnings":[{"fix":"Update import statements from `from langchain...` to `from langchain_openai...` for OpenAI-specific components like `ChatOpenAI` and `OpenAIEmbeddings`.","message":"LangChain v1 introduced a new package structure. Most OpenAI-related classes moved from the monolithic `langchain` package (e.g., `langchain.chat_models.ChatOpenAI`) to the dedicated `langchain-openai` partner package (e.g., `langchain_openai.ChatOpenAI`). Code using old import paths will break.","severity":"breaking","affected_versions":"All versions >= 1.0.0"},{"fix":"Always ensure `langchain-core` is up-to-date by running `pip install -U langchain-openai langchain-core`.","message":"Compatibility with `langchain-core` is crucial. The `langchain-openai` package frequently bumps its minimum required `langchain-core` version. Running `langchain-openai` with an outdated `langchain-core` can lead to `AttributeError`s or unexpected behavior due to schema drifts or missing fields. [cite: 1.1.12 release notes]","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"For Azure OpenAI, `ChatOpenAI` now supports the v1 API directly using `base_url` and `api_key` parameters since `langchain-openai>=1.0.1`. The legacy `AzureChatOpenAI` class is still available but `ChatOpenAI` is the recommended unified approach for the v1 API.","severity":"breaking","affected_versions":"Versions < 1.0.1"},{"fix":"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)`).","message":"When using `OpenAIEmbeddings` with non-OpenAI compatible APIs (e.g., OpenRouter, Ollama, vLLM via `base_url`), the default `check_embedding_ctx_length` might cause errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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\")`).","message":"When interacting with the OpenAI Responses API, `langchain-openai` now defaults to storing response items in message content. To restore the previous (v0) behavior, users need to explicitly opt-in.","severity":"deprecated","affected_versions":"Versions >= 1.0.0"},{"fix":"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`.","message":"OpenAI API calls require an API key for authentication. This key must be provided either as an environment variable (e.g., `OPENAI_API_KEY`) or directly as an `api_key` parameter when instantiating `ChatOpenAI` or `OpenAIEmbeddings`. Failure to provide an API key results in an `AuthenticationError`.","severity":"breaking","affected_versions":"All versions"},{"fix":"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\")`).","message":"OpenAI API calls require an API key for authentication. If an API key is not provided (either via the `OPENAI_API_KEY` environment variable or explicitly in the model constructor), an `AuthenticationError` will occur, preventing any API interaction.","severity":"breaking","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-05-12T17:21:58.425Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Install the package using pip: `pip install langchain-openai`","cause":"The `langchain-openai` integration package is not installed in your Python environment. It's a separate package and not automatically included with `langchain` itself.","error":"ModuleNotFoundError: No module named 'langchain_openai'"},{"fix":"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\")`.","cause":"The OpenAI API key is either missing, incorrect, expired, or not accessible in the environment where `langchain-openai` is trying to use it. This often happens if the `OPENAI_API_KEY` environment variable is not set or is set with an invalid key, or if the key is hardcoded incorrectly.","error":"AuthenticationError: Incorrect API key provided: sk-..."},{"fix":"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`.","cause":"You are likely using an outdated version of the `openai` Python library (older than 1.0.0) which is incompatible with the version of `langchain-openai` you have installed. The `openai` library underwent a major refactor in version 1.0.0.","error":"ValidationError: 1 validation error for ChatOpenAI __root__ `openai` has no `ChatCompletion` attribute, this is likely due to an old version of the openai package. Try upgrading it with `pip install --upgrade openai`."},{"fix":"Rename your local `openai.py` file to something else (e.g., `my_openai_utils.py`) to avoid conflicts with the official `openai` package.","cause":"This specific error often occurs when you have a local Python file named `openai.py` in your project directory. Python's import mechanism prioritizes local files, causing it to try and import from your `openai.py` instead of the installed `openai` package, leading to a circular import when `langchain_openai` tries to import `openai`.","error":"ImportError: cannot import name 'ChatOpenAI' from partially initialized module 'langchain_openai' (most likely due to a circular import)"}],"ecosystem":"pypi","meta_description":null,"install_score":95,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":"1.2.1","install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":3.83,"mem_mb":41,"disk_size":"87.9M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":3.76,"mem_mb":39.8,"disk_size":"84.9M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":10,"import_time_s":2.85,"mem_mb":41,"disk_size":"96M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":2.71,"mem_mb":39.8,"disk_size":"93M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":4.62,"mem_mb":43.8,"disk_size":"94.7M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":5.05,"mem_mb":42.5,"disk_size":"91.4M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":8.8,"import_time_s":4.27,"mem_mb":43.7,"disk_size":"103M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":4.02,"mem_mb":42.5,"disk_size":"100M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":4.17,"mem_mb":43.1,"disk_size":"85.3M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":4.48,"mem_mb":41.8,"disk_size":"82.1M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":7.1,"import_time_s":4.24,"mem_mb":43.1,"disk_size":"94M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":4.71,"mem_mb":41.8,"disk_size":"90M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":3.98,"mem_mb":43.6,"disk_size":"85.1M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":4.19,"mem_mb":42.4,"disk_size":"81.7M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":7.3,"import_time_s":4.12,"mem_mb":43.6,"disk_size":"93M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":4.33,"mem_mb":42.4,"disk_size":"90M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":3.74,"mem_mb":40.5,"disk_size":"84.4M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":3.48,"mem_mb":40.5,"disk_size":"82.3M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":11.2,"import_time_s":3.41,"mem_mb":40.5,"disk_size":"92M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":3.1,"mem_mb":40.5,"disk_size":"90M"}]},"quickstart_checks":{"last_tested":"2026-04-24","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}