Skip to content

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

add litellm

STJ90cb6e8b7d03
diff --git a/browser_use/__init__.py b/browser_use/__init__.py
index d275a4f16..37a595b7f 100644
--- a/browser_use/__init__.py
+++ b/browser_use/__init__.py
@@ -60,6 +60,7 @@ if TYPE_CHECKING:
from browser_use.llm.browser_use.chat import ChatBrowserUse
from browser_use.llm.google.chat import ChatGoogle
from browser_use.llm.groq.chat import ChatGroq
+ from browser_use.llm.litellm.chat import ChatLiteLLM
from browser_use.llm.mistral.chat import ChatMistral
from browser_use.llm.oci_raw.chat import ChatOCIRaw
from browser_use.llm.ollama.chat import ChatOllama
@@ -95,6 +96,7 @@ _LAZY_IMPORTS = {
'ChatAnthropic': ('browser_use.llm.anthropic.chat', 'ChatAnthropic'),
'ChatBrowserUse': ('browser_use.llm.browser_use.chat', 'ChatBrowserUse'),
'ChatGroq': ('browser_use.llm.groq.chat', 'ChatGroq'),
+ 'ChatLiteLLM': ('browser_use.llm.litellm.chat', 'ChatLiteLLM'),
'ChatMistral': ('browser_use.llm.mistral.chat', 'ChatMistral'),
'ChatAzureOpenAI': ('browser_use.llm.azure.chat', 'ChatAzureOpenAI'),
'ChatOCIRaw': ('browser_use.llm.oci_raw.chat', 'ChatOCIRaw'),
@@ -148,6 +150,7 @@ __all__ = [
'ChatAnthropic',
'ChatBrowserUse',
'ChatGroq',
+ 'ChatLiteLLM',
'ChatMistral',
'ChatAzureOpenAI',
'ChatOCIRaw',
diff --git a/browser_use/llm/litellm/__init__.py b/browser_use/llm/litellm/__init__.py
new file mode 100644
index 000000000..26d5f1e70
--- /dev/null
+++ b/browser_use/llm/litellm/__init__.py
@@ -0,0 +1,3 @@
+from browser_use.llm.litellm.chat import ChatLiteLLM
+
+__all__ = ['ChatLiteLLM']
diff --git a/browser_use/llm/litellm/chat.py b/browser_use/llm/litellm/chat.py
new file mode 100644
index 000000000..1c0707491
--- /dev/null
+++ b/browser_use/llm/litellm/chat.py
@@ -0,0 +1,218 @@
+import logging
+from dataclasses import dataclass, field
+from typing import Any, TypeVar, overload
+
+from pydantic import BaseModel
+
+from browser_use.llm.base import BaseChatModel
+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
+
+from .serializer import LiteLLMMessageSerializer
+
+logger = logging.getLogger(__name__)
+
+T = TypeVar('T', bound=BaseModel)
+
+
+@dataclass
+class ChatLiteLLM(BaseChatModel):
+ model: str
+ api_key: str | None = None
+ api_base: str | None = None
+ temperature: float | None = 0.0
+ max_tokens: int | None = 4096
+ max_retries: int = 3
+ metadata: dict[str, Any] | None = None
+
+ _provider_name: str = field(default='', init=False, repr=False)
+ _clean_model: str = field(default='', init=False, repr=False)
+
+ def __post_init__(self) -> None:
+ """Resolve provider info from the model string via litellm."""
+ try:
+ from litellm import get_llm_provider
+
+ self._clean_model, self._provider_name, _, _ = get_llm_provider(self.model)
+ except Exception:
+ if '/' in self.model:
+ self._provider_name, self._clean_model = self.model.split('/', 1)
+ else:
+ self._provider_name = 'openai'
+ self._clean_model = self.model
+
+ logger.debug(
+ 'ChatLiteLLM initialized: model=%s, provider=%s, clean=%s, api_base=%s',
+ self.model,
+ self._provider_name,
+ self._clean_model,
+ self.api_base or '(default)',
+ )
+
+ @property
+ def provider(self) -> str:
+ return self._provider_name or 'litellm'
+
+ @property
+ def name(self) -> str:
+ return self._clean_model or self.model
+
+ @staticmethod
+ def _parse_usage(response: Any) -> ChatInvokeUsage | None:
+ """Extract token usage from a litellm response."""
+ usage = getattr(response, 'usage', None)
+ if usage is None:
+ return None
+
+ prompt_tokens = getattr(usage, 'prompt_tokens', 0) or 0
+ completion_tokens = getattr(usage, 'completion_tokens', 0) or 0
+
+ prompt_cached = getattr(usage, 'cache_read_input_tokens', None)
+ cache_creation = getattr(usage, 'cache_creation_input_tokens', None)
+
+ if prompt_cached is None:
+ details = getattr(usage, 'prompt_tokens_details', None)
+ if details:
+ prompt_cached = getattr(details, 'cached_tokens', None)
+
+ return ChatInvokeUsage(
+ prompt_tokens=prompt_tokens,
+ prompt_cached_tokens=int(prompt_cached) if prompt_cached is not None else None,
+ prompt_cache_creation_tokens=int(cache_creation) if cache_creation is not None else None,
+ prompt_image_tokens=None,
+ completion_tokens=completion_tokens,
+ total_tokens=prompt_tokens + completion_tokens,
+ )
+
+ @overload
+ async def ainvoke(
+ self,
+ messages: list[BaseMessage],
+ output_format: None = None,
+ **kwargs: Any,
+ ) -> ChatInvokeCompletion[str]: ...
+
+ @overload
+ async def ainvoke(
+ self,
+ messages: list[BaseMessage],
+ output_format: type[T],
+ **kwargs: Any,
+ ) -> ChatInvokeCompletion[T]: ...
+
+ async def ainvoke(
+ self,
+ messages: list[BaseMessage],
+ output_format: type[T] | None = None,
+ **kwargs: Any,
+ ) -> ChatInvokeCompletion[T] | ChatInvokeCompletion[str]:
+ from litellm import acompletion
+ from litellm.exceptions import APIConnectionError, APIError, RateLimitError, Timeout
+ from litellm.types.utils import ModelResponse
+
+ litellm_messages = LiteLLMMessageSerializer.serialize(messages)
+
+ params: dict[str, Any] = {
+ 'model': self.model,
+ 'messages': litellm_messages,
+ 'num_retries': self.max_retries,
+ }
+
+ if self.temperature is not None:
+ params['temperature'] = self.temperature
+ if self.max_tokens is not None:
+ params['max_tokens'] = self.max_tokens
+ if self.api_key:
+ params['api_key'] = self.api_key
+ if self.api_base:
+ params['api_base'] = self.api_base
+ if self.metadata:
+ params['metadata'] = self.metadata
+
+ if output_format is not None:
+ schema = SchemaOptimizer.create_optimized_json_schema(output_format)
+ params['response_format'] = {
+ 'type': 'json_schema',
+ 'json_schema': {
+ 'name': 'agent_output',
+ 'strict': True,
+ 'schema': schema,
+ },
+ }
+
+ try:
+ raw_response = await acompletion(**params)
+ except RateLimitError as e:
+ raise ModelRateLimitError(
+ message=str(e),
+ model=self.name,
+ ) from e
+ except Timeout as e:
+ raise ModelProviderError(
+ message=f'Request timed out: {e}',
+ model=self.name,
+ ) from e
+ except APIConnectionError as e:
+ raise ModelProviderError(
+ message=str(e),
+ model=self.name,
+ ) from e
+ except APIError as e:
+ status = getattr(e, 'status_code', 502) or 502
+ raise ModelProviderError(
+ message=str(e),
+ status_code=status,
+ model=self.name,
+ ) from e
+ except ModelProviderError:
+ raise
+ except Exception as e:
+ raise ModelProviderError(
+ message=str(e),
+ model=self.name,
+ ) from e
+
+ assert isinstance(raw_response, ModelResponse), f'Expected ModelResponse, got {type(raw_response)}'
+ response: ModelResponse = raw_response
+
+ choice = response.choices[0] if response.choices else None
+ if choice is None:
+ raise ModelProviderError(
+ message='Empty response: no choices returned by the model',
+ status_code=502,
+ model=self.name,
+ )
+
+ content = choice.message.content or ''
+ usage = self._parse_usage(response)
+ stop_reason = choice.finish_reason
+
+ thinking: str | None = None
+ msg_obj = choice.message
+ reasoning = getattr(msg_obj, 'reasoning_content', None)
+ if reasoning:
+ thinking = str(reasoning)
+
+ if output_format is not None:
+ if not content:
+ raise ModelProviderError(
+ message='Model returned empty content for structured output request',
+ status_code=500,
+ model=self.name,
+ )
+ parsed = output_format.model_validate_json(content)
+ return ChatInvokeCompletion(
+ completion=parsed,
+ thinking=thinking,
+ usage=usage,
+ stop_reason=stop_reason,
+ )
+
+ return ChatInvokeCompletion(
+ completion=content,
+ thinking=thinking,
+ usage=usage,
+ stop_reason=stop_reason,
+ )
diff --git a/browser_use/llm/litellm/serializer.py b/browser_use/llm/litellm/serializer.py
new file mode 100644
index 000000000..6ac90f557
--- /dev/null
+++ b/browser_use/llm/litellm/serializer.py
@@ -0,0 +1,120 @@
+from typing import Any
+
+from browser_use.llm.messages import (
+ AssistantMessage,
+ BaseMessage,
+ ContentPartImageParam,
+ ContentPartTextParam,
+ SystemMessage,
+ UserMessage,
+)
+
+
+class LiteLLMMessageSerializer:
+ @staticmethod
+ def _serialize_user_content(
+ content: str | list[ContentPartTextParam | ContentPartImageParam],
+ ) -> str | list[dict[str, Any]]:
+ if isinstance(content, str):
+ return content
+
+ parts: list[dict[str, Any]] = []
+ for part in content:
+ if part.type == 'text':
+ parts.append(
+ {
+ 'type': 'text',
+ 'text': part.text,
+ }
+ )
+ elif part.type == 'image_url':
+ parts.append(
+ {
+ 'type': 'image_url',
+ 'image_url': {
+ 'url': part.image_url.url,
+ 'detail': part.image_url.detail,
+ },
+ }
+ )
+ return parts
+
+ @staticmethod
+ def _serialize_system_content(
+ content: str | list[ContentPartTextParam],
+ ) -> str | list[dict[str, Any]]:
+ if isinstance(content, str):
+ return content
+
+ return [
+ {
+ 'type': 'text',
+ 'text': p.text,
+ }
+ for p in content
+ ]
+
+ @staticmethod
+ def _serialize_assistant_content(
+ content: str | list[Any] | None,
+ ) -> str | list[dict[str, Any]] | None:
+ if content is None:
+ return None
+ if isinstance(content, str):
+ return content
+
+ parts = []
+ for part in content:
+ if part.type == 'text':
+ parts.append(
+ {
+ 'type': 'text',
+ 'text': part.text,
+ }
+ )
+ elif part.type == 'refusal':
+ parts.append(
+ {
+ 'type': 'text',
+ 'text': f'[Refusal] {part.refusal}',
+ }
+ )
+ return parts
+
+ @staticmethod
+ def serialize(messages: list[BaseMessage]) -> list[dict[str, Any]]:
+ result: list[dict[str, Any]] = []
+ for msg in messages:
+ if isinstance(msg, UserMessage):
+ d: dict[str, Any] = {'role': 'user'}
+ d['content'] = LiteLLMMessageSerializer._serialize_user_content(msg.content)
+ if msg.name is not None:
+ d['name'] = msg.name
+ result.append(d)
+
+ elif isinstance(msg, SystemMessage):
+ d = {'role': 'system'}
+ d['content'] = LiteLLMMessageSerializer._serialize_system_content(msg.content)
+ if msg.name is not None:
+ d['name'] = msg.name
+ result.append(d)
+
+ elif isinstance(msg, AssistantMessage):
+ d = {'role': 'assistant'}
+ d['content'] = LiteLLMMessageSerializer._serialize_assistant_content(msg.content)
+ if msg.name is not None:
+ d['name'] = msg.name
+ if msg.tool_calls:
+ d['tool_calls'] = [
+ {
+ 'id': tc.id,
+ 'type': 'function',
+ 'function': {
+ 'name': tc.function.name,
+ 'arguments': tc.function.arguments,
+ },
+ }
+ for tc in msg.tool_calls
+ ]
+ result.append(d)
+ return result
diff --git a/pyproject.toml b/pyproject.toml
index f97fc18bf..733644643 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -48,6 +48,7 @@ dependencies = [
"markdownify==1.2.2",
"python-docx==1.2.0",
"browser-use-sdk==2.0.15",
+ "litellm>=1.82.2",
]
# google-api-core: only used for Google LLM APIs
# pyperclip: only used for examples that use copy/paste