Ollama With LangChain
Use the ChatOllama wrapper to drop local models into LangChain and LangGraph agent pipelines.
TL;DR
- Wrap a local model with LangChain's
ChatOllamaclass. - Install the
langchain-ollamapackage before importing theChatOllamaclass. - Add tools to a model with
bind_toolsfor agents.
Install And Import
InstallAdd the dedicated LangChain Ollama package.
pip install langchain-ollamaImport ChatOllamaBring in the chat model wrapper.
from langchain_ollama import ChatOllamaEmbeddingsImport the embeddings class when needed.
from langchain_ollama import OllamaEmbeddingsBasic Usage
Create The ModelInstantiate ChatOllama with a local model name.
llm = ChatOllama(model="llama3.1")InvokeSend a prompt and get a message back.
resp = llm.invoke("Explain RAG briefly")Read ContentAccess the reply text on the content field.
print(resp.content)Tool Calling
bind_toolsAttach tools so the model can call them.
model = llm.bind_tools([get_weather])JSON ModeForce JSON output for structured parsing.
ChatOllama(model="llama3.1", format="json")TemperatureSet sampling options on the constructor.
ChatOllama(model="llama3.1", temperature=0)LangGraph Nodes
Same WrapperUse ChatOllama as the model inside a graph.
llm = ChatOllama(model="llama3.1")Bind In A NodeGive a graph node tool access via bind_tools.
node_llm = llm.bind_tools(tools)Swap ModelsChange one line to switch local models.
ChatOllama(model="qwen2.5-coder")Tips
- Use
ChatOllamaanywhere LangChain expects a chat model, so a local model swaps into existing chains and graphs unchanged. - Enable structured output with
format="json"orbind_tools, which helps smaller local models return parseable, reliable results.
Warnings
- Tool calling depends on the model; pick a tool-capable model like
llama3.1, orbind_toolsmay be ignored silently. - Point
ChatOllamaat a running server; withoutollama serve, calls fail with a connection error tolocalhost:11434.
In Practice
Wrap a local model, bind a tool, and let the model decide to call it in a LangChain flow.
- The
@tooldecorator turns a Python function into a callable tool. ChatOllamaruns the model locally with no API key.bind_toolsgives the model the tool's schema to call.- The reply's
tool_callslists what the model chose to run.
from langchain_ollama import ChatOllama
from langchain_core.tools import tool
@tool
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
llm = ChatOllama(model="llama3.1")
agent = llm.bind_tools([add])
resp = agent.invoke("What is 12 plus 30?")
print(resp.tool_calls)FAQ
Install langchain-ollama, import ChatOllama, and create it with a local model name: ChatOllama(model="llama3.1"). It behaves like any other LangChain chat model, so it fits existing chains.
Install the dedicated langchain-ollama package. Import the model with from langchain_ollama import ChatOllama, and OllamaEmbeddings from the same package when you need embeddings.
Yes, if the model supports tool calling. Attach tools with llm.bind_tools([...]) and use a capable model like llama3.1. The reply then exposes tool_calls describing what to run.
LangGraph uses the same ChatOllama object as the model inside a node. Bind tools with bind_tools, wire the node into your graph, and swap models by changing a single line.