ConnectionError
requests.exceptions.ConnectionError
Stack trace
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=11434): Max retries exceeded with url: /chat (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f8c4a2d7d30>: Failed to establish a new connection: [Errno 111] Connection refused')) Why it happens
This error occurs when the ChatOllama client cannot establish a network connection to the Ollama server. Common causes include the Ollama server not running, incorrect host or port configuration, firewall blocking, or network issues preventing socket connection.
Detection
Monitor connection attempts to the Ollama server and catch requests.exceptions.ConnectionError to log connection failures before the app crashes.
Causes & fixes
Ollama server is not running locally or remotely on the expected host and port
Start the Ollama server on the correct host and port (default localhost:11434) before running your LangChain ChatOllama client.
Incorrect host or port specified in the ChatOllama client configuration
Verify and correct the host and port parameters passed to ChatOllama to match the running Ollama server.
Firewall or network security settings blocking the connection to Ollama server port
Configure firewall or security groups to allow inbound and outbound traffic on the Ollama server port.
Using HTTPS connection without proper SSL setup when Ollama server expects HTTP
Ensure the protocol (http vs https) matches the Ollama server configuration or disable SSL verification if testing locally.
Code: broken vs fixed
from langchain.chat_models import ChatOllama
chat = ChatOllama(model='llama2')
response = chat.predict('Hello') # This line raises ConnectionError if server is down
print(response) import os
from langchain.chat_models import ChatOllama
# Ensure Ollama server is running locally on port 11434
os.environ['OLLAMA_HOST'] = 'localhost'
os.environ['OLLAMA_PORT'] = '11434'
chat = ChatOllama(model='llama2', host=os.environ['OLLAMA_HOST'], port=int(os.environ['OLLAMA_PORT']))
response = chat.predict('Hello') # Fixed: connect to running Ollama server
print(response) Workaround
Wrap ChatOllama calls in try/except requests.exceptions.ConnectionError, log the error, and retry connection after a delay or fallback to a cached response.
Prevention
Implement health checks for the Ollama server before client requests and use robust retry logic with exponential backoff to handle transient connection issues.