Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
AI and LLM integration

Python cannot import Ollama from langchain.llms any more

Your error

ImportError: cannot import name 'Ollama' from 'langchain.llms'2 more lines it can show

Fix it fast

Most common cause, 1 of 2

LangChain 1.x removed the langchain.llms module

Install the partner package that LangChain's Ollama page names and import its class.

Fix
pip install -U langchain-ollama

# app.py
from langchain_ollama import OllamaLLM

llm = OllamaLLM(model="llama3.1")
print(llm.invoke("Say hello in five words."))
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

ImportError: cannot import name 'Ollama' from 'langchain.llms' shows up because the Ollama wrapper left the langchain package. LangChain 1.x has no langchain.llms module at all, and the langchain_community Ollama class is deprecated inside a package that is being sunset. Install langchain-ollama and use from langchain_ollama import OllamaLLM, or ChatOllama for chat models. Applies to langchain 1.5 and langchain-ollama 1.1.

  • Applies to langchain 1.5.2, langchain-ollama 1.1.0, langchain-community 0.4.2, Python 3.11+
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

List the LangChain packages in the environment that runs the script. langchain 1.x with no langchain-ollama is cause 1; an import that already reads from langchain_community is cause 2.

Run this first
python -m pip list | grep -iE "langchain|ollama"
grep -rnE "from langchain(_community)?\.llms import .*Ollama" --include=*.py .

Then find the line you see in this list and open its fix:

  1. 1LangChain 1.x removed the langchain.llms modulepip list shows langchain 1.x, and the failing line is from langchain.llms import Ollama
  2. 2The code moved to langchain_community, which is being sunsetThe import is from langchain_community.llms import Ollama, and the run prints a DeprecationWarning about langchain-community plus a LangChainDeprecationWarning for Ollama

What does the error look like?

  • ImportError: cannot import name 'Ollama' from 'langchain.llms'
  • ModuleNotFoundError: No module named 'langchain.llms'
  • LangChainDeprecationWarning: The class `Ollama` was deprecated in LangChain 0.3.1 and will be removed in 1.0.0.

Cause 1 of 2

LangChain 1.x removed the langchain.llms module

You seepip list shows langchain 1.x, and the failing line is from langchain.llms import Ollama

Check

The import error appears because LangChain 1.x cut its namespace down to agents, messages, tools, chat_models and embeddings, so langchain.llms no longer exists in the package. Old tutorials and notebooks still use that path.

Check
python -c "import langchain; print(langchain.__version__)"

Fix

Install the partner package that LangChain's Ollama page names and import its class. OllamaLLM is the text-completion wrapper; the same docs note that many Ollama models are chat models, which ChatOllama serves.

Fix
pip install -U langchain-ollama

# app.py
from langchain_ollama import OllamaLLM

llm = OllamaLLM(model="llama3.1")
print(llm.invoke("Say hello in five words."))

Verify

The script imports without an error and prints a reply from the local Ollama server.

Verify
python app.py

Confirmed by docs.langchain.com

Did this fix it?

Cause 2 of 2

The code moved to langchain_community, which is being sunset

You seeThe import is from langchain_community.llms import Ollama, and the run prints a DeprecationWarning about langchain-community plus a LangChainDeprecationWarning for Ollama

Check

The langchain_community Ollama import still works, but it rests on two retirements. The Ollama class has been deprecated since 0.3.1 with langchain_ollama.OllamaLLM as its named replacement, and in May 2026 LangChain announced that the whole langchain-community package is being sunset with immediate effect, pointing users to standalone integration packages.

Check
python -W always -c "from langchain_community.llms import Ollama; Ollama(model='llama3.1')"

Fix

Swap the import and the class name. The model argument stays the same; for chat-style messages, build a ChatOllama instead.

Fix
# before
# from langchain_community.llms import Ollama
# llm = Ollama(model="llama3.1")

from langchain_ollama import ChatOllama

chat = ChatOllama(model="llama3.1")
print(chat.invoke("Name one planet.").content)

Verify

With deprecation warnings turned into errors, the script still runs and prints the reply, because nothing from langchain_community is imported any more.

Verify
python -W error::DeprecationWarning app.py

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Pin langchain and langchain-ollama together in requirements.txt, since a LangChain major release moves imports.
  • Use the same model tag in ollama pull and in OllamaLLM or ChatOllama, as the integration docs advise, and list it in the README.
  • Run the test suite once per release with python -W error::DeprecationWarning, so a deprecated LangChain class fails a build before it is deleted.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.