{"record":{"id":"287f7a1b105f5573","repo":"Fosowl/agenticSeek","slug":"ollama-connection-failed-is-the-server-running","errorCode":null,"errorMessage":"Ollama connection failed. is the server running ?","messagePattern":"Ollama connection failed\\. is the server running \\?","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"critical","filePath":"llm_server/sources/ollama_handler.py","lineNumber":42,"sourceCode":"            stream = ollama.chat(\n                model=self.model,\n                messages=history,\n                stream=True,\n            )\n            for chunk in stream:\n                content = chunk['message']['content']\n\n                with self.state.lock:\n                    if '.' in content:\n                        self.logger.info(self.state.current_buffer)\n                    self.state.current_buffer += content\n\n        except Exception as e:\n            if \"404\" in str(e):\n                self.logger.info(f\"Downloading {self.model}...\")\n                ollama.pull(self.model)\n            if \"refused\" in str(e).lower():\n                raise Exception(\"Ollama connection failed. is the server running ?\") from e\n            raise e\n        finally:\n            self.logger.info(\"Generation complete\")\n            with self.state.lock:\n                self.state.is_generating = False\n\nif __name__ == \"__main__\":\n    generator = OllamaLLM()\n    history = [\n        {\n            \"role\": \"user\",\n            \"content\": \"Hello, how are you ?\"\n        }\n    ]\n    generator.set_model(\"deepseek-r1:1.5b\")\n    generator.start(history)\n    while True:\n        print(generator.get_status())","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/Fosowl/agenticSeek/blob/ae57a2357745a9706cb12d0fd76d954c84d166fa/llm_server/sources/ollama_handler.py#L24-L60","documentation":"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.","triggerScenarios":"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().","commonSituations":"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.","solutions":["Start the Ollama server: run `ollama serve` (or start/enable the ollama systemd service) and confirm it listens on the expected port (default 11434).","Verify connectivity: `curl http://localhost:11434` should respond before calling start().","If Ollama runs on another host/port, set OLLAMA_HOST (or the client's host parameter) to the correct URL.","Check firewall/container networking so the app can reach the Ollama endpoint."],"exampleFix":"// before\ngenerator.start(history)  # fails if ollama server is down\n// after\nimport subprocess\nsubprocess.run([\"ollama\", \"serve\"], check=False)  # or ensure service is up\ngenerator.start(history)","handlingStrategy":"retry","validationCode":"import urllib.request\ndef ollama_reachable(url=\"http://localhost:11434\") -> bool:\n    try:\n        urllib.request.urlopen(url, timeout=2)\n        return True\n    except Exception:\n        return False\nif not ollama_reachable():\n    raise RuntimeError(\"Start the Ollama server first: `ollama serve`\")","typeGuard":null,"tryCatchPattern":"import time\nfor attempt in range(5):\n    try:\n        generator.start(history)\n        break\n    except Exception as e:\n        if \"Ollama connection failed\" in str(e) and attempt < 4:\n            time.sleep(2 ** attempt)\n            continue\n        raise","preventionTips":["Run `ollama serve` (or enable the ollama service) as a prerequisite and health-check it at startup.","Set OLLAMA_HOST explicitly if the server is not on the default localhost:11434.","Probe the endpoint with a lightweight GET before each generation session.","In Docker, verify network reachability to the host Ollama and pin the host/port in config."],"tags":["network","connection","ollama","python"],"backgroundTag":"connection-refused","analyzedSha":"ae57a2357745a9706cb12d0fd76d954c84d166fa","analyzedAt":"2026-08-30T02:49:05.834Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}