OllamaModelNotFoundError
ollama.client.OllamaModelNotFoundError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
response = client.chat.completions.create(model="llama2", messages=messages)
File "/usr/local/lib/python3.10/site-packages/ollama/client.py", line 120, in create
raise OllamaModelNotFoundError("Model not found locally. Pull required.")
ollama.client.OllamaModelNotFoundError: Model not found locally. Pull required. Why it happens
This error occurs because the Ollama client expects the requested model to be available locally. If the model has not been pulled or downloaded to the local environment, the client cannot instantiate it and raises this error.
Detection
Check for OllamaModelNotFoundError exceptions when calling model inference methods; log the model name and verify local model availability before proceeding.
Causes & fixes
The requested Ollama model is not downloaded locally on the machine.
Run 'ollama pull <model_name>' in your terminal to download the model before using it in your code.
Model name specified in code is misspelled or incorrect.
Verify the exact model name available via 'ollama list' and correct the model parameter in your code.
Ollama client environment does not have access to the model storage or cache directory.
Ensure the Ollama client has proper permissions and environment variables set to access the local model storage.
Code: broken vs fixed
import ollama
response = ollama.chat(model="llama2", messages=[{"role": "user", "content": "Hello"}]) # Raises OllamaModelNotFoundError import ollama
# Ensure model is pulled before usage
# Run in terminal: ollama pull llama2
response = ollama.chat(model="llama2", messages=[{"role": "user", "content": "Hello"}])
print(response) Workaround
Catch OllamaModelNotFoundError in your code and programmatically run a subprocess call to 'ollama pull <model_name>' to download the model before retrying the request.
Prevention
Always verify and pull required Ollama models during deployment or initialization phases to guarantee local availability and avoid runtime errors.