ConnectionError
requests.exceptions.ConnectionError
Stack trace
requests.exceptions.ConnectionError: Failed to establish a new connection: [Errno 111] Connection refused During handling of the above exception, another exception occurred: ollama.client.OllamaConnectionError: Could not connect to Ollama server. Is 'ollama serve' running?
Why it happens
The Ollama client attempts to connect to the local Ollama server but fails because the server process is not running. This usually happens if 'ollama serve' was not started or the server crashed.
Detection
Catch ConnectionError exceptions when calling Ollama client methods and log the error message indicating the server is unreachable before retrying or alerting.
Causes & fixes
Ollama server process is not started
Run 'ollama serve' in a terminal to start the Ollama server before running your Python client code.
Ollama server crashed or was terminated unexpectedly
Restart the Ollama server by running 'ollama serve' again and monitor logs for stability.
Network or firewall blocking connection to Ollama server port
Ensure local firewall or network settings allow connections to the Ollama server port (usually localhost:11434).
Code: broken vs fixed
import ollama
response = ollama.chat(model="llama2", messages=[{"role": "user", "content": "Hello"}]) # Raises ConnectionError if server not running import ollama
# Ensure Ollama server is running by starting it in a separate terminal with 'ollama serve'
response = ollama.chat(model="llama2", messages=[{"role": "user", "content": "Hello"}]) # Now works if server is running
print(response) Workaround
Wrap Ollama client calls in try/except ConnectionError and prompt the user or script to start 'ollama serve' if the server is unreachable.
Prevention
Automate starting the Ollama server as part of your deployment or development workflow to guarantee the server is running before client requests.