Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fix: handle types.HttpOptionsDict better, add more tests
Mark McDonald115199d2baf8
diff --git a/browser_use/llm/google/chat.py b/browser_use/llm/google/chat.pyindex bdad7d3b1..0356e1022 100644--- a/browser_use/llm/google/chat.py+++ b/browser_use/llm/google/chat.py@@ -132,21 +132,23 @@ class ChatGoogle(BaseChatModel):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)+ http_opts: dict[str, Any] = {}- headers = http_options.get('headers', {})- if not isinstance(headers, dict):- headers = dict(headers)+ if self.http_options is not None:+ if isinstance(self.http_options, types.HttpOptions):+ http_opts = self.http_options.model_dump(exclude_unset=True)+ elif isinstance(self.http_options, dict):+ http_opts = dict(self.http_options)++ headers: dict[str, str] = {}+ existing_headers = http_opts.get('headers')+ if isinstance(existing_headers, dict):+ headers = {str(k): str(v) for k, v in existing_headers.items()}- # Overwrite x-goog-api-client header as requestedheaders['x-goog-api-client'] = header_value- http_options['headers'] = headers+ http_opts['headers'] = headers- return http_options+ return http_optsdef _get_client_params(self) -> dict[str, Any]:"""Prepare client parameters dictionary."""diff --git a/tests/ci/models/test_llm_google.py b/tests/ci/models/test_llm_google.pyindex fb62bdf2e..adb6b779d 100644--- a/tests/ci/models/test_llm_google.py+++ b/tests/ci/models/test_llm_google.py@@ -28,3 +28,44 @@ def test_x_goog_api_client_header_is_set():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'+++def test_x_goog_api_client_header_with_none_http_options():+ """Test setting header when http_options is None."""+ chat = ChatGoogle(model='gemini-flash-latest', api_key='fake', http_options=None)+ params = chat._get_client_params()+ http_opts = params.get('http_options', {})+ assert http_opts.get('headers', {}).get('x-goog-api-client', '').startswith('browser-use/')+++def test_x_goog_api_client_header_with_pydantic_http_options():+ """Test setting header when http_options is a types.HttpOptions Pydantic model."""+ from google.genai import types++ pydantic_opts = types.HttpOptions(timeout=30, headers={'custom-header': 'value'})+ chat = ChatGoogle(model='gemini-flash-latest', api_key='fake', http_options=pydantic_opts)+ params = chat._get_client_params()+ http_opts = params.get('http_options', {})++ # Verify it extracts and preserves timeout and custom-header+ assert http_opts.get('timeout') == 30+ assert http_opts.get('headers', {}).get('custom-header') == 'value'+ assert http_opts.get('headers', {}).get('x-goog-api-client', '').startswith('browser-use/')+++def test_x_goog_api_client_header_with_dict_http_options():+ """Test setting header when http_options is a dictionary (types.HttpOptionsDict)."""+ from google.genai import types++ dict_opts: types.HttpOptionsDict = {+ 'timeout': 45,+ 'headers': {'another-header': 'another-value'},+ }+ chat = ChatGoogle(model='gemini-flash-latest', api_key='fake', http_options=dict_opts)+ params = chat._get_client_params()+ http_opts = params.get('http_options', {})++ # Verify it preserves dictionary values and appends the tracking header+ assert http_opts.get('timeout') == 45+ assert http_opts.get('headers', {}).get('another-header') == 'another-value'+ assert http_opts.get('headers', {}).get('x-goog-api-client', '').startswith('browser-use/')