This script synchronizes the list of models configured for the "Ollama" provider within your opencode.json file with the models actually installed on your local Ollama server instance. This ensures that your development environment is always pointing to the correct and current set of available models, preventing "Model not found" type errors in OpenCode.
Before running the script, ensure you have:
- Python 3: The script requires Python 3 to run.
- Requests Library: Install the necessary Python dependency:
pip install requests - Running Ollama Instance: Your local Ollama server must be running in the background (
ollama serve).
The sync_opencode_ollama.py script performs three main functions:
- Discovery: It connects to the configured Ollama API endpoint (defaulting to
http://localhost:11434/api/tags) and automatically pulls a list of every model currently available on your Ollama machine (e.g.,llava:latest,qwen2:7b). - Loading: It loads the existing configuration from
opencode.json(or creates a default one if it doesn't exist). - Synchronization: It updates the specific section for the Ollama provider (
provider.ollama.models) to match the discovered list of models.
1. Basic Sync (Standard Use)
This command connects to Ollama and syncs all current model names into opencode.json.
python3 sync_opencode_ollama.py2. Specifying a Config File
If your configuration file is not in the default location (~/.config/opencode/opencode.json), use the --config flag:
python3 sync_opencode_ollama.py --config /path/to/your/project/opencode.json3. Specifying a Custom Ollama Host
If your Ollama server is running on a different machine or port, use --ollama-host:
python3 sync_opencode_ollama.py --ollama-host http://192.168.1.50:11434π‘οΈ Maintenance Commands (Highly Recommended)
-
Pruning: If you delete a local model through the
ollama rmcommand, it remains listed inopencode.json. Running the sync with--pruneremoves orphaned entries from your configuration:python3 sync_opencode_ollama.py --prune
-
Dry Run: To see exactly what changes will be made without actually touching your configuration file:
python3 sync_opencode_ollama.py --dry-run