Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

fix gemini client

Magnus Müllerdb27b32b049f
diff --git a/browser_use/llm/google/chat.py b/browser_use/llm/google/chat.py
index 75724a7e6..c35ae7714 100644
--- a/browser_use/llm/google/chat.py
+++ b/browser_use/llm/google/chat.py
@@ -89,6 +89,9 @@ class ChatGoogle(BaseChatModel):
location: str | None = None
http_options: types.HttpOptions | types.HttpOptionsDict | None = None
+ # Internal client cache to prevent connection issues
+ _client: genai.Client | None = None
+
# Static
@property
def provider(self) -> str:
@@ -123,8 +126,12 @@ class ChatGoogle(BaseChatModel):
Returns:
genai.Client: An instance of the Google genai client.
"""
+ if self._client is not None:
+ return self._client
+
client_params = self._get_client_params()
- return genai.Client(**client_params)
+ self._client = genai.Client(**client_params)
+ return self._client
@property
def name(self) -> str:
diff --git a/examples/models/gemini.py b/examples/models/gemini.py
index bf8bc3306..fa1fe223c 100644
--- a/examples/models/gemini.py
+++ b/examples/models/gemini.py
@@ -14,7 +14,7 @@ api_key = os.getenv('GOOGLE_API_KEY')
if not api_key:
raise ValueError('GOOGLE_API_KEY is not set')
-llm = ChatGoogle(model='gemini-2.5-flash', api_key=api_key)
+llm = ChatGoogle(model='gemini-2.5-flash', api_key=api_key, temperature=0.0)
async def run_search():