Skip to content

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

feat: add client header to GoogleChat

Mark McDonald2eb3e3cded27
diff --git a/browser_use/llm/google/chat.py b/browser_use/llm/google/chat.py
index 3965266ce..bdad7d3b1 100644
--- a/browser_use/llm/google/chat.py
+++ b/browser_use/llm/google/chat.py
@@ -1,4 +1,5 @@
import asyncio
+import importlib.metadata
import json
import logging
import random
@@ -122,6 +123,31 @@ class ChatGoogle(BaseChatModel):
"""Get logger for this chat instance"""
return logging.getLogger(f'browser_use.llm.google.{self.model}')
+ def _get_http_options(self) -> dict[str, Any]:
+ """Get http options with the default headers set."""
+ try:
+ bu_version = importlib.metadata.version('browser-use')
+ except importlib.metadata.PackageNotFoundError:
+ bu_version = 'unknown'
+
+ header_value = f'browser-use/{bu_version}'
+
+ http_options = self.http_options or {}
+ if hasattr(http_options, 'model_dump'):
+ http_options = http_options.model_dump(exclude_unset=True)
+ elif not isinstance(http_options, dict):
+ http_options = dict(http_options)
+
+ headers = http_options.get('headers', {})
+ if not isinstance(headers, dict):
+ headers = dict(headers)
+
+ # Overwrite x-goog-api-client header as requested
+ headers['x-goog-api-client'] = header_value
+ http_options['headers'] = headers
+
+ return http_options
+
def _get_client_params(self) -> dict[str, Any]:
"""Prepare client parameters dictionary."""
# Define base client params
@@ -131,7 +157,7 @@ class ChatGoogle(BaseChatModel):
'credentials': self.credentials,
'project': self.project,
'location': self.location,
- 'http_options': self.http_options,
+ 'http_options': self._get_http_options(),
}
# Create client_params dict with non-None values
diff --git a/tests/ci/models/test_llm_google.py b/tests/ci/models/test_llm_google.py
index 9d8bc8632..fb62bdf2e 100644
--- a/tests/ci/models/test_llm_google.py
+++ b/tests/ci/models/test_llm_google.py
@@ -13,3 +13,18 @@ async def test_google_gemini_flash_latest(httpserver):
extra_kwargs={},
httpserver=httpserver,
)
+
+
+def test_x_goog_api_client_header_is_set():
+ """Test that the x-goog-api-client header is correctly set in the HTTP options."""
+ chat = ChatGoogle(model='gemini-flash-latest', api_key='fake')
+
+ # Generate the params used for genai.Client
+ params = chat._get_client_params()
+
+ # Extract the header
+ http_options = params.get('http_options', {})
+ headers = http_options.get('headers', {})
+
+ assert 'x-goog-api-client' in headers, 'x-goog-api-client header missing'
+ assert 'browser-use/' in headers['x-goog-api-client'], 'browser-use not found in x-goog-api-client header'