{"record":{"id":"b1e02662b53942e6","repo":"BerriAI/litellm","slug":"ollamaexception-original-exception","errorCode":null,"errorMessage":"OllamaException: {original_exception}","messagePattern":"OllamaException: (.+?)","errorType":"exception","errorClass":"ServiceUnavailableError","httpStatus":503,"severity":"critical","filePath":"litellm/litellm_core_utils/exception_mapping_utils.py","lineNumber":1816,"sourceCode":"    custom_llm_provider: str,\n    error_str: str,\n    exception_type: str,\n    exception_provider: str,\n    extra_information: str,\n) -> None:\n    if isinstance(original_exception, dict):\n        error_str = original_exception.get(\"error\", \"\")\n    else:\n        error_str = str(original_exception)\n    if \"no such file or directory\" in error_str:\n        raise BadRequestError(\n            message=f\"OllamaException: Invalid Model/Model not loaded - {original_exception}\",\n            model=model,\n            llm_provider=\"ollama\",\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif \"Failed to establish a new connection\" in error_str:\n        raise ServiceUnavailableError(\n            message=f\"OllamaException: {original_exception}\",\n            llm_provider=\"ollama\",\n            model=model,\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif \"Invalid response object from API\" in error_str:\n        raise BadRequestError(\n            message=f\"OllamaException: {original_exception}\",\n            llm_provider=\"ollama\",\n            model=model,\n            response=getattr(original_exception, \"response\", None),\n        )\n    elif \"Read timed out\" in error_str:\n        raise Timeout(\n            message=f\"OllamaException: {original_exception}\",\n            llm_provider=\"ollama\",\n            model=model,\n        )","sourceCodeStart":1798,"sourceCodeEnd":1834,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/litellm_core_utils/exception_mapping_utils.py#L1798-L1834","documentation":"litellm maps Ollama 'Failed to establish a new connection' errors to litellm.ServiceUnavailableError. The HTTP client could not reach the Ollama server at the configured address - it is not running or is unreachable.","triggerScenarios":"Calling model='ollama/...' when `ollama serve` is not running, the host/port is wrong (default http://localhost:11434), a firewall blocks it, or the container has no route to the host (Docker host.docker.internal issues).","commonSituations":"Forgot to start the daemon; OLLAMA_API_BASE pointing at a stale IP; macOS/Windows Docker containers unable to reach localhost services; Ollama bound only to 127.0.0.1 while client is remote.","solutions":["Start the server: run `ollama serve` (or ensure the service/daemon is up)","Set OLLAMA_API_BASE (or api_base=) to the correct reachable URL, including scheme","If Ollama must accept remote clients, set OLLAMA_HOST=0.0.0.0 and open the port","From Docker, use host.docker.internal:11434 or the host network on Linux","Verify reachability: curl http://<host>:11434/api/tags"],"exampleFix":"# before\nlitellm.completion(model='ollama/llama3', messages=msgs)  # nothing on :11434\n# after\nimport os\nos.environ['OLLAMA_API_BASE'] = 'http://host.docker.internal:11434'\n# shell: OLLAMA_HOST=0.0.0.0 ollama serve\nlitellm.completion(model='ollama/llama3', messages=msgs)","handlingStrategy":"validation","validationCode":"import requests, os\nbase = os.environ.get('OLLAMA_API_BASE', 'http://localhost:11434')\ntry:\n    requests.get(f'{base}/api/tags', timeout=3).raise_for_status()\nexcept requests.ConnectionError:\n    raise RuntimeError(f'Ollama unreachable at {base} - start `ollama serve`')","typeGuard":"import litellm\n\ndef is_ollama_down(e: Exception) -> bool:\n    return isinstance(e, litellm.ServiceUnavailableError) and 'new connection' in str(e)","tryCatchPattern":"try:\n    litellm.completion(model='ollama/llama3', messages=msgs)\nexcept litellm.ServiceUnavailableError as e:\n    if 'new connection' in str(e):\n        wait_for_ollama_health()  # then retry once\n    raise","preventionTips":["Run Ollama under a supervisor (systemd/docker restart=always)","Set OLLAMA_API_BASE explicitly in config, not by convention","Add a startup connectivity probe before serving traffic"],"tags":["ollama","connection","service-unavailable","litellm"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}