feat(gemini): normalize base URL to strip API version suffixes

Automatically remove trailing slashes and version paths (e.g., /v1beta) from GEMINI_BASE_URL
Update GeminiAnalyzer to use the normalized URL and add type hints
Add test coverage for Gemini client configuration
This commit is contained in:
2026-04-02 10:25:18 +08:00
parent 7003dfa0df
commit d6218d6bad
6 changed files with 104 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
import re
from typing import Self
from dotenv import load_dotenv
@@ -30,3 +31,10 @@ class Settings:
def require_api_key(self) -> None:
if not self.api_key:
raise ValueError("Missing GEMINI_API_KEY (or GOOGLE_API_KEY) in the environment.")
def normalized_base_url(self) -> str | None:
if not self.base_url:
return None
base_url = self.base_url.strip().rstrip("/")
return re.sub(r"/v\d+(?:alpha|beta)?$", "", base_url, flags=re.IGNORECASE)

View File

@@ -1,7 +1,8 @@
from __future__ import annotations
from dataclasses import dataclass
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
import json
from .config import Settings
@@ -10,6 +11,8 @@ from .config import Settings
@dataclass(slots=True)
class GeminiAnalyzer:
settings: Settings
_types: Any = field(init=False, repr=False)
_client: Any = field(init=False, repr=False)
def __post_init__(self) -> None:
self.settings.require_api_key()
@@ -18,8 +21,9 @@ class GeminiAnalyzer:
from google.genai import types
http_options = types.HttpOptions(timeout=120_000)
if self.settings.base_url:
http_options = types.HttpOptions(base_url=self.settings.base_url, timeout=120_000)
base_url = self.settings.normalized_base_url()
if base_url:
http_options = types.HttpOptions(base_url=base_url, timeout=120_000)
self._types = types
self._client = genai.Client(api_key=self.settings.api_key, http_options=http_options)