Skip to content

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

linter

kevint-cerebras82752d53805b
diff --git a/browser_use/llm/cerebras/chat.py b/browser_use/llm/cerebras/chat.py
index 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 json
from dataclasses import dataclass
from 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, ResponseFormatJSONSchema
from pydantic import BaseModel
from browser_use.llm.base import BaseChatModel
from browser_use.llm.cerebras.serializer import CerebrasMessageSerializer
from browser_use.llm.exceptions import ModelProviderError, ModelRateLimitError
from browser_use.llm.messages import BaseMessage
-from browser_use.llm.schema import SchemaOptimizer
from browser_use.llm.views import ChatInvokeCompletion, ChatInvokeUsage
T = 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 it
import json
-
+
# Get the schema to guide the model
schema = output_format.model_json_schema()
schema_str = json.dumps(schema, indent=2)
-
+
# Create a prompt that asks for the specific JSON structure
json_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 prompt
if 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: ignore
model=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.content
if not content:
raise ModelProviderError('Empty JSON content in Cerebras response', model=self.name)
-
+
usage = self._get_usage(resp)
-
+
# Try to extract JSON from the response
import 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.py
index 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 os
import 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())