Two machines can list a model under the same name without listing the same digest. If an evaluation depends on a particular local model artifact, save the model inventory alongside the run metadata and compare it before attributing changes to a prompt. Ollama documents a read-only GET /api/tags endpoint whose response contains a models array with names and digests. This script compares two saved responses without connecting to a server.
Prerequisites and code
Use Python 3.12 with no packages. Save as ollama_inventory.py. Run with no arguments for synthetic API-shaped fixtures or supply two UTF-8 snapshot paths in baseline-then-current order.
import json
from pathlib import Path
def inventory(raw):
payload = json.loads(raw)
if not isinstance(payload, dict) or not isinstance(payload.get("models"), list):
raise ValueError("expected a models array")
result = {}
for entry in payload["models"]:
if not isinstance(entry, dict):
raise ValueError("expected a model object")
name, digest = entry.get("name"), entry.get("digest")
if (not isinstance(name, str) or not name.strip() or name in result
or not isinstance(digest, str) or not digest.strip()):
raise ValueError("invalid or duplicate inventory entry")
result[name] = digest
return result
def compare(old, new):
return [(name, "added" if name not in old else
"removed" if name not in new else "changed")
for name in sorted(old.keys() | new.keys())
if old.get(name) != new.get(name)]
def fixture(**entries):
return json.dumps({"models": [{"name": k, "digest": v}
for k, v in entries.items()]})
if __name__ == "__main__":
import sys
if len(sys.argv) == 3:
old, new = (inventory(Path(p).read_text(encoding="utf-8"))
for p in sys.argv[1:])
elif len(sys.argv) == 1:
old = inventory(fixture(draft="a"*64, retired="b"*64, stable="c"*64))
new = inventory(fixture(draft="d"*64, fresh="e"*64, stable="c"*64))
assert compare(old, new) == [("draft", "changed"), ("fresh", "added"),
("retired", "removed")]
assert compare(new, new) == []
for raw in ('{}', '{"models":[{"name":"draft"}]}',
'{"models":[{"name":"a","digest":"x"},{"name":"a","digest":"y"}]}'):
try:
inventory(raw)
except ValueError:
continue
raise AssertionError("accepted invalid inventory")
else:
raise SystemExit("give two snapshot paths or no arguments")
for name, change in compare(old, new):
print(f"{change}: {name!r}")
Run it offline
python3 ollama_inventory.py
changed: 'draft'
added: 'fresh'
removed: 'retired'
For existing exports, run python3 ollama_inventory.py baseline.json current.json. An empty report means no name-to-digest changes were found. The comparison sorts names for reproducibility and uses the digest as an opaque string. It does not discard tag suffixes or infer that similarly named models are aliases. A removed name and an added name might carry the same digest and still count as separate inventory changes.
Decide what to do with a diff
Use a changed entry as a reason to confirm which inventory and settings the run actually used. Matching digests do not guarantee identical runtime settings, prompts, or inference behavior. A malformed export is also not equivalent to an empty model inventory. If parsing fails, check that the export is a successful endpoint response and not an error body saved as JSON. If several hosts report the same name, compare each host independently rather than concatenating lists and picking a winner by file order. This is a reporting tool and does not pull, remove or run a model.
Ollama List models API reference · Java Samples Python tutorials