ollama Python library fails with httpx.ConnectError
httpx.ConnectErrorShort answer
httpx.ConnectError means the client could not open a connection to the address it used. Ollama listens on 127.0.0.1 port 11434 by default. Check the server answers there, point the Python Client at the real host if it runs elsewhere, and set OLLAMA_HOST on the server when a container or another machine must reach it.
Fix it fast
1Find out which cause you have
Ask the server directly, from the same machine and shell as your Python code. If this fails, nothing is listening there.
curl http://localhost:11434/api/generate -d '{"model": "mistral"}'2Match what you see, then jump to its fix
- 1
curl on localhost also failsThe Ollama server is not running at the address the client usesFix - 2
Ollama runs on another machine or port, and the code uses the default clientThe client uses a different host than the serverFix - 3
Your code runs in Docker or on another computer, and the server runs on the hostThe server only accepts local connectionsFix
- 1
What does the error look like?
httpx.ConnectError
Cause 1 of 3
The Ollama server is not running at the address the client uses
1Check
The Ollama FAQ says it listens on 127.0.0.1 port 11434. ollama ps talks to the same server, so it fails when the server is down.
ollama ps2Fix
Start the Ollama app, or restart the service on Linux, then repeat the curl call.
sudo systemctl restart ollama3Verify
curl returns a JSON reply.
curl http://localhost:11434/api/generate -d '{"model": "mistral"}'Confirmed by docs.ollama.com
Cause 2 of 3
The client uses a different host than the server
1Check
The ollama-python README shows creating a Client with an explicit host, and says extra arguments go to httpx.Client.
grep -rn "Client(\|ollama\." --include=*.py .2Fix
Create a client with the real address and use it for every call.
from ollama import Client
client = Client(host="http://192.168.1.50:11434")
print(client.list())3Verify
The call returns the model list.
python app.pyConfirmed by raw.githubusercontent.com
Cause 3 of 3
The server only accepts local connections
1Check
By default Ollama binds to 127.0.0.1, which other machines and containers cannot reach. The FAQ says to change the bind address with the OLLAMA_HOST environment variable.
ss -ltn | grep 114342Fix
On Linux with systemd, add the variable to the service, reload and restart.
sudo systemctl edit ollama.service
# add under [Service]:
# Environment="OLLAMA_HOST=0.0.0.0:11434"
sudo systemctl daemon-reload
sudo systemctl restart ollama3Verify
From the other machine or container, request the server address.
curl http://HOST_IP:11434/api/generate -d '{"model": "mistral"}'Confirmed by docs.ollama.com
How do I stop it coming back?
- Keep the Ollama address in one setting, not scattered through the code.
- On macOS and Windows set OLLAMA_HOST the way the FAQ describes, then restart the app.
- Expose the port only to networks you trust, and keep it closed to the internet.
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 built on the same technologies:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
AI
Open WebUI or another Docker app cannot connect to Ollama
Connect call failed ('127.0.0.1', 11434)
Inside a container, localhost is the container itself, not your machine, so a call to 127.0.0.1:11434 never reaches Ollama.
3 causes, each with a fixAI
TextLoader fails on a text file with an encoding error
UnicodeDecodeError: codec can't decode byte
TextLoader opens the file with the encoding you give it, or the system default if you give none.
3 causes, each with a fixAI
LlamaIndex cannot import Ollama from llama_index.llms
ImportError: cannot import name 'Ollama' from 'llama_index.llms' (unknown location)
Since LlamaIndex 0.10, each integration is its own package and the old top-level llama_index.llms path is gone.
3 causes, each with a fixAI
LlamaIndex gets 404 Not Found from Ollama at /api/chat
Client error '404 Not Found' for url 'http://localhost:11434/api/chat'
Ollama's error docs describe 404 as a resource that was not found, such as a model that does not exist.
2 causes, each with a fixAI
The shell says ollama: command not found
command not found: ollama
The shell can only run ollama if the binary is installed in the same place the shell runs.
2 causes, each with a fixAI
Ollama is slow and the GPU is not being used
Replies arrive a few tokens per second on a machine with a GPU
Run ollama ps and read the PROCESSOR column: 100% GPU means the model is on the card, anything with CPU means it is not fully there.
4 causes, each with a fix