High severity beginner · Fix: 2-5 min

ConnectionError

requests.exceptions.ConnectionError

What this error means
The ChatOllama client failed to connect to the Ollama local or remote server, causing a network connection error.

Stack trace

traceback
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'))
QUICK FIX
Ensure the Ollama server is running and reachable at the configured host and port before starting your LangChain ChatOllama client.

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

1

Ollama server is not running locally or remotely on the expected host and port

✓ Fix

Start the Ollama server on the correct host and port (default localhost:11434) before running your LangChain ChatOllama client.

2

Incorrect host or port specified in the ChatOllama client configuration

✓ Fix

Verify and correct the host and port parameters passed to ChatOllama to match the running Ollama server.

3

Firewall or network security settings blocking the connection to Ollama server port

✓ Fix

Configure firewall or security groups to allow inbound and outbound traffic on the Ollama server port.

4

Using HTTPS connection without proper SSL setup when Ollama server expects HTTP

✓ Fix

Ensure the protocol (http vs https) matches the Ollama server configuration or disable SSL verification if testing locally.

Code: broken vs fixed

Broken - triggers the error
python
from langchain.chat_models import ChatOllama

chat = ChatOllama(model='llama2')
response = chat.predict('Hello')  # This line raises ConnectionError if server is down
print(response)
Fixed - works correctly
python
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)
Added explicit host and port parameters matching the running Ollama server and ensured environment variables configure connection properly.

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.

Python 3.9+ · langchain >=0.2.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.