Local model work has two boundaries to prove. The Ollama server must have a chat model ready, and the Python application must reach it through LangChain. Keeping that check small catches service, model-name, and package errors before the same local model is used inside an agent, chatbot, or retrieval flow.
The Python integration uses ChatOllama from the langchain-ollama package. It talks to the Ollama service at the configured base URL, normally http://localhost:11434, while the client handles the /api/chat endpoint path. The model name in the script must match a model already pulled into Ollama.
A short fixed prompt keeps the smoke test easy to read. A local model can vary in wording, so the clearest success signal is an AIMessage plus response content from the selected model rather than perfect natural-language compliance.
Related: How to install LangChain with pip
Related: How to call a chat model in LangChain
Related: How to generate Ollama embeddings in LangChain
$ ollama serve
Desktop and service installs may already keep Ollama running. Leave this foreground command open only when no background service is active.
$ ollama pull gpt-oss:20b
Use any Ollama chat model that fits the machine. The same model name is used later as OLLAMA_MODEL.
$ curl http://localhost:11434/api/tags
{"models":[{"name":"gpt-oss:20b","model":"gpt-oss:20b"}]}
The returned model name must match the value passed to ChatOllama. If the API does not respond, start Ollama before changing the Python script.
$ python3 -m pip install --upgrade langchain-ollama
langchain-ollama installs the ChatOllama integration and its LangChain Core dependency.
Related: How to install LangChain with pip
$ cat > connect_ollama_chat.py <<'PY'
import os
from langchain_ollama import ChatOllama
model = ChatOllama(
model=os.environ.get("OLLAMA_MODEL", "gpt-oss:20b"),
base_url=os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434"),
temperature=0,
max_retries=0,
)
response = model.invoke(
"Reply with exactly: Ollama chat model reached through LangChain."
)
print(type(response).__name__)
print(response.content)
PY
Set OLLAMA_BASE_URL only when Ollama listens on another host or port. Do not add /api to the base URL because the client builds the chat endpoint path.
$ python3 connect_ollama_chat.py AIMessage Ollama chat model reached through LangChain.
A local model may answer with slightly different wording. The connection is working when the script returns AIMessage and readable response content instead of a connection, model-name, or package import error.
$ rm connect_ollama_chat.py