Older Python examples can refer to modules that moved when a library changed its public API. In LangChain v1, ModuleNotFoundError: No module named 'langchain.chains' usually means a legacy chain import needs the separate langchain-classic package and its replacement import path.

The langchain package now concentrates on agent APIs, while legacy chains such as LLMChain and TransformChain live under langchain_classic.chains. Installing the compatibility package does not recreate langchain.chains, so the old import must change as well. Existing chain logic can remain in place while a broader migration is planned.

The repair applies to a project already running LangChain v1 with Python 3.10 or later. The terminal must use the same Python environment as the failing application; installing into another interpreter will leave the error unchanged. LLMChain still prints a deprecation warning after the import works; that warning is separate from the missing-module error, but it names 2.0.0 as the removal version, so the replacement import is a temporary route.

Steps to fix ModuleNotFoundError for langchain.chains in LangChain:

  1. Run your affected Python program in place of app.py to reproduce the missing-module error.
    $ python app.py
    Traceback (most recent call last):
      File "/home/user/project/app.py", line 1, in <module>
        from langchain.chains import TransformChain
    ModuleNotFoundError: No module named 'langchain.chains'

    The chain class in your traceback may instead be LLMChain or another legacy chain; the failing module name is the important signal.

  2. Inspect the interpreter and LangChain version used by that program.
    $ python -c "import sys, langchain; \
    print(sys.executable); print(langchain.__file__); \
    print(langchain.__version__)"
    /home/user/project/.venv/bin/python
    /home/user/project/.venv/lib/python3.12/site-packages/langchain/__init__.py
    1.4.9

    The paths and version vary by environment. A 1.x version loaded from the application's environment matches the package split. A local langchain.py or langchain/__init__.py path followed by an AttributeError for __version__ means that file is shadowing the package, so rename it before changing any import. If the application runs in an IDE or notebook, its configured interpreter or kernel must match the executable shown here.

  3. Install langchain-classic into the application's Python environment.

    Dependency resolution can change shared packages, so an existing project virtual environment and its recorded dependency versions provide a recovery boundary. The package comes from LangChain's PyPI distribution. The installation name contains a hyphen, while Python imports use an underscore. A dependency file such as requirements.txt also needs langchain-classic, or a fresh install stops at No module named 'langchain_classic'. Tested with langchain 1.4.9 and langchain-classic 1.0.8 on Python 3.12.

    $ python -m pip install langchain-classic
  4. Open the Python source file identified by the traceback in your text editor.

    For the traceback above, the file is app.py.

  5. Replace the legacy chain import with its langchain_classic equivalent, keeping your own class name.

    The old line in app.py is:

    from langchain.chains import TransformChain

    The replacement line is:

    from langchain_classic.chains import TransformChain

    A class such as LLMChain changes in the same way. The class name and chain code stay the same, but other langchain.* imports that v1 removed fail the same way at the retest. A deeper legacy path keeps its suffix, so langchain.chains.llm becomes langchain_classic.chains.llm. The package split is documented in the LangChain v1 migration reference.

  6. Save the edited Python source file.
  7. Confirm that your chain class imports from langchain_classic through the application's interpreter.
    $ python -c "from langchain_classic.chains import \
    TransformChain; print(TransformChain.__module__)"
    langchain_classic.chains.transform

    For another class, use its name in place of TransformChain. The module name proves that Python found the compatibility implementation. If langchain_classic is missing, the installation and execution environments still differ. An ImportError for your class means the compatibility package does not export it from langchain_classic.chains. In a notebook, a kernel restart may be needed after changing installed packages.

  8. Rerun your program in place of app.py with the same interpreter. Example output:
    $ python app.py
    langchain

    The result is your program's own. The example comes from this app.py, which lowercases its input.

    app.py
    from langchain_classic.chains import TransformChain
     
     
    def lowercase(inputs):
        return {"lower": inputs["text"].lower()}
     
     
    chain = TransformChain(
        input_variables=["text"],
        output_variables=["lower"],
        transform=lowercase,
    )
    print(chain.invoke({"text": "LangChain"})["lower"])

    A No module named error for another langchain.* module means that import was also removed in v1. For No module named 'langchain.prompts', import from langchain_core.prompts; the LangChain v1 migration covers the wider move. A traceback from another source file means that file needs the same edit. A provider authentication error, missing integration package, or LangChainDeprecationWarning line identifies a separate problem after the import has resolved.

Share this guide