Simplify using correect types

This commit is contained in:
Nicolas Mowen 2026-03-25 09:03:19 -06:00
parent 42560de302
commit 7e41e1ecef
2 changed files with 20 additions and 17 deletions

View File

@ -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,

View File

@ -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