diff --git a/frigate/genai/plugins/azure-openai.py b/frigate/genai/plugins/azure-openai.py index 4cd4084ae3..3599eb0dbd 100644 --- a/frigate/genai/plugins/azure-openai.py +++ b/frigate/genai/plugins/azure-openai.py @@ -7,14 +7,15 @@ overrides what is genuinely Azure-specific: - Client construction: parses ``api-version`` out of the configured ``base_url`` query string and instantiates :class:`openai.AzureOpenAI` - with ``azure_endpoint`` instead of ``base_url``. + with ``azure_endpoint`` instead of ``base_url``. Raises if the URL is + malformed; :class:`GenAIClientManager` catches the exception and + disables the provider. - Context size: Azure does not expose a per-model ``max_model_len`` field reliably, so we keep the historical 128K default rather than the model-name heuristic used by OpenAI. """ import logging -from typing import Optional from urllib.parse import parse_qs, urlparse from openai import AzureOpenAI @@ -30,22 +31,16 @@ logger = logging.getLogger(__name__) class AzureOpenAIClient(OpenAIClient): """Generative AI client for Frigate using Azure OpenAI.""" - provider: AzureOpenAI # type: ignore[assignment] - - def _init_provider(self) -> Optional[AzureOpenAI]: + def _init_provider(self) -> AzureOpenAI: """Initialize the AzureOpenAI client from the configured base_url.""" - try: - parsed_url = urlparse(self.genai_config.base_url or "") - query_params = parse_qs(parsed_url.query) - api_version = query_params.get("api-version", [None])[0] - azure_endpoint = f"{parsed_url.scheme}://{parsed_url.netloc}/" + parsed_url = urlparse(self.genai_config.base_url or "") + query_params = parse_qs(parsed_url.query) + api_version = query_params.get("api-version", [None])[0] - if not api_version: - logger.warning("Azure OpenAI url is missing API version.") - return None - except Exception as e: - logger.warning("Error parsing Azure OpenAI url: %s", str(e)) - return None + if not api_version: + raise ValueError("Azure OpenAI base_url is missing api-version.") + + azure_endpoint = f"{parsed_url.scheme}://{parsed_url.netloc}/" return AzureOpenAI( api_key=self.genai_config.api_key, diff --git a/frigate/genai/plugins/openai.py b/frigate/genai/plugins/openai.py index 3e5adabb35..f07e83b5dc 100644 --- a/frigate/genai/plugins/openai.py +++ b/frigate/genai/plugins/openai.py @@ -38,7 +38,11 @@ class OpenAIClient(GenAIClient): context_size: Optional[int] = None def _init_provider(self) -> OpenAI: - """Initialize the client.""" + """Initialize the client. + + Subclasses (e.g. Azure) should raise on configuration errors; the + manager catches construction failures and disables the provider. + """ # Extract context_size from provider_options as it's not a valid OpenAI client parameter # It will be used in get_context_size() instead provider_opts = {