Skip to content

Commit d9f189c

Browse files
brucearctorwuliang229
authored andcommitted
fix: improve error message when beautifulsoup4/lxml not installed for load_web_page
Merge google#4853 ## Description The built-in `load_web_page` tool requires `beautifulsoup4` and `lxml`, which are available via the `[extensions]` optional dependency group. When a user installs `google-adk` without the `[extensions]` extra and calls `load_web_page`, they get a raw `ModuleNotFoundError: No module named 'bs4'` with no guidance on how to resolve it. This change wraps the deferred imports in a `try/except` to provide a clear, actionable error message: ``` ImportError: load_web_page requires the "beautifulsoup4" and "lxml" packages. Install them with: pip install google-adk[extensions] ``` Fixes google#4852 Co-authored-by: Liang Wu <wuliang@google.com> COPYBARA_INTEGRATE_REVIEW=google#4853 from brucearctor:fix/load-web-page-import-error 27aa20e PiperOrigin-RevId: 933416379
1 parent 06959b9 commit d9f189c

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ optional-dependencies.test = [
193193
"a2a-sdk>=0.3,<0.4",
194194
"anthropic>=0.78", # For anthropic model tests; 0.78 introduced ThinkingConfigAdaptiveParam (required for Claude Opus 4.7).
195195
"anyio>=4.9,<5",
196+
"beautifulsoup4>=3.2.2",
196197
"crewai[tools]; python_version>='3.11' and python_version<'3.12'", # For CrewaiTool tests; chromadb/pypika fail on 3.12+
197198
"e2b>=2,<3",
198199
"gepa>=0.1",
@@ -220,6 +221,7 @@ optional-dependencies.test = [
220221
"langgraph>=0.2.60,<0.4.8",
221222
"litellm>=1.83.7,<=1.83.14",
222223
"llama-index-readers-file>=0.4",
224+
"lxml>=5.3",
223225
"mcp>=1.24,<2",
224226
"openai>=1.100.2",
225227
"opentelemetry-exporter-gcp-logging>=1.9.0a0,<=1.12.0a0",

src/google/adk/tools/load_web_page.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,14 @@ def load_web_page(url: str) -> str:
286286
Returns:
287287
str: The text content of the url.
288288
"""
289-
from bs4 import BeautifulSoup
289+
try:
290+
from bs4 import BeautifulSoup
291+
import lxml # noqa: F401 -- verify lxml is available for the parser
292+
except ImportError as e:
293+
raise ImportError(
294+
'load_web_page requires the "beautifulsoup4" and "lxml" packages. '
295+
'Install them with: pip install google-adk[extensions]'
296+
) from e
290297

291298
try:
292299
response = _fetch_response(url)

0 commit comments

Comments
 (0)