diff --git a/InsightEngine/llms/base.py b/InsightEngine/llms/base.py index 090c10c59..948a14f5b 100644 --- a/InsightEngine/llms/base.py +++ b/InsightEngine/llms/base.py @@ -37,7 +37,7 @@ def __init__(self, api_key: str, model_name: str, base_url: Optional[str] = None raise ValueError("Insight Engine INSIGHT_ENGINE_MODEL_NAME is required.") self.api_key = api_key - self.base_url = base_url + self.base_url = self._validate_base_url(base_url) if base_url else base_url self.model_name = model_name self.provider = model_name timeout_fallback = os.getenv("LLM_REQUEST_TIMEOUT") or os.getenv("INSIGHT_ENGINE_REQUEST_TIMEOUT") or "1800" diff --git a/MediaEngine/llms/base.py b/MediaEngine/llms/base.py index 888b0a5e3..a3f45d463 100644 --- a/MediaEngine/llms/base.py +++ b/MediaEngine/llms/base.py @@ -33,6 +33,28 @@ class LLMClient: Minimal wrapper around the OpenAI-compatible chat completion API. """ + @staticmethod + def _validate_base_url(base_url: str) -> str: + """Validate and normalize base_url to ensure it has a protocol prefix.""" + if not base_url: + return base_url + + base_url = base_url.strip() + + # Check if URL already has a protocol + if base_url.startswith(('http://', 'https://')): + return base_url + + # Auto-add https:// prefix if missing + import warnings + warnings.warn( + f"base_url '{base_url}' is missing 'http://' or 'https://' protocol prefix. " + f"Auto-prepending 'https://'. Please update your configuration.", + UserWarning + ) + return f"https://{base_url}" + + def __init__(self, api_key: str, model_name: str, base_url: Optional[str] = None): if not api_key: raise ValueError("Media Engine LLM API key is required.") @@ -40,7 +62,7 @@ def __init__(self, api_key: str, model_name: str, base_url: Optional[str] = None raise ValueError("Media Engine model name is required.") self.api_key = api_key - self.base_url = base_url + self.base_url = self._validate_base_url(base_url) if base_url else base_url self.model_name = model_name self.provider = model_name timeout_fallback = os.getenv("LLM_REQUEST_TIMEOUT") or os.getenv("MEDIA_ENGINE_REQUEST_TIMEOUT") or "1800"