Skip to content

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

Merge branch 'fix/issue-1716-event-driven-pause-resume' of https://github.com/MagellaX/browser-use into fix/issue-1716-event-driven-pause-resume

MagellaX1417d3979959
diff --cc browser_use/agent/views.py
index 1c3507bc0,6af10cac0..67cf405b1
--- a/browser_use/agent/views.py
+++ b/browser_use/agent/views.py
@@@ -1,6 -1,6 +1,5 @@@
from __future__ import annotations
--import asyncio
import json
import traceback
from dataclasses import dataclass
@@@ -90,8 -90,30 +87,7 @@@ class AgentState(BaseModel)
message_manager_state: MessageManagerState = Field(default_factory=MessageManagerState)
file_system_state: FileSystemState | None = None
- def __init__(self, **data):
- super().__init__(**data)
- # Ensure pause_event is initially set (not paused)
- if not self.pause_event.is_set():
- self.pause_event.set()
-
- @property
- def paused(self) -> bool:
- """Check if agent is paused by examining the pause event"""
- return not self.pause_event.is_set()
-
- def pause(self) -> None:
- """Pause the agent by clearing the pause event"""
- self.pause_event.clear()
-
- def resume(self) -> None:
- """Resume the agent by setting the pause event"""
- self.pause_event.set()
-
- async def wait_until_resumed(self) -> None:
- """Wait until the agent is resumed"""
- await self.pause_event.wait()
-
-
@dataclass
class AgentStepInfo:
step_number: int
diff --cc examples/models/langchain/chat.py
index 08502c4bf,5313189e5..30c2020cb
--- a/examples/models/langchain/chat.py
+++ b/examples/models/langchain/chat.py
@@@ -142,56 -139,24 +142,56 @@@ class ChatLangchain(BaseChatModel)
else:
# Use LangChain's structured output capability
- try:
- structured_chat = self.chat.with_structured_output(output_format)
- parsed_object = await structured_chat.ainvoke(langchain_messages)
+ structured_output_success = False
+ response = None
- # For structured output, usage metadata is typically not available
- # in the parsed object since it's a Pydantic model, not an AIMessage
- usage = None
+ # First, try to use structured output if not disabled
+ if not self.disable_structured_output:
+ try:
+ # For LangChain OpenAI models, disable json_schema mode if it's causing issues
+ if hasattr(self.chat, 'model_kwargs'):
+ # Temporarily modify model kwargs to use json_mode instead of json_schema
+ original_kwargs = getattr(self.chat, 'model_kwargs', {})
- self.chat.model_kwargs = {**original_kwargs}
++ setattr(self.chat, 'model_kwargs', {**original_kwargs})
+
+ # Check if this is a ChatOpenAI model with structured output issues
+ if self.chat.__class__.__name__ == 'ChatOpenAI':
+ # Use method="function_calling" instead of default "json_mode"
+ structured_chat = self.chat.with_structured_output(output_format, method='function_calling')
+ else:
+ structured_chat = self.chat.with_structured_output(output_format)
+ else:
+ structured_chat = self.chat.with_structured_output(output_format)
- # Type cast since LangChain's with_structured_output returns the correct type
- return ChatInvokeCompletion(
- completion=parsed_object, # type: ignore
- usage=usage,
- )
- except AttributeError:
- # Fall back to manual parsing if with_structured_output is not available
+ parsed_object = await structured_chat.ainvoke(langchain_messages)
+ structured_output_success = True
+
+ # For structured output, usage metadata is typically not available
+ # in the parsed object since it's a Pydantic model, not an AIMessage
+ usage = None
+
+ # Type cast since LangChain's with_structured_output returns the correct type
+ return ChatInvokeCompletion(
+ completion=parsed_object, # type: ignore
+ usage=usage,
+ )
+ except Exception as e:
+ # If structured output fails, fall back to manual parsing
+ # This handles cases where the API doesn't support json_schema
+ if 'json_schema' in str(e) or 'response_format' in str(e):
+ # Fall through to manual parsing
+ pass
+ else:
+ # Re-raise other errors
+ raise
+
+ # Fall back to manual parsing if structured output failed or was disabled
+ if not structured_output_success:
response = await self.chat.ainvoke(langchain_messages) # type: ignore
- if not isinstance(response, 'LangChainAIMessage'):
+ from langchain_core.messages import AIMessage as LangChainAIMessage # type: ignore
+
+ if not isinstance(response, LangChainAIMessage):
raise ModelProviderError(
message=f'Response is not an AIMessage: {type(response)}',
model=self.name,
@@@ -239,3 -193,3 +239,9 @@@
message=f'LangChain model error: {str(e)}',
model=self.name,
) from e
++
++ # This should never be reached, but add fallback for type checker
++ raise ModelProviderError(
++ message='Unexpected code path reached in ainvoke',
++ model=self.name,
++ )