Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(ollama): bracket IPv6 hosts in tags requests (#6700)

LIa1ae099393ae
diff --git a/openviking_cli/utils/ollama.py b/openviking_cli/utils/ollama.py
index a15e15249..0d31968e9 100644
--- a/openviking_cli/utils/ollama.py
+++ b/openviking_cli/utils/ollama.py
@@ -74,13 +74,20 @@ def parse_ollama_url(api_base: str | None) -> tuple[str, int]:
# ---------------------------------------------------------------------------
+def _ollama_tags_url(host: str, port: int) -> str:
+ """Keep IPv6 host literals bracketed in the tags endpoint URL."""
+ if ":" in host and not host.startswith("["):
+ host = f"[{host}]"
+ return f"http://{host}:{port}/api/tags"
+
+
def check_ollama_running(
host: str = OLLAMA_DEFAULT_HOST,
port: int = OLLAMA_DEFAULT_PORT,
) -> bool:
"""Return ``True`` if Ollama is responding at *host*:*port*."""
try:
- url = f"http://{host}:{port}/api/tags"
+ url = _ollama_tags_url(host, port)
req = urllib.request.Request(url, method="GET")
with urllib.request.urlopen(req, timeout=3):
return True
@@ -94,7 +101,7 @@ def get_ollama_models(
) -> list[str]:
"""Fetch names of locally available Ollama models."""
try:
- url = f"http://{host}:{port}/api/tags"
+ url = _ollama_tags_url(host, port)
req = urllib.request.Request(url, method="GET")
with urllib.request.urlopen(req, timeout=5) as resp:
data = json.loads(resp.read().decode())
diff --git a/tests/unit/test_ollama_http_urls.py b/tests/unit/test_ollama_http_urls.py
new file mode 100644
index 000000000..d02b8f25c
--- /dev/null
+++ b/tests/unit/test_ollama_http_urls.py
@@ -0,0 +1,51 @@
+"""Check the Ollama tags request URL for supported host spellings."""
+
+from __future__ import annotations
+
+import json
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+import openviking_cli.utils.ollama as ollama
+
+
+ ("host", "port", "expected_url"),
+ [
+ ("localhost", 11434, "http://localhost:11434/api/tags"),
+ ("127.0.0.1", 8080, "http://127.0.0.1:8080/api/tags"),
+ ("gpu-server", 11434, "http://gpu-server:11434/api/tags"),
+ ("::1", 11434, "http://[::1]:11434/api/tags"),
+ ("2001:db8::1", 8080, "http://[2001:db8::1]:8080/api/tags"),
+ ("[::1]", 11434, "http://[::1]:11434/api/tags"),
+ ],
+)
[email protected]("reader", ["check_ollama_running", "get_ollama_models"])
+def test_tags_request_preserves_host(host, port, expected_url, reader):
+ response = MagicMock()
+ response.read.return_value = json.dumps({"models": [{"name": "example:latest"}]}).encode()
+ with patch.object(ollama.urllib.request, "urlopen") as urlopen:
+ urlopen.return_value.__enter__.return_value = response
+ result = getattr(ollama, reader)(host, port)
+
+ assert result == (True if reader == "check_ollama_running" else ["example:latest"])
+ urlopen.assert_called_once()
+ request = urlopen.call_args.args[0]
+ assert request.full_url == expected_url
+ assert request.get_method() == "GET"
+ assert urlopen.call_args.kwargs["timeout"] == (3 if reader == "check_ollama_running" else 5)
+
+
[email protected]("reader", ["check_ollama_running", "get_ollama_models"])
+def test_parsed_ipv6_endpoint_builds_a_valid_tags_url(reader):
+ host, port = ollama.parse_ollama_url("http://[::1]:11434/v1")
+ assert (host, port) == ("::1", 11434)
+ response = MagicMock()
+ response.read.return_value = b'{"models": []}'
+ with patch.object(ollama.urllib.request, "urlopen") as urlopen:
+ urlopen.return_value.__enter__.return_value = response
+ getattr(ollama, reader)(host, port)
+
+ request = urlopen.call_args.args[0]
+ assert request.full_url == "http://[::1]:11434/api/tags"