{"record":{"id":"5cd8663657cbb1ba","repo":"BerriAI/litellm","slug":"keyerror-e-got-unexpected-response-from-ollama","errorCode":null,"errorMessage":"KeyError: {e}, Got unexpected response from Ollama: {chunk}","messagePattern":"KeyError: (.+?), Got unexpected response from Ollama: (.+?)","errorType":"exception","errorClass":"OllamaError","httpStatus":400,"severity":"error","filePath":"litellm/llms/ollama/chat/transformation.py","lineNumber":541,"sourceCode":"                    )\n                ]\n\n            usage: Final = ChatCompletionUsageBlock(\n                prompt_tokens=chunk.get(\"prompt_eval_count\", 0),\n                completion_tokens=chunk.get(\"eval_count\", 0),\n                total_tokens=chunk.get(\"prompt_eval_count\", 0) + chunk.get(\"eval_count\", 0),\n            )\n\n            return ModelResponseStream(\n                id=str(uuid.uuid4()),\n                object=\"chat.completion.chunk\",\n                created=int(time.time()),  # ollama created_at is in UTC\n                usage=usage,\n                model=chunk[\"model\"],\n                choices=choices,\n            )\n        except KeyError as e:\n            raise OllamaError(\n                message=f\"KeyError: {e}, Got unexpected response from Ollama: {chunk}\",\n                status_code=400,\n                headers={\"Content-Type\": \"application/json\"},\n            )\n        except Exception as e:\n            raise e\n","sourceCodeStart":523,"sourceCodeEnd":548,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/ollama/chat/transformation.py#L523-L548","documentation":"In the Ollama streaming chat path, chunk_parser builds a ModelResponseStream from the Ollama chunk using direct key access (`chunk[\"model\"]` plus message content keys). If any required key is missing, the KeyError is caught and re-raised as an OllamaError (400) that names the missing key and dumps the whole chunk into the message.","triggerScenarios":"Streaming `litellm.completion(model='ollama/...', stream=True)` where a chunk from the Ollama server lacks the 'model' key or message structure — e.g. an unexpected keep-alive/status line parsed as JSON, a very old Ollama version with a different chunk schema, or a chunk that is an error payload rather than a chat chunk.","commonSituations":"Ollama server version drift (fields added/removed between releases), hitting a proxy that injects non-chat JSON frames, or Ollama returning an unexpected final chunk shape for non-chat models (embedding-only models called via chat).","solutions":["Inspect the chunk printed in the error message to see which key is missing and what the payload actually is.","Upgrade (or pin) the Ollama server to a version compatible with your LiteLLM release.","If a proxy sits in front of Ollama, bypass it or configure it to pass SSE frames through unmodified.","Confirm the model you call actually supports chat generation (not an embedding-only model)."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    stream = litellm.completion(model=\"ollama/llama3\", messages=msgs, stream=True)\n    for chunk in stream:\n        ...\nexcept Exception as e:\n    if \"Got unexpected response from Ollama\" in str(e):\n        # inspect chunk payload in message; likely version mismatch\n        log.error(\"ollama chunk schema mismatch: %s\", e)\n        raise","preventionTips":["Pin Ollama server and LiteLLM versions together in CI","Bypass proxies that rewrite SSE frames","Log the raw chunk from the error message before debugging blindly"],"tags":["ollama","streaming","keyerror","response-parsing"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}