Skip to content

Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.

fix(gemini): route requests to the configured SDK endpoint (#193)

Rudy Mizrahi Celekli86bcd23116d8
diff --git a/ifixai/providers/gemini.py b/ifixai/providers/gemini.py
index abd7b16..060ebfa 100644
--- a/ifixai/providers/gemini.py
+++ b/ifixai/providers/gemini.py
@@ -1,5 +1,6 @@
import asyncio
import os
+from urllib.parse import urlsplit
import google.generativeai as genai
from google.ai.generativelanguage import GenerativeServiceAsyncClient
@@ -72,6 +73,11 @@ class GeminiProvider(ChatProvider):
# or judge call cannot replace this request's API key.
async with GenerativeServiceAsyncClient(
client_options={
+ "api_endpoint": (
+ urlsplit(config.endpoint).netloc
+ if config.endpoint and "://" in config.endpoint
+ else config.endpoint or "generativelanguage.googleapis.com"
+ ),
"api_key": (
config.api_key or os.environ.get("GEMINI_API_KEY")
or os.environ.get("GOOGLE_API_KEY")
diff --git a/ifixai/tests/test_gemini_client_isolation.py b/ifixai/tests/test_gemini_client_isolation.py
index ea3ad4d..c700fc6 100644
--- a/ifixai/tests/test_gemini_client_isolation.py
+++ b/ifixai/tests/test_gemini_client_isolation.py
@@ -107,3 +107,37 @@ async def test_request_owned_sdk_client_closes_on_every_exit(monkeypatch, exit_k
finally:
for native in native_clients:
await native.transport.close()
+
+
[email protected]("configured,expected", [
+ (None, "generativelanguage.googleapis.com:443"),
+ ("owned-gateway.invalid:8443", "owned-gateway.invalid:8443"),
+ ("https://owned-gateway.invalid:8443/", "owned-gateway.invalid:8443"),
+])
+async def test_native_gemini_client_uses_configured_endpoint(monkeypatch, configured, expected):
+ pytest.importorskip("google.generativeai")
+ glm = pytest.importorskip("google.ai.generativelanguage")
+ from ifixai.providers import gemini
+
+ seen = []
+
+ def request_client(**kwargs):
+ native = glm.GenerativeServiceAsyncClient(**kwargs)
+ seen.append(native.transport._host)
+
+ async def generate_content(request, **options):
+ assert request.model == "models/owned-model"
+ return glm.GenerateContentResponse(candidates=[glm.Candidate(
+ content=glm.Content(parts=[glm.Part(text="owned reply")]),
+ finish_reason=glm.Candidate.FinishReason.STOP,
+ )])
+
+ monkeypatch.setattr(native, "generate_content", generate_content)
+ return native
+
+ monkeypatch.setattr(gemini, "GenerativeServiceAsyncClient", request_client)
+ config = ProviderConfig(provider="gemini", endpoint=configured, model="owned-model",
+ api_key="owned-key", max_retries=0)
+ assert await gemini.GeminiProvider().send_message([ChatMessage(content="hello")], config) == "owned reply"
+ assert seen == [expected]