{"record":{"id":"99ce4ccdc924add0","repo":"Fosowl/agenticSeek","slug":"ollama-connection-refused-at-host-is-the-server","errorCode":null,"errorMessage":"Ollama connection refused at {host}. Is the server running?","messagePattern":"Ollama connection refused at (.+?)\\. Is the server running\\?","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"sources/llm_provider.py","lineNumber":198,"sourceCode":"                model=self.model,\n                messages=history,\n                stream=True,\n            )\n            for chunk in stream:\n                if verbose:\n                    print(chunk[\"message\"][\"content\"], end=\"\", flush=True)\n                thought += chunk[\"message\"][\"content\"]\n        except httpx.ConnectError as e:\n            raise Exception(\n                f\"\\nOllama connection failed at {host}. Check if the server is running.\"\n            ) from e\n        except Exception as e:\n            if hasattr(e, 'status_code') and e.status_code == 404:\n                animate_thinking(f\"Downloading {self.model}...\")\n                client.pull(self.model)\n                return self.ollama_fn(history, verbose)\n            if \"refused\" in str(e).lower():\n                raise Exception(\n                    f\"Ollama connection refused at {host}. Is the server running?\"\n                ) from e\n            raise e\n\n        return thought\n\n    def huggingface_fn(self, history, verbose=False):\n        \"\"\"\n        Use huggingface to generate text.\n        \"\"\"\n        from huggingface_hub import InferenceClient\n        client = InferenceClient(\n            api_key=self.get_api_key(\"huggingface\")\n        )\n        completion = client.chat.completions.create(\n            model=self.model,\n            messages=history,\n            max_tokens=1024,","sourceCodeStart":180,"sourceCodeEnd":216,"githubUrl":"https://github.com/Fosowl/agenticSeek/blob/ae57a2357745a9706cb12d0fd76d954c84d166fa/sources/llm_provider.py#L180-L216","documentation":"This error is raised by ollama_fn (sources/llm_provider.py:198) when the HTTP request to the Ollama server fails with a 'connection refused'-style error that is not a plain httpx.ConnectError (that case is handled separately at line 188). The library detects the word 'refused' in the underlying exception string and re-raises it with the target host, meaning the TCP connection reached the machine but nothing is listening on the Ollama port. This is a server-side availability problem, not a client code bug.","triggerScenarios":"client.chat(model=..., messages=..., stream=True) is called against host (e.g. http://localhost:11434) and the underlying exception message contains 'refused' — i.e. no process is bound to that port. Note it is only reachable for exceptions that are not httpx.ConnectError but still carry 'refused' (some Ollama client/httpx versions wrap connect failures differently), and not for 404 responses (model not pulled), which are auto-retried via client.pull.","commonSituations":"Ollama daemon not started (forgot `ollama serve`); wrong host/port in config.ini (e.g. port 11434 vs a custom OLLAMA_HOST); running inside Docker where 'localhost' points at the container instead of the host (the library has an internal_url for docker, but a misconfigured server_address still refuses); server crashed or stopped mid-session; firewall/proxy rejecting the port on a remote server.","solutions":["Start the Ollama server: run `ollama serve` (or start the Ollama desktop app) and verify it responds, e.g. `curl http://localhost:11434/api/tags`.","Check the host/port in config.ini: the address must point where Ollama listens (default port 11434); set OLLAMA_HOST if you use a non-default port.","If the app runs in Docker, use the docker-appropriate address (host.docker.internal or the host IP) instead of localhost, matching the library's internal_url handling.","If connecting to a remote Ollama server, confirm the server is reachable from your machine and the port is open (firewall/security group rules).","Restart the Ollama service if it crashed; check its logs for bind errors (port already in use, etc.)."],"exampleFix":"// before (config.ini)\nserver_address = localhost:11433\n\n// after\nserver_address = localhost:11434  ; or start ollama with OLLAMA_HOST=0.0.0.0:11433","handlingStrategy":"validation","validationCode":"import socket\n\ndef assert_ollama_reachable(host=\"localhost\", port=11434, timeout=2):\n    try:\n        with socket.create_connection((host, port), timeout=timeout):\n            return True\n    except OSError as e:\n        raise RuntimeError(\n            f\"Ollama not reachable at {host}:{port} ({e}). Run `ollama serve` first.\"\n        ) from e\n\nassert_ollama_reachable()  # call before constructing the provider / ollama_fn","typeGuard":"def is_connection_refused(err: BaseException) -> bool:\n    \"\"\"Narrow arbitrary provider exceptions down to the connection-refused case.\"\"\"\n    return isinstance(err, Exception) and \"refused\" in str(err).lower()","tryCatchPattern":"import time\n\ndef safe_ollama_fn(provider, history, retries=3, delay=1.0):\n    for attempt in range(retries):\n        try:\n            return provider.ollama_fn(history)\n        except Exception as e:\n            if \"refused\" in str(e).lower() and attempt < retries - 1:\n                time.sleep(delay * (attempt + 1))  # give the server time to start\n                continue\n            raise RuntimeError(\n                \"Ollama server is not running. Start it with `ollama serve`.\"\n            ) from e","preventionTips":["Run a preflight TCP check (socket.create_connection) against the Ollama host/port before making LLM calls.","Start Ollama as a systemd/launchd service so it survives reboots instead of relying on a manual `ollama serve`.","Keep the port consistent (default 11434) between OLLAMA_HOST and config.ini's server_address.","In Docker, use host.docker.internal or the host IP, never localhost, to reach an Ollama server on the host.","Health-check http://<host>:11434/api/tags at app startup and fail fast with a clear setup message."],"tags":["network","connection-refused","ollama","server-unavailable","configuration"],"backgroundTag":"connection-refused","analyzedSha":"ae57a2357745a9706cb12d0fd76d954c84d166fa","analyzedAt":"2026-08-30T02:49:05.834Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}