affaan-m/ECC · error · AuthenticationError

Ollama connection failed: {msg}

Error message

Ollama connection failed: {msg}

What it means

OllamaProvider.generate() raises AuthenticationError with the message 'Ollama connection failed: ...' whenever the underlying exception message contains '401' OR the word 'connection'. Ollama runs as a local HTTP server (default http://localhost:11434) with no API key, so this almost always indicates the server is unreachable — not real authentication. The '401' branch is effectively a red herring for local Ollama; connection failures are the common cause.

Source

Thrown at src/llm/providers/ollama.py:104

                tool_calls = [
                    ToolCall(
                        id=tc.get("id", ""),
                        name=tc.get("function", {}).get("name", ""),
                        arguments=tc.get("function", {}).get("arguments", {}),
                    )
                    for tc in result["message"]["tool_calls"]
                ]

            return LLMOutput(
                content=content,
                tool_calls=tool_calls,
                model=model,
                stop_reason=result.get("done_reason"),
            )
        except Exception as e:
            msg = str(e)
            if "401" in msg or "connection" in msg.lower():
                raise AuthenticationError(f"Ollama connection failed: {msg}", provider=ProviderType.OLLAMA) from e
            if "429" in msg or "rate_limit" in msg.lower():
                raise RateLimitError(msg, provider=ProviderType.OLLAMA) from e
            if "context" in msg.lower() and "length" in msg.lower():
                raise ContextLengthError(msg, provider=ProviderType.OLLAMA) from e
            raise

    def list_models(self) -> list[ModelInfo]:
        return self._models.copy()

    def validate_config(self) -> bool:
        return bool(self.base_url)

    def get_default_model(self) -> str:
        return self.default_model

View on GitHub (pinned to 01e15490f0)

Solutions

  1. Start Ollama: run 'ollama serve' or verify the systemd service is up.
  2. Confirm base_url with 'curl <base_url>/api/tags'.
  3. Check OLLAMA_HOST / base_url resolves and is reachable from the client process.
  4. Pull the requested model with 'ollama pull <model>' before calling generate().
  5. Remember this maps to AuthenticationError even for connection failures — catch AuthenticationError, not ConnectionError.

Example fix

// before
output = provider.generate(llm_input)

// after
import httpx
from llm.core.interface import AuthenticationError

def ensure_ollama(base_url: str) -> None:
    try:
        httpx.get(f"{base_url.rstrip('/')}/api/tags", timeout=5).raise_for_status()
    except Exception:
        import subprocess
        subprocess.Popen(["ollama", "serve"])

try:
    output = provider.generate(llm_input)
except AuthenticationError as e:
    if "Ollama connection failed" not in str(e):
        raise
    ensure_ollama(provider.base_url)
    output = provider.generate(llm_input)
Defensive patterns

Strategy: validation

Validate before calling

import httpx

def ollama_reachable(base_url: str, timeout: float = 5.0) -> bool:
    try:
        r = httpx.get(f"{base_url.rstrip('/')}/api/tags", timeout=timeout)
        return r.status_code == 200
    except Exception:
        return False

Type guard

from llm.core.interface import AuthenticationError

def is_ollama_conn_error(exc: BaseException) -> bool:
    return isinstance(exc, AuthenticationError) and "Ollama connection failed" in str(exc)

Try / catch

from llm.core.interface import AuthenticationError

try:
    output = provider.generate(llm_input)
except AuthenticationError as e:
    if "Ollama connection failed" not in str(e):
        raise  # real auth error, not a connection failure
    start_ollama()
    output = provider.generate(llm_input)

Prevention

When it happens

Trigger: Ollama daemon not running; base_url pointing at a dead/unreachable address; OLLAMA_HOST env var wrong; firewall blocking the port; urllib.error.URLError whose message contains 'connection'.

Common situations: Fresh install where 'ollama serve' was never started; base_url configured for a remote host that is down; Ollama bound to 127.0.0.1 but the client uses a container hostname; an SSH tunnel closed; missing the model after first install.

Related errors


AI-assisted analysis of affaan-m/ECC@01e15490f0 (2026-08-13). Data as JSON: /api/errors/a5cc3c26c28c679d. Report an issue: GitHub.