{"id":2562,"library":"langchain-ollama","title":"LangChain Ollama Integration","description":"langchain-ollama is an integration package that connects LangChain, a framework for developing applications powered by language models, with Ollama, a platform for running open-source large language models (LLMs) locally. This allows developers to build AI applications that leverage local LLMs for enhanced privacy, reduced costs, and offline capabilities. The library is actively maintained with frequent releases, currently at version 1.1.0, requiring Python versions >=3.10.0 and <4.0.0.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/langchain-ai/langchain","tags":["LLM","Ollama","LangChain","embedding","local-models","chat-model","text-generation"],"install":[{"cmd":"pip install langchain-ollama","lang":"bash","label":"Install Python package"},{"cmd":"curl -fsSL https://ollama.com/install.sh | sh\nollama pull llama3","lang":"bash","label":"Install Ollama server and pull a model (Prerequisite)"}],"dependencies":[{"reason":"Core components for LangChain integrations.","package":"langchain-core","optional":false},{"reason":"Required to run local LLMs that langchain-ollama connects to. Not a Python dependency, but a system prerequisite.","package":"ollama (server/cli)","optional":false}],"imports":[{"note":"As of recent versions, ChatOllama moved from langchain_community to langchain_ollama for direct integration, though langchain_community might still expose older or aliased versions.","wrong":"from langchain_community.chat_models import ChatOllama","symbol":"ChatOllama","correct":"from langchain_ollama import ChatOllama"},{"note":"The canonical LLM import for text completion is now directly from langchain_ollama.llms.OllamaLLM, not the legacy Ollama from langchain_community.llms.","wrong":"from langchain_community.llms import Ollama","symbol":"OllamaLLM","correct":"from langchain_ollama.llms import OllamaLLM"},{"note":"OllamaEmbeddings has moved to langchain_ollama.embeddings for direct integration in newer versions.","wrong":"from langchain_community.embeddings import OllamaEmbeddings","symbol":"OllamaEmbeddings","correct":"from langchain_ollama.embeddings import OllamaEmbeddings"}],"quickstart":{"code":"import os\nfrom langchain_ollama import ChatOllama\nfrom langchain_core.prompts import ChatPromptTemplate\nfrom langchain_core.output_parsers import StrOutputParser\n\n# Ensure Ollama server is running and 'llama3' model is pulled (e.g., `ollama pull llama3`)\n# You can check Ollama status at http://localhost:11434/\n\n# Instantiate the ChatOllama model\n# You can specify base_url if Ollama is not on default localhost:11434\nllm = ChatOllama(model=\"llama3\", base_url=os.environ.get(\"OLLAMA_BASE_URL\", \"http://localhost:11434\"))\n\n# Define a prompt template\nprompt = ChatPromptTemplate.from_messages([\n    (\"system\", \"You are a helpful AI assistant. Answer the user's questions truthfully.\"),\n    (\"user\", \"{question}\")\n])\n\n# Create a simple chain\nchain = prompt | llm | StrOutputParser()\n\n# Invoke the chain\nresponse = chain.invoke({\"question\": \"What is the capital of France?\"})\nprint(response)\n\n# Example for embeddings\nfrom langchain_ollama.embeddings import OllamaEmbeddings\nembeddings_model = OllamaEmbeddings(model=\"llama3\", base_url=os.environ.get(\"OLLAMA_BASE_URL\", \"http://localhost:11434\"))\n\nquery_vector = embeddings_model.embed_query(\"What is the largest city in France?\")\nprint(f\"Embedding vector length: {len(query_vector)}\")\n","lang":"python","description":"This quickstart demonstrates how to set up a `ChatOllama` instance to interact with a local LLM (e.g., Llama 3) via the Ollama server. It includes a basic chat prompt template and uses `StrOutputParser` for clean output. It also shows how to instantiate `OllamaEmbeddings` to generate vector embeddings. Before running, ensure the Ollama server is installed and running, and the specified model (e.g., 'llama3') has been pulled using `ollama pull <model_name>`."},"warnings":[{"fix":"Update your import statements to use `from langchain_ollama import ...` for the latest functionality and stability. For embeddings, use `from langchain_ollama.embeddings import OllamaEmbeddings`.","message":"The primary import paths for `ChatOllama`, `OllamaLLM`, and `OllamaEmbeddings` have shifted from `langchain_community` to `langchain_ollama` (e.g., `from langchain_ollama import ChatOllama`). Older code relying on `langchain_community` imports may break or use deprecated versions.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Install Ollama from https://ollama.com and run `ollama pull <model_name>` for your chosen model(s). Ensure the Ollama server is active before running your Python application.","message":"The `ollama` server application must be installed and running, and the desired LLM model (e.g., 'llama3') must be pulled using `ollama pull <model_name>` via the command line before `langchain-ollama` can connect to it. Failure to do so will result in connection or model not found errors.","severity":"gotcha","affected_versions":"*"},{"fix":"For complex agentic workflows requiring robust tool use, consider testing thoroughly and potentially simplifying tool definitions or using structured output parsers for more reliable JSON parsing. For critical production systems with advanced tool calling, evaluate if alternative LLM providers offer more native support.","message":"Ollama's native tool calling support is less mature compared to providers like OpenAI. When implementing agents that use tools, `langchain-ollama` often relies on JSON-based agent workarounds, which might have a lower success rate for complex tasks than native function calling.","severity":"gotcha","affected_versions":"*"},{"fix":"Pass the correct `base_url` parameter to `ChatOllama`, `OllamaLLM`, or `OllamaEmbeddings` constructors. It's recommended to use an environment variable for flexibility (e.g., `os.environ.get('OLLAMA_BASE_URL', 'http://localhost:11434')`).","message":"Connecting to an Ollama instance not running on the default `http://localhost:11434` requires explicitly setting the `base_url` parameter during instantiation (e.g., `ChatOllama(base_url='http://your-ollama-host:port')`). Incorrect configuration can lead to connection errors.","severity":"gotcha","affected_versions":"*"},{"fix":"Migrate your `OllamaEmbeddings` imports to `from langchain_ollama.embeddings import OllamaEmbeddings` to use the current and supported version.","message":"The `OllamaEmbeddings` class in `langchain_community` was officially deprecated in favor of `langchain_ollama.embeddings.OllamaEmbeddings` around version `0.3.1` of `langchain-community`, with removal planned for `1.0.0` of `langchain-community`.","severity":"deprecated","affected_versions":"langchain-community >=0.3.1"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}