langchain-ai/langchain · error · ImportError

Could not import {module_name} python package. Please instal

Error message

Could not import {module_name} python package. Please install it with `pip install {pip_name}`.

What it means

Raised by `import_module` in `langchain_core.utils.utils` when `importlib.import_module` fails: the requested module (an optional dependency) is not installed in the current environment. The message tells you the exact `pip install <package>` command to run, mapping underscores to hyphens (e.g. `langchain_openai` → `pip install langchain-openai`).

Source

Thrown at libs/core/langchain_core/utils/utils.py:142

        module_name: The name of the module to import.
        pip_name: The name of the module to install with pip.
        package: The package to import the module from.

    Returns:
        The imported module.

    Raises:
        ImportError: If the module is not installed.
    """
    try:
        module = importlib.import_module(module_name, package)
    except (ImportError, ModuleNotFoundError) as e:
        pip_name = pip_name or module_name.split(".", maxsplit=1)[0].replace("_", "-")
        msg = (
            f"Could not import {module_name} python package. "
            f"Please install it with `pip install {pip_name}`."
        )
        raise ImportError(msg) from e
    return module


def check_package_version(
    package: str,
    lt_version: str | None = None,
    lte_version: str | None = None,
    gt_version: str | None = None,
    gte_version: str | None = None,
) -> None:
    """Check the version of a package.

    Args:
        package: The name of the package.
        lt_version: The version must be less than this.
        lte_version: The version must be less than or equal to this.
        gt_version: The version must be greater than this.
        gte_version: The version must be greater than or equal to this.

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Run the exact command from the message: `pip install <pip_name>` (or `uv add`/`uv sync --group ...` in this monorepo).
  2. If it is already installed, verify you are running the same interpreter/venv: `python -c "import sys; print(sys.executable)"`.
  3. In Docker/CI, add the optional dependency to the image/environment definition.
  4. Check the chained exception (`raise ... from e`) for a nested ImportError indicating a broken install — reinstall the package.

Example fix

# before
# ImportError: Could not import langchain_openai python package. Please install it with `pip install langchain-openai`.
from langchain_openai import ChatOpenAI

# after (terminal)
# pip install langchain-openai
from langchain_openai import ChatOpenAI
Defensive patterns

Strategy: try-catch

Validate before calling

import importlib.util

def module_available(module_name: str) -> bool:
    return importlib.util.find_spec(module_name) is not None

if not module_available("langchain_openai"):
    raise RuntimeError("this feature requires: pip install langchain-openai")

Try / catch

try:
    from langchain_core.utils.utils import import_module
    mod = import_module("langchain_openai")
except ImportError as e:
    raise RuntimeError("optional dependency missing; run: pip install langchain-openai") from e

Prevention

When it happens

Trigger: Using an integration or utility that lazily imports an optional package — e.g. a chat model trying to import `langchain_openai`, `aiohttp`, `tiktoken` — while that package is absent from the venv. Also raised when the package is installed but its own imports fail inside a broken environment (the original ImportError is chained).

Common situations: Installing only `langchain-core` and using partner integrations; multiple virtualenvs/interpreters (package installed in one, app runs in another); Docker images trimmed of optional deps; version conflicts where the package's transitive import fails; typos in the module name passed by custom code.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/556ce043c2be3ad7. Report an issue: GitHub.