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
linter
kevint-cerebras82752d53805b
diff --git a/browser_use/llm/cerebras/chat.py b/browser_use/llm/cerebras/chat.pyindex 2084a4926..6a3cb1f18 100644--- a/browser_use/llm/cerebras/chat.py+++ b/browser_use/llm/cerebras/chat.py@@ -1,6 +1,5 @@from __future__ import annotations-import jsonfrom dataclasses import dataclassfrom typing import Any, TypeVar, overload@@ -14,14 +13,12 @@ from openai import (RateLimitError,)from openai.types.chat import ChatCompletion-from openai.types.shared_params.response_format_json_schema import JSONSchema, ResponseFormatJSONSchemafrom pydantic import BaseModelfrom browser_use.llm.base import BaseChatModelfrom browser_use.llm.cerebras.serializer import CerebrasMessageSerializerfrom browser_use.llm.exceptions import ModelProviderError, ModelRateLimitErrorfrom browser_use.llm.messages import BaseMessage-from browser_use.llm.schema import SchemaOptimizerfrom browser_use.llm.views import ChatInvokeCompletion, ChatInvokeUsageT = TypeVar('T', bound=BaseModel)@@ -138,11 +135,11 @@ class ChatCerebras(BaseChatModel):# For Cerebras, we'll use a simpler approach without response_format# Instead, we'll ask the model to return JSON and parse itimport json-+# Get the schema to guide the modelschema = output_format.model_json_schema()schema_str = json.dumps(schema, indent=2)-+# Create a prompt that asks for the specific JSON structurejson_prompt = f"""Please respond with a JSON object that follows this exact schema:@@ -150,7 +147,7 @@ Please respond with a JSON object that follows this exact schema:Your response must be valid JSON only, no other text."""-+# Add or modify the last user message to include the JSON promptif cerebras_messages and cerebras_messages[-1]['role'] == 'user':if isinstance(cerebras_messages[-1]['content'], str):@@ -159,11 +156,8 @@ Your response must be valid JSON only, no other text.cerebras_messages[-1]['content'].append({'type': 'text', 'text': json_prompt})else:# Add as a new user message- cerebras_messages.append({- 'role': 'user',- 'content': json_prompt- })-+ cerebras_messages.append({'role': 'user', 'content': json_prompt})+resp = await client.chat.completions.create( # type: ignoremodel=self.model,messages=cerebras_messages, # type: ignore@@ -172,17 +166,18 @@ Your response must be valid JSON only, no other text.content = resp.choices[0].message.contentif not content:raise ModelProviderError('Empty JSON content in Cerebras response', model=self.name)-+usage = self._get_usage(resp)-+# Try to extract JSON from the responseimport re+json_match = re.search(r'\{.*\}', content, re.DOTALL)if json_match:json_str = json_match.group(0)else:json_str = content-+parsed = output_format.model_validate_json(json_str)return ChatInvokeCompletion(completion=parsed,diff --git a/examples/models/cerebras_example.py b/examples/models/cerebras_example.pyindex 77eb1bc98..1edb199e8 100644--- a/examples/models/cerebras_example.py+++ b/examples/models/cerebras_example.py@@ -27,51 +27,53 @@ Large/Capable models (70B-480B):Note: Cerebras has some limitations with complex structured output due to JSON schema compatibility."""-import osimport asyncio+import os+from browser_use import Agent-from browser_use.llm import ChatCerebras+async def main():- # Set your API key (recommended to use environment variable)- api_key = os.getenv("CEREBRAS_API_KEY")- if not api_key:- raise ValueError("Please set CEREBRAS_API_KEY environment variable")-- # Option 1: Use the pre-configured model instance (recommended)- from browser_use import llm-- # Choose your model:- # Small/Fast models:- # model = llm.cerebras_llama3_1_8b # 8B, fast- # model = llm.cerebras_llama_4_scout_17b_16e_instruct # 17B, instruction-tuned- # model = llm.cerebras_llama_4_maverick_17b_128e_instruct # 17B, extended context- # model = llm.cerebras_qwen_3_32b # 32B-- # Large/Capable models:- # model = llm.cerebras_llama3_3_70b # 70B, latest- # model = llm.cerebras_gpt_oss_120b # 120B, OpenAI's model- # model = llm.cerebras_qwen_3_235b_a22b_instruct_2507 # 235B, instruction-tuned- model = llm.cerebras_qwen_3_235b_a22b_thinking_2507 # 235B, complex reasoning- # model = llm.cerebras_qwen_3_coder_480b # 480B, code generation-- # Option 2: Create the model instance directly- # model = ChatCerebras(- # model="qwen-3-coder-480b", # or any other model ID- # api_key=os.getenv("CEREBRAS_API_KEY"),- # temperature=0.2,- # max_tokens=4096,- # )-- # Create and run the agent with a simple task- task = "Explain the concept of quantum entanglement in simple terms."- agent = Agent(task=task, llm=model)-- print(f"Running task with Cerebras {model.name} (ID: {model.model}): {task}")- history = await agent.run(max_steps=3)- result = history.final_result()-- print(f"Result: {result}")--if __name__ == "__main__":- asyncio.run(main())+ # Set your API key (recommended to use environment variable)+ api_key = os.getenv('CEREBRAS_API_KEY')+ if not api_key:+ raise ValueError('Please set CEREBRAS_API_KEY environment variable')++ # Option 1: Use the pre-configured model instance (recommended)+ from browser_use import llm++ # Choose your model:+ # Small/Fast models:+ # model = llm.cerebras_llama3_1_8b # 8B, fast+ # model = llm.cerebras_llama_4_scout_17b_16e_instruct # 17B, instruction-tuned+ # model = llm.cerebras_llama_4_maverick_17b_128e_instruct # 17B, extended context+ # model = llm.cerebras_qwen_3_32b # 32B++ # Large/Capable models:+ # model = llm.cerebras_llama3_3_70b # 70B, latest+ # model = llm.cerebras_gpt_oss_120b # 120B, OpenAI's model+ # model = llm.cerebras_qwen_3_235b_a22b_instruct_2507 # 235B, instruction-tuned+ model = llm.cerebras_qwen_3_235b_a22b_thinking_2507 # 235B, complex reasoning+ # model = llm.cerebras_qwen_3_coder_480b # 480B, code generation++ # Option 2: Create the model instance directly+ # model = ChatCerebras(+ # model="qwen-3-coder-480b", # or any other model ID+ # api_key=os.getenv("CEREBRAS_API_KEY"),+ # temperature=0.2,+ # max_tokens=4096,+ # )++ # Create and run the agent with a simple task+ task = 'Explain the concept of quantum entanglement in simple terms.'+ agent = Agent(task=task, llm=model)++ print(f'Running task with Cerebras {model.name} (ID: {model.model}): {task}')+ history = await agent.run(max_steps=3)+ result = history.final_result()++ print(f'Result: {result}')+++if __name__ == '__main__':+ asyncio.run(main())