{"record":{"id":"95dd29b48d546f52","repo":"BerriAI/litellm","slug":"ollama-error-chunk","errorCode":null,"errorMessage":"Ollama Error - {chunk}","messagePattern":"Ollama Error - (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/llms/ollama/completion/transformation.py","lineNumber":458,"sourceCode":"            streaming_response=streaming_response,\n            sync_stream=sync_stream,\n            json_mode=json_mode,\n        )\n\n\nclass OllamaTextCompletionResponseIterator(BaseModelResponseIterator):\n    def __init__(self, streaming_response, sync_stream: bool, json_mode: bool | None = False):\n        super().__init__(streaming_response, sync_stream, json_mode)\n        self.started_reasoning_content: bool = False\n        self.finished_reasoning_content: bool = False\n\n    def _handle_string_chunk(self, str_line: str) -> GenericStreamingChunk | ModelResponseStream:\n        return self.chunk_parser(json.loads(str_line))\n\n    def chunk_parser(self, chunk: dict) -> GenericStreamingChunk | ModelResponseStream:\n        try:\n            if \"error\" in chunk:\n                raise Exception(f\"Ollama Error - {chunk}\")\n\n            text = \"\"\n            is_finished = False\n            finish_reason = None\n            if chunk[\"done\"] is True:\n                text = \"\"\n                is_finished = True\n                finish_reason = \"stop\"\n                prompt_eval_count: Final[int | None] = chunk.get(\"prompt_eval_count\", None)\n                eval_count: Final[int | None] = chunk.get(\"eval_count\", None)\n\n                usage: ChatCompletionUsageBlock | None = None\n                if prompt_eval_count is not None and eval_count is not None:\n                    usage = ChatCompletionUsageBlock(\n                        prompt_tokens=prompt_eval_count,\n                        completion_tokens=eval_count,\n                        total_tokens=prompt_eval_count + eval_count,\n                    )","sourceCodeStart":440,"sourceCodeEnd":476,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/ollama/completion/transformation.py#L440-L476","documentation":"In Ollama text-completion streaming, each parsed chunk is checked for an 'error' key. Ollama reports some errors inside an HTTP 200 stream (e.g. model load failure); LiteLLM raises a generic Exception whose message is 'Ollama Error - {chunk}', embedding the raw error payload.","triggerScenarios":"Streaming `litellm.completion(model='ollama/<model>', ...)` (text-completion path) where the model failed to load, does not exist on the server, or ran out of memory mid-stream. The chunk arrives as {\"error\": \"...\"} inside the SSE stream and triggers this raise inside chunk_parser.","commonSituations":"Pulling a model name with a typo or that was never `ollama pull`ed; server OOM while generating; model file corruption; or the model being deleted from the server between requests.","solutions":["Read the chunk in the message — it contains Ollama's actual error text (e.g. 'model ... not found').","Run `ollama list` to confirm the model exists and `ollama run <model>` to verify it loads.","For OOM errors, free memory or use a smaller model/quantization.","Wrap streaming calls in try/except and surface the error instead of assuming stream failure is a network issue."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    for chunk in litellm.text_completion(model=\"ollama/m\", prompt=p, stream=True):\n        ...\nexcept Exception as e:\n    if str(e).startswith(\"Ollama Error -\"):\n        # in-band stream error: message contains the raw Ollama payload\n        handle_ollama_error_payload(str(e))","preventionTips":["Verify models exist with ollama list before streaming","Treat any chunk as potentially fatal — in-band errors arrive after successful chunks","Monitor Ollama server memory to pre-empt OOM mid-stream"],"tags":["ollama","streaming","text-completion","upstream-error"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}