From 084358875f8b1ea002998ae9cddc68480cf92eed Mon Sep 17 00:00:00 2001 From: GdoongMathew Date: Sun, 19 Jul 2026 15:32:53 +0800 Subject: [PATCH 1/4] update `check_ollama_model_exists` using ollama.list api. --- api/ollama_patch.py | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/api/ollama_patch.py b/api/ollama_patch.py index bb6a77294..a7b4cb482 100644 --- a/api/ollama_patch.py +++ b/api/ollama_patch.py @@ -23,30 +23,24 @@ def check_ollama_model_exists(model_name: str, ollama_host: str = None) -> bool: Returns: bool: True if model exists, False otherwise """ + import ollama if ollama_host is None: ollama_host = os.getenv("OLLAMA_HOST", "http://localhost:11434") - try: # Remove /api prefix if present and add it back if ollama_host.endswith('/api'): ollama_host = ollama_host[:-4] - - response = requests.get(f"{ollama_host}/api/tags", timeout=5) - if response.status_code == 200: - models_data = response.json() - available_models = [model.get('name', '').split(':')[0] for model in models_data.get('models', [])] - model_base_name = model_name.split(':')[0] # Remove tag if present - - is_available = model_base_name in available_models - if is_available: - logger.info(f"Ollama model '{model_name}' is available") - else: - logger.warning(f"Ollama model '{model_name}' is not available. Available models: {available_models}") - return is_available + ret: ollama.ListResponse = ollama.Client(host=ollama_host, timeout=5).list() + is_available = any(model_name == model.model for model in ret.models) + if is_available: + logger.info("Ollama model '%s' is available", model_name) else: - logger.warning(f"Could not check Ollama models, status code: {response.status_code}") - return False - except requests.exceptions.RequestException as e: + logger.warning( + "Ollama model '%s' is not available. Available models: %s. ", + model_name, + str([model.model for model in ret.models])) + return is_available + except ConnectionError as e: logger.warning(f"Could not connect to Ollama to check models: {e}") return False except Exception as e: From da715858bca072b964fe26d072432d46ac15de4b Mon Sep 17 00:00:00 2001 From: GdoongMathew Date: Sun, 19 Jul 2026 15:45:08 +0800 Subject: [PATCH 2/4] fix exception type --- api/ollama_patch.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/api/ollama_patch.py b/api/ollama_patch.py index a7b4cb482..9ba5ac34f 100644 --- a/api/ollama_patch.py +++ b/api/ollama_patch.py @@ -1,4 +1,6 @@ import logging + +import httpx import requests import os @@ -40,7 +42,7 @@ def check_ollama_model_exists(model_name: str, ollama_host: str = None) -> bool: model_name, str([model.model for model in ret.models])) return is_available - except ConnectionError as e: + except (httpx.ConnectTimeout, ConnectionError): logger.warning(f"Could not connect to Ollama to check models: {e}") return False except Exception as e: From 592523e581de456fb9b725b1d6a1effbc50458fe Mon Sep 17 00:00:00 2001 From: GdoongMathew Date: Sun, 19 Jul 2026 15:46:05 +0800 Subject: [PATCH 3/4] fix import scope --- api/ollama_patch.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api/ollama_patch.py b/api/ollama_patch.py index 9ba5ac34f..ab07b033f 100644 --- a/api/ollama_patch.py +++ b/api/ollama_patch.py @@ -1,7 +1,5 @@ import logging -import httpx -import requests import os # Configure logging @@ -26,6 +24,7 @@ def check_ollama_model_exists(model_name: str, ollama_host: str = None) -> bool: bool: True if model exists, False otherwise """ import ollama + import httpx if ollama_host is None: ollama_host = os.getenv("OLLAMA_HOST", "http://localhost:11434") try: From 84f051723edbb8d6aab2060a484fa4d3213b3a39 Mon Sep 17 00:00:00 2001 From: GdoongMathew Date: Sun, 19 Jul 2026 15:51:31 +0800 Subject: [PATCH 4/4] fix catching exception --- api/ollama_patch.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/ollama_patch.py b/api/ollama_patch.py index ab07b033f..cc04b7317 100644 --- a/api/ollama_patch.py +++ b/api/ollama_patch.py @@ -41,7 +41,7 @@ def check_ollama_model_exists(model_name: str, ollama_host: str = None) -> bool: model_name, str([model.model for model in ret.models])) return is_available - except (httpx.ConnectTimeout, ConnectionError): + except (httpx.ConnectTimeout, ConnectionError) as e: logger.warning(f"Could not connect to Ollama to check models: {e}") return False except Exception as e: