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
feat(llm/google): forward cached_content into generate_content
Saurav Pandadf063f90688e
diff --git a/browser_use/llm/google/chat.py b/browser_use/llm/google/chat.pyindex 9c79e20f7..f2bf04731 100644--- a/browser_use/llm/google/chat.py+++ b/browser_use/llm/google/chat.py@@ -99,6 +99,9 @@ class ChatGoogle(BaseChatModel):config: types.GenerateContentConfigDict | None = Noneinclude_system_in_user: bool = Falsesupports_structured_output: bool = True # New flag+ cached_content: str | None = (+ None # Gemini explicit CachedContent name (e.g. "cachedContents/abc123") to reuse a precomputed prefix across calls. Per-call override via ainvoke(..., cached_content=...).+ )max_retries: int = 5 # Number of retries for retryable errorsretryable_status_codes: list[int] = field(default_factory=lambda: [429, 500, 502, 503, 504]) # Status codes to retry onretry_base_delay: float = 1.0 # Base delay in seconds for exponential backoff@@ -321,6 +324,11 @@ class ChatGoogle(BaseChatModel):if self.max_output_tokens is not None:config['max_output_tokens'] = self.max_output_tokens+ # Explicit Gemini CachedContent: per-call kwarg takes precedence over the instance default.+ cached_content_name = kwargs.get('cached_content', self.cached_content)+ if cached_content_name:+ config['cached_content'] = cached_content_name+async def _make_api_call():start_time = time.time()self.logger.debug(f'🚀 Starting API call to {self.model}')diff --git a/tests/ci/models/test_llm_google.py b/tests/ci/models/test_llm_google.pyindex 96bbb450b..05e4a814a 100644--- a/tests/ci/models/test_llm_google.py+++ b/tests/ci/models/test_llm_google.py@@ -53,6 +53,40 @@ def test_x_goog_api_client_header_with_pydantic_http_options():assert http_opts.get('headers', {}).get('x-goog-api-client', '').startswith('browser-use/')+async def test_cached_content_threaded_into_config(monkeypatch):+ """Setting cached_content (instance default or per-call kwarg) should reach the generate_content call."""+ from types import SimpleNamespace++ from browser_use.llm.messages import UserMessage++ captured: dict = {}++ async def fake_generate_content(*, model, contents, config):+ captured['config'] = dict(config) if config else {}+ return SimpleNamespace(text='ok', parsed=None, usage_metadata=None, candidates=[])++ def fake_get_client(self):+ return SimpleNamespace(aio=SimpleNamespace(models=SimpleNamespace(generate_content=fake_generate_content)))++ monkeypatch.setattr(ChatGoogle, 'get_client', fake_get_client)++ # Instance default+ chat = ChatGoogle(model='gemini-flash-latest', api_key='fake', cached_content='cachedContents/abc123')+ await chat.ainvoke([UserMessage(content='hi')])+ assert captured['config'].get('cached_content') == 'cachedContents/abc123'++ # Per-call kwarg overrides instance default+ captured.clear()+ await chat.ainvoke([UserMessage(content='hi')], cached_content='cachedContents/override')+ assert captured['config'].get('cached_content') == 'cachedContents/override'++ # When unset, key is absent from config+ captured.clear()+ chat_no_cache = ChatGoogle(model='gemini-flash-latest', api_key='fake')+ await chat_no_cache.ainvoke([UserMessage(content='hi')])+ assert 'cached_content' not in captured['config']++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