From 95009f7d95e02cbebbdad6f0bd0a00bb682aa32a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=AE=AE=E0=AE=A9=E0=AF=8B=E0=AE=9C=E0=AF=8D=E0=AE=95?= =?UTF-8?q?=E0=AF=81=E0=AE=AE=E0=AE=BE=E0=AE=B0=E0=AF=8D=20=E0=AE=AA?= =?UTF-8?q?=E0=AE=B4=E0=AE=A9=E0=AE=BF=E0=AE=9A=E0=AF=8D=E0=AE=9A=E0=AE=BE?= =?UTF-8?q?=E0=AE=AE=E0=AE=BF?= Date: Sat, 19 Jul 2025 13:53:39 +0530 Subject: [PATCH] Add runtime type check for llm in Agent init Added a type check to ensure the llm argument is an instance of BaseChatModel in Agent's constructor, raising a ValueError otherwise. Also marked BaseChatModel as @runtime_checkable to support isinstance checks. --- browser_use/agent/service.py | 2 ++ browser_use/llm/base.py | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/browser_use/agent/service.py b/browser_use/agent/service.py index 8fd81ccdd..783694533 100644 --- a/browser_use/agent/service.py +++ b/browser_use/agent/service.py @@ -185,6 +185,8 @@ class Agent(Generic[Context, AgentStructuredOutput]): include_tool_call_examples: bool = False, **kwargs, ): + if not isinstance(llm, BaseChatModel): + raise ValueError('invalid llm, must be from browser_use.llm') # Check for deprecated planner parameters planner_params = [planner_llm, use_vision_for_planner, is_planner_reasoning, extend_planner_system_message] if any(param is not None and param is not False for param in planner_params) or planner_interval != 1: diff --git a/browser_use/llm/base.py b/browser_use/llm/base.py index 6bc4ae563..0e623d780 100644 --- a/browser_use/llm/base.py +++ b/browser_use/llm/base.py @@ -4,7 +4,7 @@ We have switched all of our code from langchain to openai.types.chat.chat_comple For easier transition we have """ -from typing import Any, Protocol, TypeVar, overload +from typing import Any, Protocol, TypeVar, overload, runtime_checkable from pydantic import BaseModel @@ -14,6 +14,7 @@ from browser_use.llm.views import ChatInvokeCompletion T = TypeVar('T', bound=BaseModel) +@runtime_checkable class BaseChatModel(Protocol): _verified_api_keys: bool = False