Skip to content

Commit 8180637

Browse files
colriotErick Friiseyurtsev
authored
community[patch]: Fix Playwright Tools bug with Pydantic schemas (#27050)
- Add tests for Playwright tools schema serialization - Introduce base empty args Input class for BaseBrowserTool Test Plan: `poetry run pytest tests/unit_tests/tools/playwright/test_all.py` Fixes #26758 --------- Co-authored-by: Erick Friis <erick@langchain.dev> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
1 parent 92024d0 commit 8180637

File tree

6 files changed

+42
-3
lines changed

6 files changed

+42
-3
lines changed

libs/community/extended_testing_deps.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ oracledb>=2.2.0,<3
5858
pandas>=2.0.1,<3
5959
pdfminer-six>=20221105,<20240706
6060
pgvector>=0.1.6,<0.2
61+
playwright>=1.48.0,<2
6162
praw>=7.7.1,<8
6263
premai>=0.3.25,<0.4
6364
psychicapi>=0.8.0,<0.9

libs/community/langchain_community/tools/playwright/current_page.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
)
1616

1717

18+
class CurrentWebPageToolInput(BaseModel):
19+
"""Explicit no-args input for CurrentWebPageTool."""
20+
21+
1822
class CurrentWebPageTool(BaseBrowserTool): # type: ignore[override, override]
1923
"""Tool for getting the URL of the current webpage."""
2024

2125
name: str = "current_webpage"
2226
description: str = "Returns the URL of the current page"
23-
args_schema: Type[BaseModel] = BaseModel
27+
args_schema: Type[BaseModel] = CurrentWebPageToolInput
2428

2529
def _run(
2630
self,

libs/community/langchain_community/tools/playwright/extract_text.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
)
1616

1717

18+
class ExtractTextToolInput(BaseModel):
19+
"""Explicit no-args input for ExtractTextTool."""
20+
21+
1822
class ExtractTextTool(BaseBrowserTool): # type: ignore[override, override]
1923
"""Tool for extracting all the text on the current webpage."""
2024

2125
name: str = "extract_text"
2226
description: str = "Extract all the text on the current webpage"
23-
args_schema: Type[BaseModel] = BaseModel
27+
args_schema: Type[BaseModel] = ExtractTextToolInput
2428

2529
@model_validator(mode="before")
2630
@classmethod

libs/community/langchain_community/tools/playwright/navigate_back.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@
1515
)
1616

1717

18+
class NavigateBackToolInput(BaseModel):
19+
"""Explicit no-args input for NavigateBackTool."""
20+
21+
1822
class NavigateBackTool(BaseBrowserTool): # type: ignore[override, override]
1923
"""Navigate back to the previous page in the browser history."""
2024

2125
name: str = "previous_webpage"
2226
description: str = "Navigate back to the previous page in the browser history"
23-
args_schema: Type[BaseModel] = BaseModel
27+
args_schema: Type[BaseModel] = NavigateBackToolInput
2428

2529
def _run(self, run_manager: Optional[CallbackManagerForToolRun] = None) -> str:
2630
"""Use the tool."""

libs/community/tests/unit_tests/tools/playwright/__init__.py

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Test Playwright's Tools."""
2+
3+
from unittest.mock import Mock
4+
5+
import pytest
6+
7+
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
8+
9+
10+
@pytest.mark.requires("playwright")
11+
@pytest.mark.requires("bs4")
12+
def test_playwright_tools_schemas() -> None:
13+
"""Test calling 'tool_call_schema' for every tool to check to init issues."""
14+
15+
from playwright.sync_api import Browser
16+
17+
sync_browser = Mock(spec=Browser)
18+
tools = PlayWrightBrowserToolkit.from_browser(sync_browser=sync_browser).get_tools()
19+
20+
for tool in tools:
21+
try:
22+
tool.tool_call_schema
23+
except Exception as e:
24+
raise AssertionError(
25+
f"Error for '{tool.name}' tool: {type(e).__name__}: {e}"
26+
) from e

0 commit comments

Comments
 (0)