{"record":{"id":"39ed0e3227dcfac3","repo":"affaan-m/ECC","slug":"contextlengtherror-msg-provider-providertype-ollama-from-e","errorCode":null,"errorMessage":"ContextLengthError(msg, provider=ProviderType.OLLAMA) from e","messagePattern":"ContextLengthError\\(msg, provider=ProviderType\\.OLLAMA\\) from e","errorType":"exception","errorClass":"ContextLengthError","httpStatus":null,"severity":"error","filePath":"src/llm/providers/ollama.py","lineNumber":108,"sourceCode":"                        arguments=tc.get(\"function\", {}).get(\"arguments\", {}),\n                    )\n                    for tc in result[\"message\"][\"tool_calls\"]\n                ]\n\n            return LLMOutput(\n                content=content,\n                tool_calls=tool_calls,\n                model=model,\n                stop_reason=result.get(\"done_reason\"),\n            )\n        except Exception as e:\n            msg = str(e)\n            if \"401\" in msg or \"connection\" in msg.lower():\n                raise AuthenticationError(f\"Ollama connection failed: {msg}\", provider=ProviderType.OLLAMA) from e\n            if \"429\" in msg or \"rate_limit\" in msg.lower():\n                raise RateLimitError(msg, provider=ProviderType.OLLAMA) from e\n            if \"context\" in msg.lower() and \"length\" in msg.lower():\n                raise ContextLengthError(msg, provider=ProviderType.OLLAMA) from e\n            raise\n\n    def list_models(self) -> list[ModelInfo]:\n        return self._models.copy()\n\n    def validate_config(self) -> bool:\n        return bool(self.base_url)\n\n    def get_default_model(self) -> str:\n        return self.default_model\n","sourceCodeStart":90,"sourceCodeEnd":119,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/src/llm/providers/ollama.py#L90-L119","documentation":"Ollama's generate() raises ContextLengthError when the underlying error message contains both 'context' and 'length', meaning the prompt (plus num_predict/num_ctx) exceeded Ollama's context size. The original exception is chained via 'from e'.","triggerScenarios":"Calling generate() with a prompt larger than the model's configured num_ctx; Ollama returning an error mentioning 'context length'.","commonSituations":"Loading long documents into prompts without chunking; default num_ctx (often 2048/4096) being far smaller than the model's advertised window; long tool-call conversations accumulating tokens.","solutions":["Increase the context size (num_ctx option) when creating/loading the model","Chunk or summarize long inputs before sending","Truncate chat history to fit within num_ctx","Choose a model/variant with a larger supported context window"],"exampleFix":"// before\n# default num_ctx too small for long prompt\nresp = provider.generate(inp)\n// after\nollama run llama3 --num-ctx 16384\nresp = provider.generate(inp)","handlingStrategy":"validation","validationCode":"NUM_CTX = 16384\nif len(prompt) // 4 + num_predict >= NUM_CTX:\n    raise ValueError(\"Prompt would exceed Ollama context window\")","typeGuard":null,"tryCatchPattern":"try:\n    resp = provider.generate(inp)\nexcept ContextLengthError:\n    inp = replace(inp, prompt=truncate_to_tokens(inp.prompt, NUM_CTX - num_predict))\n    resp = provider.generate(inp)","preventionTips":["Set num_ctx explicitly when loading the model","Chunk long documents instead of sending whole files","Keep a rolling window of chat history","Verify context size with a tokenizer, not character counts"],"tags":["llm","context-window","ollama"],"backgroundTag":"context-length-exceeded","analyzedSha":"8321021c54d670126ce3b2969d5deb880b4b0c2a","analyzedAt":"2026-09-16T10:08:13.343Z","contentChangedAt":"2026-09-16T10:08:13.343Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}