From 7e41e1ecefce8c1dd4e0c215f7eb40fc33742347 Mon Sep 17 00:00:00 2001 From: Nicolas Mowen Date: Wed, 25 Mar 2026 09:03:19 -0600 Subject: [PATCH] Simplify using correect types --- frigate/genai/azure-openai.py | 8 ++++---- frigate/genai/gemini.py | 29 ++++++++++++++++------------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/frigate/genai/azure-openai.py b/frigate/genai/azure-openai.py index 75096b8d8..7cd8894a4 100644 --- a/frigate/genai/azure-openai.py +++ b/frigate/genai/azure-openai.py @@ -23,10 +23,10 @@ class OpenAIClient(GenAIClient): def _init_provider(self) -> AzureOpenAI | None: """Initialize the client.""" try: - parsed_url = urlparse(self.genai_config.base_url) - query_params = parse_qs(parsed_url.query) # type: ignore[type-var] + 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}/" # type: ignore[str-bytes-safe] + azure_endpoint = f"{parsed_url.scheme}://{parsed_url.netloc}/" if not api_version: logger.warning("Azure OpenAI url is missing API version.") @@ -36,7 +36,7 @@ class OpenAIClient(GenAIClient): logger.warning("Error parsing Azure OpenAI url: %s", str(e)) return None - return AzureOpenAI( # type: ignore[call-overload,no-any-return] + return AzureOpenAI( api_key=self.genai_config.api_key, api_version=api_version, azure_endpoint=azure_endpoint, diff --git a/frigate/genai/gemini.py b/frigate/genai/gemini.py index fb5695046..b031b5263 100644 --- a/frigate/genai/gemini.py +++ b/frigate/genai/gemini.py @@ -6,6 +6,7 @@ from typing import Any, AsyncGenerator, Optional from google import genai from google.genai import errors, types +from google.genai.types import FunctionCallingConfigMode from frigate.config import GenAIProviderEnum from frigate.genai import GenAIClient, register_genai_provider @@ -78,7 +79,9 @@ class GeminiClient(GenAIClient): return None try: - description = response.text.strip() # type: ignore[union-attr] + if response.text is None: + return None + description = response.text.strip() except (ValueError, AttributeError): # No description was generated return None @@ -110,10 +113,10 @@ class GeminiClient(GenAIClient): # Map roles to Gemini format if role == "system": # Gemini doesn't have system role, prepend to first user message - if gemini_messages and gemini_messages[0].role == "user": - gemini_messages[0].parts[ # type: ignore[index] + if gemini_messages and gemini_messages[0].role == "user" and gemini_messages[0].parts: + gemini_messages[0].parts[ 0 - ].text = f"{content}\n\n{gemini_messages[0].parts[0].text}" # type: ignore[index] + ].text = f"{content}\n\n{gemini_messages[0].parts[0].text}" else: gemini_messages.append( types.Content( @@ -171,15 +174,15 @@ class GeminiClient(GenAIClient): if tool_choice: if tool_choice == "none": tool_config = types.ToolConfig( - function_calling_config=types.FunctionCallingConfig(mode="NONE") # type: ignore[arg-type] + function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.NONE) ) elif tool_choice == "auto": tool_config = types.ToolConfig( - function_calling_config=types.FunctionCallingConfig(mode="AUTO") # type: ignore[arg-type] + function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.AUTO) ) elif tool_choice == "required": tool_config = types.ToolConfig( - function_calling_config=types.FunctionCallingConfig(mode="ANY") # type: ignore[arg-type] + function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.ANY) ) # Build request config @@ -307,10 +310,10 @@ class GeminiClient(GenAIClient): # Map roles to Gemini format if role == "system": # Gemini doesn't have system role, prepend to first user message - if gemini_messages and gemini_messages[0].role == "user": - gemini_messages[0].parts[ # type: ignore[index] + if gemini_messages and gemini_messages[0].role == "user" and gemini_messages[0].parts: + gemini_messages[0].parts[ 0 - ].text = f"{content}\n\n{gemini_messages[0].parts[0].text}" # type: ignore[index] + ].text = f"{content}\n\n{gemini_messages[0].parts[0].text}" else: gemini_messages.append( types.Content( @@ -368,15 +371,15 @@ class GeminiClient(GenAIClient): if tool_choice: if tool_choice == "none": tool_config = types.ToolConfig( - function_calling_config=types.FunctionCallingConfig(mode="NONE") # type: ignore[arg-type] + function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.NONE) ) elif tool_choice == "auto": tool_config = types.ToolConfig( - function_calling_config=types.FunctionCallingConfig(mode="AUTO") # type: ignore[arg-type] + function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.AUTO) ) elif tool_choice == "required": tool_config = types.ToolConfig( - function_calling_config=types.FunctionCallingConfig(mode="ANY") # type: ignore[arg-type] + function_calling_config=types.FunctionCallingConfig(mode=FunctionCallingConfigMode.ANY) ) # Build request config