Fosowl/agenticSeek · critical · Exception

Ollama connection failed. is the server running ?

Error message

Ollama connection failed. is the server running ?

What it means

The Ollama HTTP client raised because nothing is listening on the local Ollama server endpoint (connection refused). The handler detects the word "refused" in the underlying exception and re-raises it as a human-readable message, chaining the original error. This happens before any model download/inference can occur, and the finally block still resets state.is_generating to False.

Source

Thrown at llm_server/sources/ollama_handler.py:42

            stream = ollama.chat(
                model=self.model,
                messages=history,
                stream=True,
            )
            for chunk in stream:
                content = chunk['message']['content']

                with self.state.lock:
                    if '.' in content:
                        self.logger.info(self.state.current_buffer)
                    self.state.current_buffer += content

        except Exception as e:
            if "404" in str(e):
                self.logger.info(f"Downloading {self.model}...")
                ollama.pull(self.model)
            if "refused" in str(e).lower():
                raise Exception("Ollama connection failed. is the server running ?") from e
            raise e
        finally:
            self.logger.info("Generation complete")
            with self.state.lock:
                self.state.is_generating = False

if __name__ == "__main__":
    generator = OllamaLLM()
    history = [
        {
            "role": "user",
            "content": "Hello, how are you ?"
        }
    ]
    generator.set_model("deepseek-r1:1.5b")
    generator.start(history)
    while True:
        print(generator.get_status())

View on GitHub (pinned to ae57a23577)

Solutions

  1. Start the Ollama server: run `ollama serve` (or start/enable the ollama systemd service) and confirm it listens on the expected port (default 11434).
  2. Verify connectivity: `curl http://localhost:11434` should respond before calling start().
  3. If Ollama runs on another host/port, set OLLAMA_HOST (or the client's host parameter) to the correct URL.
  4. Check firewall/container networking so the app can reach the Ollama endpoint.

Example fix

// before
generator.start(history)  # fails if ollama server is down
// after
import subprocess
subprocess.run(["ollama", "serve"], check=False)  # or ensure service is up
generator.start(history)
Defensive patterns

Strategy: retry

Validate before calling

import urllib.request
def ollama_reachable(url="http://localhost:11434") -> bool:
    try:
        urllib.request.urlopen(url, timeout=2)
        return True
    except Exception:
        return False
if not ollama_reachable():
    raise RuntimeError("Start the Ollama server first: `ollama serve`")

Try / catch

import time
for attempt in range(5):
    try:
        generator.start(history)
        break
    except Exception as e:
        if "Ollama connection failed" in str(e) and attempt < 4:
            time.sleep(2 ** attempt)
            continue
        raise

Prevention

When it happens

Trigger: Calling generate() (via start()) when the Ollama daemon is not running or the client cannot reach the Ollama host/port; the except branch matches if "refused" appears in str(e).lower().

Common situations: Ollama service not started (`ollama serve` not running); OLLAMA_HOST bound to a different port/interface than the client default; running in Docker/container without network access to the host Ollama; server crashed or still booting when the request fired.

Understand the failure class

Background: ECONNREFUSED and "connection refused" / "could not connect to server" errors: what they mean and how to fix them — this error's family across 44 libraries.

Related errors


AI-assisted analysis of Fosowl/agenticSeek@ae57a23577 (2026-08-30). Data as JSON: /api/errors/287f7a1b105f5573. Report an issue: GitHub.