diff --git a/.env.example b/.env.example index d92fa916..28799b2c 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,2 @@ BROWSERBASE_API_KEY=bb_live_your_api_key_here -BROWSERBASE_PROJECT_ID=your-bb-project-uuid-here -MODEL_API_KEY=sk-proj-your-llm-api-key-here \ No newline at end of file +MODEL_API_KEY=sk-proj-your-llm-api-key-here diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 2ebc4e90..f80372ae 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "3.19.5" + ".": "3.20.0" } diff --git a/.stats.yml b/.stats.yml index ade26eb1..391cde37 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1,4 +1,4 @@ configured_endpoints: 8 -openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase%2Fstagehand-b969ce378479c79ee64c05127c0ed6c6ce2edbee017ecd037242fb618a5ebc9f.yml -openapi_spec_hash: a24aabaa5214effb679808b7f2be0ad4 -config_hash: 0cc516caf1432087f40654336e0fa8cd +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase/stagehand-6f6bfb81d092f30a5e2005328c97d61b9ea36132bb19e9e79e55294b9534ce20.yml +openapi_spec_hash: f3fc1e3688a38dc2c28f7178f7d534e5 +config_hash: 1fb12ae9b478488bc1e56bfbdc210b01 diff --git a/CHANGELOG.md b/CHANGELOG.md index bbaf6ba1..c5ab97a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,37 @@ # Changelog +## 3.20.0 (2026-05-06) + +Full Changelog: [v3.19.5...v3.20.0](https://github.com/browserbase/stagehand-python/compare/v3.19.5...v3.20.0) + +### Features + +* [feat]: add `ignoreSelectors` to `extract()` ([f11a102](https://github.com/browserbase/stagehand-python/commit/f11a102884344ddefd2dd0b98f22fea687a12e6e)) +* [STG-1798] feat: support Browserbase verified sessions ([078ab5c](https://github.com/browserbase/stagehand-python/commit/078ab5c76f13beee16e44d7eed5e3018f1cc5bd8)) +* [STG-1808] Deprecate Browserbase project ID ([bfe505c](https://github.com/browserbase/stagehand-python/commit/bfe505cb1c3c1c6a85d7873ee05860476d71a9d6)) +* Bedrock auth passthrough ([9463fa4](https://github.com/browserbase/stagehand-python/commit/9463fa49cb839abbb2c6a1adb0d053e5006216a7)) +* remove experimental requirement on agent variables ([#2079](https://github.com/browserbase/stagehand-python/issues/2079)) ([92ed1e1](https://github.com/browserbase/stagehand-python/commit/92ed1e1d43e89d92ffc4c3aa30c7f1a521f5d0e6)) +* Revert "[STG-1573] Add providerOptions for extensible model auth ([#1822](https://github.com/browserbase/stagehand-python/issues/1822))" ([3c04086](https://github.com/browserbase/stagehand-python/commit/3c0408675154c9f7d241c4e92e9cb82f0419d6b3)) +* support setting headers via env ([b1c6127](https://github.com/browserbase/stagehand-python/commit/b1c61276ff9cecf89c078e1d97a6b927d8d308e1)) + + +### Bug Fixes + +* **client:** preserve hardcoded query params when merging with user params ([feac39d](https://github.com/browserbase/stagehand-python/commit/feac39d88a841ff710d672b622f3280037589f66)) +* ensure file data are only sent as 1 parameter ([b870657](https://github.com/browserbase/stagehand-python/commit/b8706575ab0f95b9e6781ee3685f9b79e0fe6036)) +* use correct field name format for multipart file arrays ([852600a](https://github.com/browserbase/stagehand-python/commit/852600a4c6169df6497fc4d77a06abae8812e375)) + + +### Performance Improvements + +* **client:** optimize file structure copying in multipart requests ([69d396f](https://github.com/browserbase/stagehand-python/commit/69d396fa404d0ad3905786dcf6f56be8be7ab9b2)) + + +### Chores + +* **internal:** more robust bootstrap script ([9bc61e1](https://github.com/browserbase/stagehand-python/commit/9bc61e12f1599b14dd8b2a0c38a50c089512e9c7)) +* **internal:** reformat pyproject.toml ([03f4dbe](https://github.com/browserbase/stagehand-python/commit/03f4dbe5ef6e230f3e96302ba8e5f4064edda10f)) + ## 3.19.5 (2026-04-03) Full Changelog: [v3.19.4...v3.19.5](https://github.com/browserbase/stagehand-python/compare/v3.19.4...v3.19.5) diff --git a/README.md b/README.md index bd9c0524..aff5444e 100644 --- a/README.md +++ b/README.md @@ -87,10 +87,8 @@ Python 3.9 or higher. Set your environment variables (from `examples/.env.example`): -- `STAGEHAND_API_URL` - `MODEL_API_KEY` - `BROWSERBASE_API_KEY` -- `BROWSERBASE_PROJECT_ID` ```bash cp examples/.env.example examples/.env @@ -139,7 +137,6 @@ def main() -> None: with Stagehand( server="remote", browserbase_api_key=os.environ.get("BROWSERBASE_API_KEY"), - browserbase_project_id=os.environ.get("BROWSERBASE_PROJECT_ID"), model_api_key=os.environ.get("MODEL_API_KEY"), ) as client: session = client.sessions.start( @@ -234,7 +231,6 @@ from stagehand import AsyncStagehand client = AsyncStagehand( browserbase_api_key="My Browserbase API Key", - browserbase_project_id="My Browserbase Project ID", model_api_key="My Model API Key", ) ``` @@ -255,9 +251,11 @@ See this table for the available options: | Keyword argument | Environment variable | Required | Default value | | ------------------------ | ------------------------ | -------- | ----------------------------------------- | | `browserbase_api_key` | `BROWSERBASE_API_KEY` | true | - | -| `browserbase_project_id` | `BROWSERBASE_PROJECT_ID` | true | - | +| `browserbase_project_id` | - | false | - | | `model_api_key` | `MODEL_API_KEY` | true | - | -| `base_url` | `STAGEHAND_BASE_URL` | false | `"https://api.stagehand.browserbase.com"` | +| `base_url` | `STAGEHAND_API_URL` | false | `"https://api.stagehand.browserbase.com"` | + +`browserbase_project_id` is deprecated, accepted for backwards compatibility, and ignored. `STAGEHAND_BASE_URL` remains supported as a deprecated fallback when `STAGEHAND_API_URL` is unset. Keyword arguments take precedence over environment variables. @@ -662,7 +660,7 @@ import httpx from stagehand import Stagehand, DefaultHttpxClient client = Stagehand( - # Or use the `STAGEHAND_BASE_URL` env var + # Or use the `STAGEHAND_API_URL` env var, with `STAGEHAND_BASE_URL` as a fallback base_url="http://my.test.server.example.com:8083", http_client=DefaultHttpxClient( proxy="http://my.test.proxy.example.com", diff --git a/examples/.env.example b/examples/.env.example index 6272bb0b..0033442c 100644 --- a/examples/.env.example +++ b/examples/.env.example @@ -1,4 +1,2 @@ -STAGEHAND_API_URL=https://api.stagehand.browserbase.com MODEL_API_KEY=sk-proj-your-llm-api-key-here BROWSERBASE_API_KEY=bb_live_your_api_key_here -BROWSERBASE_PROJECT_ID=your-bb-project-uuid-here diff --git a/examples/act_example.py b/examples/act_example.py index 68d2e53e..88108d86 100644 --- a/examples/act_example.py +++ b/examples/act_example.py @@ -10,7 +10,6 @@ Required environment variables: - BROWSERBASE_API_KEY: Your Browserbase API key -- BROWSERBASE_PROJECT_ID: Your Browserbase project ID - MODEL_API_KEY: Your OpenAI API key """ @@ -45,10 +44,9 @@ async def main() -> None: load_example_env() load_example_env() # Create client using environment variables - # BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, MODEL_API_KEY + # BROWSERBASE_API_KEY and MODEL_API_KEY async with AsyncStagehand( browserbase_api_key=os.environ.get("BROWSERBASE_API_KEY"), - browserbase_project_id=os.environ.get("BROWSERBASE_PROJECT_ID"), model_api_key=os.environ.get("MODEL_API_KEY"), ) as client: # Start a new browser session diff --git a/examples/agent_execute.py b/examples/agent_execute.py index 8d44f57e..70171d80 100644 --- a/examples/agent_execute.py +++ b/examples/agent_execute.py @@ -3,7 +3,6 @@ Required environment variables: - BROWSERBASE_API_KEY -- BROWSERBASE_PROJECT_ID - MODEL_API_KEY Optional: diff --git a/examples/byob_example.py b/examples/byob_example.py index b6180fbe..34d2adf3 100644 --- a/examples/byob_example.py +++ b/examples/byob_example.py @@ -6,7 +6,6 @@ Required environment variables: - BROWSERBASE_API_KEY -- BROWSERBASE_PROJECT_ID - MODEL_API_KEY Usage: @@ -52,7 +51,6 @@ async def main() -> None: load_example_env() async with AsyncStagehand( browserbase_api_key=os.environ.get("BROWSERBASE_API_KEY"), - browserbase_project_id=os.environ.get("BROWSERBASE_PROJECT_ID"), model_api_key=os.environ.get("MODEL_API_KEY"), ) as client, async_playwright() as playwright: browser = await playwright.chromium.launch(headless=True) diff --git a/examples/env.py b/examples/env.py index ea047de1..57266552 100644 --- a/examples/env.py +++ b/examples/env.py @@ -4,10 +4,8 @@ from pathlib import Path REQUIRED_KEYS = { - "STAGEHAND_API_URL", "MODEL_API_KEY", "BROWSERBASE_API_KEY", - "BROWSERBASE_PROJECT_ID", } @@ -44,9 +42,6 @@ def load_example_env() -> None: + " (from examples/.env)" ) - # Normalize for SDKs that expect STAGEHAND_BASE_URL - os.environ.setdefault("STAGEHAND_BASE_URL", os.environ["STAGEHAND_API_URL"]) - # Use the repo-local SEA binary when available (avoid global installs). sea_binary = env_path.parent.parent / "bin" / "sea" / "stagehand-darwin-arm64" if sea_binary.exists(): diff --git a/examples/full_example.py b/examples/full_example.py index 26c27766..0ccbb450 100644 --- a/examples/full_example.py +++ b/examples/full_example.py @@ -12,7 +12,6 @@ Required environment variables: - BROWSERBASE_API_KEY: Your Browserbase API key -- BROWSERBASE_PROJECT_ID: Your Browserbase project ID - MODEL_API_KEY: Your OpenAI API key """ @@ -47,10 +46,9 @@ async def main() -> None: load_example_env() load_example_env() # Create client using environment variables - # BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, MODEL_API_KEY + # BROWSERBASE_API_KEY and MODEL_API_KEY async with AsyncStagehand( browserbase_api_key=os.environ.get("BROWSERBASE_API_KEY"), - browserbase_project_id=os.environ.get("BROWSERBASE_PROJECT_ID"), model_api_key=os.environ.get("MODEL_API_KEY"), ) as client: # Start a new browser session (returns a session helper bound to a session_id) diff --git a/examples/local_browser_playwright_example.py b/examples/local_browser_playwright_example.py index d3f208df..205e22c9 100644 --- a/examples/local_browser_playwright_example.py +++ b/examples/local_browser_playwright_example.py @@ -12,10 +12,9 @@ Environment variables required: - MODEL_API_KEY - BROWSERBASE_API_KEY (can be any value in local mode) -- BROWSERBASE_PROJECT_ID (can be any value in local mode) Optional: -- STAGEHAND_BASE_URL (defaults to http://127.0.0.1:3000) +- STAGEHAND_API_URL or STAGEHAND_BASE_URL (defaults to http://127.0.0.1:3000) """ from __future__ import annotations @@ -82,11 +81,8 @@ def main() -> None: sys.exit("Set the MODEL_API_KEY environment variable to run this example.") bb_api_key = os.environ.get("BROWSERBASE_API_KEY") - bb_project_id = os.environ.get("BROWSERBASE_PROJECT_ID") - if not bb_api_key or not bb_project_id: - sys.exit( - "Set BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID to run this example." - ) + if not bb_api_key: + sys.exit("Set BROWSERBASE_API_KEY to run this example.") try: from playwright.sync_api import sync_playwright # type: ignore[import-not-found] @@ -102,7 +98,6 @@ def main() -> None: with Stagehand( server="local", browserbase_api_key=bb_api_key, - browserbase_project_id=bb_project_id, model_api_key=model_api_key, local_ready_timeout_s=30.0, ) as client: diff --git a/examples/local_example.py b/examples/local_example.py index 440c69ac..38aa972b 100644 --- a/examples/local_example.py +++ b/examples/local_example.py @@ -4,7 +4,6 @@ Required environment variables: - BROWSERBASE_API_KEY (can be any value in local mode) -- BROWSERBASE_PROJECT_ID (can be any value in local mode) - MODEL_API_KEY (read by this example and passed explicitly to the client) diff --git a/examples/local_server_multiregion_browser_example.py b/examples/local_server_multiregion_browser_example.py index 2bb1ed61..ec7503d6 100644 --- a/examples/local_server_multiregion_browser_example.py +++ b/examples/local_server_multiregion_browser_example.py @@ -11,10 +11,9 @@ Environment variables required: - MODEL_API_KEY - BROWSERBASE_API_KEY -- BROWSERBASE_PROJECT_ID Optional: -- STAGEHAND_BASE_URL (defaults to http://127.0.0.1:3000 when server="local") +- STAGEHAND_API_URL or STAGEHAND_BASE_URL (defaults to http://127.0.0.1:3000 when server="local") """ from __future__ import annotations @@ -53,11 +52,8 @@ def main() -> None: sys.exit("Set the MODEL_API_KEY environment variable to run this example.") bb_api_key = os.environ.get("BROWSERBASE_API_KEY") - bb_project_id = os.environ.get("BROWSERBASE_PROJECT_ID") - if not bb_api_key or not bb_project_id: - sys.exit( - "Set BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID to run this example." - ) + if not bb_api_key: + sys.exit("Set BROWSERBASE_API_KEY to run this example.") try: from playwright.sync_api import sync_playwright # type: ignore[import-not-found] @@ -73,7 +69,6 @@ def main() -> None: with Stagehand( server="local", browserbase_api_key=bb_api_key, - browserbase_project_id=bb_project_id, model_api_key=model_api_key, local_ready_timeout_s=30.0, ) as client: diff --git a/examples/logging_example.py b/examples/logging_example.py index 881ee7a7..96bed5ff 100644 --- a/examples/logging_example.py +++ b/examples/logging_example.py @@ -4,7 +4,6 @@ Required environment variables: - BROWSERBASE_API_KEY: Your Browserbase API key -- BROWSERBASE_PROJECT_ID: Your Browserbase project ID - MODEL_API_KEY: Your OpenAI API key """ @@ -23,7 +22,6 @@ async def main() -> None: # Create client using environment variables async with AsyncStagehand( browserbase_api_key=os.environ.get("BROWSERBASE_API_KEY"), - browserbase_project_id=os.environ.get("BROWSERBASE_PROJECT_ID"), model_api_key=os.environ.get("MODEL_API_KEY"), ) as client: # Start a new browser session with verbose logging enabled diff --git a/examples/playwright_page_example.py b/examples/playwright_page_example.py index 1f032ee6..08ddac4d 100644 --- a/examples/playwright_page_example.py +++ b/examples/playwright_page_example.py @@ -10,10 +10,9 @@ Environment variables required: - MODEL_API_KEY - BROWSERBASE_API_KEY -- BROWSERBASE_PROJECT_ID Optional: -- STAGEHAND_BASE_URL (defaults to https://api.stagehand.browserbase.com) +- STAGEHAND_API_URL or STAGEHAND_BASE_URL (defaults to https://api.stagehand.browserbase.com) """ from __future__ import annotations @@ -52,11 +51,8 @@ def main() -> None: sys.exit("Set the MODEL_API_KEY environment variable to run this example.") bb_api_key = os.environ.get("BROWSERBASE_API_KEY") - bb_project_id = os.environ.get("BROWSERBASE_PROJECT_ID") - if not bb_api_key or not bb_project_id: - sys.exit( - "Set BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID to run this example." - ) + if not bb_api_key: + sys.exit("Set BROWSERBASE_API_KEY to run this example.") try: from playwright.sync_api import sync_playwright # type: ignore[import-not-found] @@ -72,7 +68,6 @@ def main() -> None: with Stagehand( server="remote", browserbase_api_key=bb_api_key, - browserbase_project_id=bb_project_id, model_api_key=model_api_key, ) as client: print("⏳ Starting Stagehand session...") diff --git a/examples/pydoll_tab_example.py b/examples/pydoll_tab_example.py index a7358edd..283831af 100644 --- a/examples/pydoll_tab_example.py +++ b/examples/pydoll_tab_example.py @@ -12,10 +12,9 @@ Environment variables required: - MODEL_API_KEY - BROWSERBASE_API_KEY -- BROWSERBASE_PROJECT_ID Optional: -- STAGEHAND_BASE_URL (defaults to https://api.stagehand.browserbase.com) +- STAGEHAND_API_URL or STAGEHAND_BASE_URL (defaults to https://api.stagehand.browserbase.com) Notes: - This example requires Python 3.10+ because `pydoll-python` requires Python 3.10+. @@ -121,9 +120,8 @@ async def main() -> None: sys.exit("Set the MODEL_API_KEY environment variable to run this example.") bb_api_key = os.environ.get("BROWSERBASE_API_KEY") - bb_project_id = os.environ.get("BROWSERBASE_PROJECT_ID") - if not bb_api_key or not bb_project_id: - sys.exit("Set BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID to run this example.") + if not bb_api_key: + sys.exit("Set BROWSERBASE_API_KEY to run this example.") try: from pydoll.browser.chromium import Chrome # type: ignore[import-not-found] @@ -138,7 +136,6 @@ async def main() -> None: async with AsyncStagehand( server="remote", browserbase_api_key=bb_api_key, - browserbase_project_id=bb_project_id, model_api_key=model_api_key, ) as client: print("⏳ Starting Stagehand session...") diff --git a/examples/remote_browser_playwright_example.py b/examples/remote_browser_playwright_example.py index 22e119cd..4645d21c 100644 --- a/examples/remote_browser_playwright_example.py +++ b/examples/remote_browser_playwright_example.py @@ -12,10 +12,9 @@ Environment variables required: - MODEL_API_KEY - BROWSERBASE_API_KEY -- BROWSERBASE_PROJECT_ID Optional: -- STAGEHAND_BASE_URL (defaults to https://api.stagehand.browserbase.com) +- STAGEHAND_API_URL or STAGEHAND_BASE_URL (defaults to https://api.stagehand.browserbase.com) """ from __future__ import annotations @@ -54,11 +53,8 @@ def main() -> None: sys.exit("Set the MODEL_API_KEY environment variable to run this example.") bb_api_key = os.environ.get("BROWSERBASE_API_KEY") - bb_project_id = os.environ.get("BROWSERBASE_PROJECT_ID") - if not bb_api_key or not bb_project_id: - sys.exit( - "Set BROWSERBASE_API_KEY and BROWSERBASE_PROJECT_ID to run this example." - ) + if not bb_api_key: + sys.exit("Set BROWSERBASE_API_KEY to run this example.") try: from playwright.sync_api import sync_playwright # type: ignore[import-not-found] @@ -74,7 +70,6 @@ def main() -> None: with Stagehand( server="remote", browserbase_api_key=bb_api_key, - browserbase_project_id=bb_project_id, model_api_key=model_api_key, ) as client: print("⏳ Starting Stagehand session...") diff --git a/pyproject.toml b/pyproject.toml index 96fa8963..d3411cde 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "stagehand" -version = "3.19.5" +version = "3.20.0" description = "The official Python library for the stagehand API" dynamic = ["readme"] license = "MIT" diff --git a/scripts/bootstrap b/scripts/bootstrap index 4638ec69..5a23841b 100755 --- a/scripts/bootstrap +++ b/scripts/bootstrap @@ -4,7 +4,7 @@ set -e cd "$(dirname "$0")/.." -if [ -f "Brewfile" ] && [ "$(uname -s)" = "Darwin" ] && [ "$SKIP_BREW" != "1" ] && [ -t 0 ]; then +if [ -f "Brewfile" ] && [ "$(uname -s)" = "Darwin" ] && [ "${SKIP_BREW:-}" != "1" ] && [ -t 0 ]; then brew bundle check >/dev/null 2>&1 || { echo -n "==> Install Homebrew dependencies? (y/N): " read -r response diff --git a/src/stagehand/_base_client.py b/src/stagehand/_base_client.py index 951feb82..9af0033b 100644 --- a/src/stagehand/_base_client.py +++ b/src/stagehand/_base_client.py @@ -540,6 +540,10 @@ def _build_request( files = cast(HttpxRequestFiles, ForceMultipartDict()) prepared_url = self._prepare_url(options.url) + # preserve hard-coded query params from the url + if params and prepared_url.query: + params = {**dict(prepared_url.params.items()), **params} + prepared_url = prepared_url.copy_with(raw_path=prepared_url.raw_path.split(b"?", 1)[0]) if "_" in prepared_url.host: # work around https://github.com/encode/httpx/discussions/2880 kwargs["extensions"] = {"sni_hostname": prepared_url.host.replace("_", "-")} diff --git a/src/stagehand/_client.py b/src/stagehand/_client.py index affa6420..0c405416 100644 --- a/src/stagehand/_client.py +++ b/src/stagehand/_client.py @@ -22,7 +22,11 @@ RequestOptions, not_given, ) -from ._utils import is_given, get_async_library +from ._utils import ( + is_given, + is_mapping_t, + get_async_library, +) from ._compat import cached_property from ._models import FinalRequestOptions from ._version import __version__ @@ -128,7 +132,6 @@ def __init__( This automatically infers the following arguments from their corresponding environment variables if they are not provided: - `browserbase_api_key` from `BROWSERBASE_API_KEY` - - `browserbase_project_id` from `BROWSERBASE_PROJECT_ID` `model_api_key` is intentionally not inferred from any AI provider environment variable. Pass it explicitly when you want the SDK to send `x-model-api-key` on remote requests or @@ -136,8 +139,6 @@ def __init__( """ if browserbase_api_key is None: browserbase_api_key = os.environ.get("BROWSERBASE_API_KEY") - if browserbase_project_id is None: - browserbase_project_id = os.environ.get("BROWSERBASE_PROJECT_ID") self.browserbase_api_key = browserbase_api_key self.browserbase_project_id = browserbase_project_id @@ -161,6 +162,15 @@ def __init__( ) ### + custom_headers_env = os.environ.get("STAGEHAND_CUSTOM_HEADERS") + if custom_headers_env is not None: + parsed: dict[str, str] = {} + for line in custom_headers_env.split("\n"): + colon = line.find(":") + if colon >= 0: + parsed[line[:colon].strip()] = line[colon + 1 :].strip() + default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})} + super().__init__( version=__version__, base_url=base_url, @@ -215,7 +225,7 @@ def qs(self) -> Querystring: @property @override def auth_headers(self) -> dict[str, str]: - return {**self._bb_api_key_auth, **self._bb_project_id_auth, **self._llm_model_api_key_auth} + return {**self._bb_api_key_auth, **self._llm_model_api_key_auth} ### @property @@ -225,8 +235,7 @@ def _bb_api_key_auth(self) -> dict[str, str]: @property def _bb_project_id_auth(self) -> dict[str, str]: - browserbase_project_id = self.browserbase_project_id - return {"x-bb-project-id": browserbase_project_id} if browserbase_project_id else {} + return {} @property def _llm_model_api_key_auth(self) -> dict[str, str]: @@ -414,7 +423,6 @@ def __init__( This automatically infers the following arguments from their corresponding environment variables if they are not provided: - `browserbase_api_key` from `BROWSERBASE_API_KEY` - - `browserbase_project_id` from `BROWSERBASE_PROJECT_ID` `model_api_key` is intentionally not inferred from any AI provider environment variable. Pass it explicitly when you want the SDK to send `x-model-api-key` on remote requests or @@ -422,8 +430,6 @@ def __init__( """ if browserbase_api_key is None: browserbase_api_key = os.environ.get("BROWSERBASE_API_KEY") - if browserbase_project_id is None: - browserbase_project_id = os.environ.get("BROWSERBASE_PROJECT_ID") self.browserbase_api_key = browserbase_api_key self.browserbase_project_id = browserbase_project_id @@ -447,6 +453,15 @@ def __init__( ) ### + custom_headers_env = os.environ.get("STAGEHAND_CUSTOM_HEADERS") + if custom_headers_env is not None: + parsed: dict[str, str] = {} + for line in custom_headers_env.split("\n"): + colon = line.find(":") + if colon >= 0: + parsed[line[:colon].strip()] = line[colon + 1 :].strip() + default_headers = {**parsed, **(default_headers if is_mapping_t(default_headers) else {})} + super().__init__( version=__version__, base_url=base_url, @@ -501,7 +516,7 @@ def qs(self) -> Querystring: @property @override def auth_headers(self) -> dict[str, str]: - return {**self._bb_api_key_auth, **self._bb_project_id_auth, **self._llm_model_api_key_auth} + return {**self._bb_api_key_auth, **self._llm_model_api_key_auth} ### @property @@ -511,8 +526,7 @@ def _bb_api_key_auth(self) -> dict[str, str]: @property def _bb_project_id_auth(self) -> dict[str, str]: - browserbase_project_id = self.browserbase_project_id - return {"x-bb-project-id": browserbase_project_id} if browserbase_project_id else {} + return {} @property def _llm_model_api_key_auth(self) -> dict[str, str]: diff --git a/src/stagehand/_custom/sea_server.py b/src/stagehand/_custom/sea_server.py index e31f7b4a..a72ed591 100644 --- a/src/stagehand/_custom/sea_server.py +++ b/src/stagehand/_custom/sea_server.py @@ -329,7 +329,7 @@ def configure_client_base_url( return base_url if base_url is None: - base_url = os.environ.get("STAGEHAND_BASE_URL") + base_url = os.environ.get("STAGEHAND_API_URL") or os.environ.get("STAGEHAND_BASE_URL") if base_url is None: base_url = "https://api.stagehand.browserbase.com" return base_url diff --git a/src/stagehand/_custom/session.py b/src/stagehand/_custom/session.py index 83d5ce1a..4ca99711 100644 --- a/src/stagehand/_custom/session.py +++ b/src/stagehand/_custom/session.py @@ -699,7 +699,7 @@ def _resolve_start_browser(client: Any, browser: session_start_params.Browser | if browser is not omit or getattr(client, "_server_mode", None) != "local": return browser - if client.browserbase_api_key is None or client.browserbase_project_id is None: + if client.browserbase_api_key is None: raise StagehandError( "Local server mode without Browserbase credentials requires an explicit local browser, " "e.g. browser={'type': 'local'}." diff --git a/src/stagehand/_files.py b/src/stagehand/_files.py index cc14c14f..0fdce17b 100644 --- a/src/stagehand/_files.py +++ b/src/stagehand/_files.py @@ -3,8 +3,8 @@ import io import os import pathlib -from typing import overload -from typing_extensions import TypeGuard +from typing import Sequence, cast, overload +from typing_extensions import TypeVar, TypeGuard import anyio @@ -17,7 +17,9 @@ HttpxFileContent, HttpxRequestFiles, ) -from ._utils import is_tuple_t, is_mapping_t, is_sequence_t +from ._utils import is_list, is_mapping, is_tuple_t, is_mapping_t, is_sequence_t + +_T = TypeVar("_T") def is_base64_file_input(obj: object) -> TypeGuard[Base64FileInput]: @@ -121,3 +123,51 @@ async def async_read_file_content(file: FileContent) -> HttpxFileContent: return await anyio.Path(file).read_bytes() return file + + +def deepcopy_with_paths(item: _T, paths: Sequence[Sequence[str]]) -> _T: + """Copy only the containers along the given paths. + + Used to guard against mutation by extract_files without copying the entire structure. + Only dicts and lists that lie on a path are copied; everything else + is returned by reference. + + For example, given paths=[["foo", "files", "file"]] and the structure: + { + "foo": { + "bar": {"baz": {}}, + "files": {"file": } + } + } + The root dict, "foo", and "files" are copied (they lie on the path). + "bar" and "baz" are returned by reference (off the path). + """ + return _deepcopy_with_paths(item, paths, 0) + + +def _deepcopy_with_paths(item: _T, paths: Sequence[Sequence[str]], index: int) -> _T: + if not paths: + return item + if is_mapping(item): + key_to_paths: dict[str, list[Sequence[str]]] = {} + for path in paths: + if index < len(path): + key_to_paths.setdefault(path[index], []).append(path) + + # if no path continues through this mapping, it won't be mutated and copying it is redundant + if not key_to_paths: + return item + + result = dict(item) + for key, subpaths in key_to_paths.items(): + if key in result: + result[key] = _deepcopy_with_paths(result[key], subpaths, index + 1) + return cast(_T, result) + if is_list(item): + array_paths = [path for path in paths if index < len(path) and path[index] == ""] + + # if no path expects a list here, nothing will be mutated inside it - return by reference + if not array_paths: + return cast(_T, item) + return cast(_T, [_deepcopy_with_paths(entry, array_paths, index + 1) for entry in item]) + return item diff --git a/src/stagehand/_qs.py b/src/stagehand/_qs.py index de8c99bc..4127c19c 100644 --- a/src/stagehand/_qs.py +++ b/src/stagehand/_qs.py @@ -2,17 +2,13 @@ from typing import Any, List, Tuple, Union, Mapping, TypeVar from urllib.parse import parse_qs, urlencode -from typing_extensions import Literal, get_args +from typing_extensions import get_args -from ._types import NotGiven, not_given +from ._types import NotGiven, ArrayFormat, NestedFormat, not_given from ._utils import flatten _T = TypeVar("_T") - -ArrayFormat = Literal["comma", "repeat", "indices", "brackets"] -NestedFormat = Literal["dots", "brackets"] - PrimitiveData = Union[str, int, float, bool, None] # this should be Data = Union[PrimitiveData, "List[Data]", "Tuple[Data]", "Mapping[str, Data]"] # https://github.com/microsoft/pyright/issues/3555 diff --git a/src/stagehand/_types.py b/src/stagehand/_types.py index d25d0560..6751e5d0 100644 --- a/src/stagehand/_types.py +++ b/src/stagehand/_types.py @@ -47,6 +47,9 @@ ModelT = TypeVar("ModelT", bound=pydantic.BaseModel) _T = TypeVar("_T") +ArrayFormat = Literal["comma", "repeat", "indices", "brackets"] +NestedFormat = Literal["dots", "brackets"] + # Approximates httpx internal ProxiesTypes and RequestFiles types # while adding support for `PathLike` instances diff --git a/src/stagehand/_utils/__init__.py b/src/stagehand/_utils/__init__.py index 10cb66d2..1c090e51 100644 --- a/src/stagehand/_utils/__init__.py +++ b/src/stagehand/_utils/__init__.py @@ -24,7 +24,6 @@ coerce_integer as coerce_integer, file_from_path as file_from_path, strip_not_given as strip_not_given, - deepcopy_minimal as deepcopy_minimal, get_async_library as get_async_library, maybe_coerce_float as maybe_coerce_float, get_required_header as get_required_header, diff --git a/src/stagehand/_utils/_utils.py b/src/stagehand/_utils/_utils.py index 1c50ff6a..6e1fbd53 100644 --- a/src/stagehand/_utils/_utils.py +++ b/src/stagehand/_utils/_utils.py @@ -17,11 +17,11 @@ ) from pathlib import Path from datetime import date, datetime -from typing_extensions import TypeGuard +from typing_extensions import TypeGuard, get_args import sniffio -from .._types import Omit, NotGiven, FileTypes, HeadersLike +from .._types import Omit, NotGiven, FileTypes, ArrayFormat, HeadersLike _T = TypeVar("_T") _TupleT = TypeVar("_TupleT", bound=Tuple[object, ...]) @@ -40,25 +40,45 @@ def extract_files( query: Mapping[str, object], *, paths: Sequence[Sequence[str]], + array_format: ArrayFormat = "brackets", ) -> list[tuple[str, FileTypes]]: """Recursively extract files from the given dictionary based on specified paths. A path may look like this ['foo', 'files', '', 'data']. + ``array_format`` controls how ```` segments contribute to the emitted + field name. Supported values: ``"brackets"`` (``foo[]``), ``"repeat"`` and + ``"comma"`` (``foo``), ``"indices"`` (``foo[0]``, ``foo[1]``). + Note: this mutates the given dictionary. """ files: list[tuple[str, FileTypes]] = [] for path in paths: - files.extend(_extract_items(query, path, index=0, flattened_key=None)) + files.extend(_extract_items(query, path, index=0, flattened_key=None, array_format=array_format)) return files +def _array_suffix(array_format: ArrayFormat, array_index: int) -> str: + if array_format == "brackets": + return "[]" + if array_format == "indices": + return f"[{array_index}]" + if array_format == "repeat" or array_format == "comma": + # Both repeat the bare field name for each file part; there is no + # meaningful way to comma-join binary parts. + return "" + raise NotImplementedError( + f"Unknown array_format value: {array_format}, choose from {', '.join(get_args(ArrayFormat))}" + ) + + def _extract_items( obj: object, path: Sequence[str], *, index: int, flattened_key: str | None, + array_format: ArrayFormat, ) -> list[tuple[str, FileTypes]]: try: key = path[index] @@ -75,9 +95,11 @@ def _extract_items( if is_list(obj): files: list[tuple[str, FileTypes]] = [] - for entry in obj: - assert_is_file_content(entry, key=flattened_key + "[]" if flattened_key else "") - files.append((flattened_key + "[]", cast(FileTypes, entry))) + for array_index, entry in enumerate(obj): + suffix = _array_suffix(array_format, array_index) + emitted_key = (flattened_key + suffix) if flattened_key else suffix + assert_is_file_content(entry, key=emitted_key) + files.append((emitted_key, cast(FileTypes, entry))) return files assert_is_file_content(obj, key=flattened_key) @@ -86,8 +108,9 @@ def _extract_items( index += 1 if is_dict(obj): try: - # We are at the last entry in the path so we must remove the field - if (len(path)) == index: + # Remove the field if there are no more dict keys in the path, + # only "" traversal markers or end. + if all(p == "" for p in path[index:]): item = obj.pop(key) else: item = obj[key] @@ -105,6 +128,7 @@ def _extract_items( path, index=index, flattened_key=flattened_key, + array_format=array_format, ) elif is_list(obj): if key != "": @@ -116,9 +140,12 @@ def _extract_items( item, path, index=index, - flattened_key=flattened_key + "[]" if flattened_key is not None else "[]", + flattened_key=( + (flattened_key if flattened_key is not None else "") + _array_suffix(array_format, array_index) + ), + array_format=array_format, ) - for item in obj + for array_index, item in enumerate(obj) ] ) @@ -176,21 +203,6 @@ def is_iterable(obj: object) -> TypeGuard[Iterable[object]]: return isinstance(obj, Iterable) -def deepcopy_minimal(item: _T) -> _T: - """Minimal reimplementation of copy.deepcopy() that will only copy certain object types: - - - mappings, e.g. `dict` - - list - - This is done for performance reasons. - """ - if is_mapping(item): - return cast(_T, {k: deepcopy_minimal(v) for k, v in item.items()}) - if is_list(item): - return cast(_T, [deepcopy_minimal(entry) for entry in item]) - return item - - # copied from https://github.com/Rapptz/RoboDanny def human_join(seq: Sequence[str], *, delim: str = ", ", final: str = "or") -> str: size = len(seq) diff --git a/src/stagehand/_version.py b/src/stagehand/_version.py index 15e655a3..b5e0b8a6 100644 --- a/src/stagehand/_version.py +++ b/src/stagehand/_version.py @@ -1,4 +1,4 @@ # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. __title__ = "stagehand" -__version__ = "3.19.5" # x-release-please-version +__version__ = "3.20.0" # x-release-please-version diff --git a/src/stagehand/types/session_execute_params.py b/src/stagehand/types/session_execute_params.py index 944c3856..ab7ea532 100644 --- a/src/stagehand/types/session_execute_params.py +++ b/src/stagehand/types/session_execute_params.py @@ -2,7 +2,7 @@ from __future__ import annotations -from typing import Union, Optional +from typing import Dict, Union, Optional from typing_extensions import Literal, Required, Annotated, TypeAlias, TypedDict from .._utils import PropertyInfo @@ -14,6 +14,8 @@ "AgentConfigExecutionModel", "AgentConfigModel", "ExecuteOptions", + "ExecuteOptionsVariables", + "ExecuteOptionsVariablesUnionMember3", "SessionExecuteParamsNonStreaming", "SessionExecuteParamsStreaming", ] @@ -66,6 +68,15 @@ class AgentConfig(TypedDict, total=False): """Custom system prompt for the agent""" +class ExecuteOptionsVariablesUnionMember3(TypedDict, total=False): + value: Required[Union[str, float, bool]] + + description: str + + +ExecuteOptionsVariables: TypeAlias = Union[str, float, bool, ExecuteOptionsVariablesUnionMember3] + + class ExecuteOptions(TypedDict, total=False): instruction: Required[str] """Natural language instruction for the agent""" @@ -82,6 +93,9 @@ class ExecuteOptions(TypedDict, total=False): use_search: Annotated[bool, PropertyInfo(alias="useSearch")] """Whether to enable the web search tool powered by Browserbase Search API""" + variables: Dict[str, ExecuteOptionsVariables] + """Variables available to the agent via %variableName% syntax in supported tools""" + class SessionExecuteParamsNonStreaming(SessionExecuteParamsBase, total=False): stream_response: Annotated[Literal[False], PropertyInfo(alias="streamResponse")] diff --git a/src/stagehand/types/session_extract_params.py b/src/stagehand/types/session_extract_params.py index 82a8e1b3..fd03aa67 100644 --- a/src/stagehand/types/session_extract_params.py +++ b/src/stagehand/types/session_extract_params.py @@ -5,6 +5,7 @@ from typing import Dict, Union, Optional from typing_extensions import Literal, Required, Annotated, TypeAlias, TypedDict +from .._types import SequenceNotStr from .._utils import PropertyInfo from .model_config_param import ModelConfigParam @@ -37,6 +38,9 @@ class SessionExtractParamsBase(TypedDict, total=False): class Options(TypedDict, total=False): + ignore_selectors: Annotated[SequenceNotStr[str], PropertyInfo(alias="ignoreSelectors")] + """Selectors for elements and subtrees that should be excluded from extraction""" + model: OptionsModel """Model configuration object or model name string (e.g., 'openai/gpt-5-nano')""" diff --git a/src/stagehand/types/session_start_params.py b/src/stagehand/types/session_start_params.py index 17f55997..c13fd036 100644 --- a/src/stagehand/types/session_start_params.py +++ b/src/stagehand/types/session_start_params.py @@ -175,6 +175,10 @@ class BrowserbaseSessionCreateParamsBrowserSettings(TypedDict, total=False): block_ads: Annotated[bool, PropertyInfo(alias="blockAds")] + captcha_image_selector: Annotated[str, PropertyInfo(alias="captchaImageSelector")] + + captcha_input_selector: Annotated[str, PropertyInfo(alias="captchaInputSelector")] + context: BrowserbaseSessionCreateParamsBrowserSettingsContext extension_id: Annotated[str, PropertyInfo(alias="extensionId")] @@ -183,10 +187,14 @@ class BrowserbaseSessionCreateParamsBrowserSettings(TypedDict, total=False): log_session: Annotated[bool, PropertyInfo(alias="logSession")] + os: Literal["windows", "mac", "linux", "mobile", "tablet"] + record_session: Annotated[bool, PropertyInfo(alias="recordSession")] solve_captchas: Annotated[bool, PropertyInfo(alias="solveCaptchas")] + verified: bool + viewport: BrowserbaseSessionCreateParamsBrowserSettingsViewport @@ -232,6 +240,11 @@ class BrowserbaseSessionCreateParams(TypedDict, total=False): keep_alive: Annotated[bool, PropertyInfo(alias="keepAlive")] project_id: Annotated[str, PropertyInfo(alias="projectId")] + """Deprecated. + + Browserbase API keys are now project-scoped, so this field is no longer + required. + """ proxies: Union[bool, Iterable[BrowserbaseSessionCreateParamsProxiesProxyConfigList]] diff --git a/tests/api_resources/test_sessions.py b/tests/api_resources/test_sessions.py index 8dfddbe2..1929c8f5 100644 --- a/tests/api_resources/test_sessions.py +++ b/tests/api_resources/test_sessions.py @@ -273,6 +273,7 @@ def test_method_execute_with_all_params_overload_1(self, client: Stagehand) -> N "max_steps": 20, "tool_timeout": 30000, "use_search": True, + "variables": {"foo": "string"}, }, frame_id="frameId", should_cache=True, @@ -371,6 +372,7 @@ def test_method_execute_with_all_params_overload_2(self, client: Stagehand) -> N "max_steps": 20, "tool_timeout": 30000, "use_search": True, + "variables": {"foo": "string"}, }, stream_response=True, frame_id="frameId", @@ -443,6 +445,7 @@ def test_method_extract_with_all_params_overload_1(self, client: Stagehand) -> N frame_id="frameId", instruction="Extract all product names and prices from the page", options={ + "ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"], "model": { "model_name": "openai/gpt-5.4-mini", "api_key": "sk-some-openai-api-key", @@ -511,6 +514,7 @@ def test_method_extract_with_all_params_overload_2(self, client: Stagehand) -> N frame_id="frameId", instruction="Extract all product names and prices from the page", options={ + "ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"], "model": { "model_name": "openai/gpt-5.4-mini", "api_key": "sk-some-openai-api-key", @@ -875,6 +879,8 @@ def test_method_start_with_all_params(self, client: Stagehand) -> None: "browser_settings": { "advanced_stealth": True, "block_ads": True, + "captcha_image_selector": "captchaImageSelector", + "captcha_input_selector": "captchaInputSelector", "context": { "id": "id", "persist": True, @@ -894,8 +900,10 @@ def test_method_start_with_all_params(self, client: Stagehand) -> None: }, }, "log_session": True, + "os": "windows", "record_session": True, "solve_captchas": True, + "verified": True, "viewport": { "height": 0, "width": 0, @@ -1199,6 +1207,7 @@ async def test_method_execute_with_all_params_overload_1(self, async_client: Asy "max_steps": 20, "tool_timeout": 30000, "use_search": True, + "variables": {"foo": "string"}, }, frame_id="frameId", should_cache=True, @@ -1297,6 +1306,7 @@ async def test_method_execute_with_all_params_overload_2(self, async_client: Asy "max_steps": 20, "tool_timeout": 30000, "use_search": True, + "variables": {"foo": "string"}, }, stream_response=True, frame_id="frameId", @@ -1369,6 +1379,7 @@ async def test_method_extract_with_all_params_overload_1(self, async_client: Asy frame_id="frameId", instruction="Extract all product names and prices from the page", options={ + "ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"], "model": { "model_name": "openai/gpt-5.4-mini", "api_key": "sk-some-openai-api-key", @@ -1437,6 +1448,7 @@ async def test_method_extract_with_all_params_overload_2(self, async_client: Asy frame_id="frameId", instruction="Extract all product names and prices from the page", options={ + "ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"], "model": { "model_name": "openai/gpt-5.4-mini", "api_key": "sk-some-openai-api-key", @@ -1801,6 +1813,8 @@ async def test_method_start_with_all_params(self, async_client: AsyncStagehand) "browser_settings": { "advanced_stealth": True, "block_ads": True, + "captcha_image_selector": "captchaImageSelector", + "captcha_input_selector": "captchaInputSelector", "context": { "id": "id", "persist": True, @@ -1820,8 +1834,10 @@ async def test_method_start_with_all_params(self, async_client: AsyncStagehand) }, }, "log_session": True, + "os": "windows", "record_session": True, "solve_captchas": True, + "verified": True, "viewport": { "height": 0, "width": 0, diff --git a/tests/test_client.py b/tests/test_client.py index e962cdba..fd0e7810 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -460,13 +460,12 @@ def test_validate_headers(self) -> None: request = client._build_request(FinalRequestOptions(method="get", url="/foo")) assert request.headers.get("x-bb-api-key") == browserbase_api_key request = client._build_request(FinalRequestOptions(method="get", url="/foo")) - assert request.headers.get("x-bb-project-id") == browserbase_project_id + assert request.headers.get("x-bb-project-id") is None request = client._build_request(FinalRequestOptions(method="get", url="/foo")) assert request.headers.get("x-model-api-key") == model_api_key with update_env( BROWSERBASE_API_KEY=Omit(), - BROWSERBASE_PROJECT_ID=Omit(), ): client2 = Stagehand( base_url=base_url, @@ -505,6 +504,30 @@ def test_default_query_option(self) -> None: client.close() + def test_hardcoded_query_params_in_url(self, client: Stagehand) -> None: + request = client._build_request(FinalRequestOptions(method="get", url="/foo?beta=true")) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true"} + + request = client._build_request( + FinalRequestOptions( + method="get", + url="/foo?beta=true", + params={"limit": "10", "page": "abc"}, + ) + ) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true", "limit": "10", "page": "abc"} + + request = client._build_request( + FinalRequestOptions( + method="get", + url="/files/a%2Fb?beta=true", + params={"limit": "10"}, + ) + ) + assert request.url.raw_path == b"/files/a%2Fb?beta=true&limit=10" + def test_request_extra_json(self, client: Stagehand) -> None: request = client._build_request( FinalRequestOptions( @@ -771,7 +794,7 @@ def test_base_url_setter(self) -> None: client.close() def test_base_url_env(self) -> None: - with update_env(STAGEHAND_BASE_URL="http://localhost:5000/from/env"): + with update_env(STAGEHAND_API_URL="http://localhost:5000/from/env"): client = Stagehand( browserbase_api_key=browserbase_api_key, browserbase_project_id=browserbase_project_id, @@ -780,6 +803,29 @@ def test_base_url_env(self) -> None: ) assert client.base_url == "http://localhost:5000/from/env/" + def test_base_url_legacy_env(self) -> None: + with update_env(STAGEHAND_BASE_URL="http://localhost:5000/from/legacy/env"): + client = Stagehand( + browserbase_api_key=browserbase_api_key, + browserbase_project_id=browserbase_project_id, + model_api_key=model_api_key, + _strict_response_validation=True, + ) + assert client.base_url == "http://localhost:5000/from/legacy/env/" + + def test_base_url_env_prefers_api_url(self) -> None: + with update_env( + STAGEHAND_API_URL="http://localhost:5000/from/api/env", + STAGEHAND_BASE_URL="http://localhost:5000/from/base/env", + ): + client = Stagehand( + browserbase_api_key=browserbase_api_key, + browserbase_project_id=browserbase_project_id, + model_api_key=model_api_key, + _strict_response_validation=True, + ) + assert client.base_url == "http://localhost:5000/from/api/env/" + @pytest.mark.parametrize( "client", [ @@ -1507,13 +1553,12 @@ def test_validate_headers(self) -> None: request = client._build_request(FinalRequestOptions(method="get", url="/foo")) assert request.headers.get("x-bb-api-key") == browserbase_api_key request = client._build_request(FinalRequestOptions(method="get", url="/foo")) - assert request.headers.get("x-bb-project-id") == browserbase_project_id + assert request.headers.get("x-bb-project-id") is None request = client._build_request(FinalRequestOptions(method="get", url="/foo")) assert request.headers.get("x-model-api-key") == model_api_key with update_env( BROWSERBASE_API_KEY=Omit(), - BROWSERBASE_PROJECT_ID=Omit(), ): client2 = AsyncStagehand( base_url=base_url, @@ -1552,6 +1597,30 @@ async def test_default_query_option(self) -> None: await client.close() + async def test_hardcoded_query_params_in_url(self, async_client: AsyncStagehand) -> None: + request = async_client._build_request(FinalRequestOptions(method="get", url="/foo?beta=true")) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true"} + + request = async_client._build_request( + FinalRequestOptions( + method="get", + url="/foo?beta=true", + params={"limit": "10", "page": "abc"}, + ) + ) + url = httpx.URL(request.url) + assert dict(url.params) == {"beta": "true", "limit": "10", "page": "abc"} + + request = async_client._build_request( + FinalRequestOptions( + method="get", + url="/files/a%2Fb?beta=true", + params={"limit": "10"}, + ) + ) + assert request.url.raw_path == b"/files/a%2Fb?beta=true&limit=10" + def test_request_extra_json(self, client: Stagehand) -> None: request = client._build_request( FinalRequestOptions( @@ -1822,7 +1891,7 @@ async def test_base_url_setter(self) -> None: await client.close() async def test_base_url_env(self) -> None: - with update_env(STAGEHAND_BASE_URL="http://localhost:5000/from/env"): + with update_env(STAGEHAND_API_URL="http://localhost:5000/from/env"): client = AsyncStagehand( browserbase_api_key=browserbase_api_key, browserbase_project_id=browserbase_project_id, @@ -1831,6 +1900,29 @@ async def test_base_url_env(self) -> None: ) assert client.base_url == "http://localhost:5000/from/env/" + async def test_base_url_legacy_env(self) -> None: + with update_env(STAGEHAND_BASE_URL="http://localhost:5000/from/legacy/env"): + client = AsyncStagehand( + browserbase_api_key=browserbase_api_key, + browserbase_project_id=browserbase_project_id, + model_api_key=model_api_key, + _strict_response_validation=True, + ) + assert client.base_url == "http://localhost:5000/from/legacy/env/" + + async def test_base_url_env_prefers_api_url(self) -> None: + with update_env( + STAGEHAND_API_URL="http://localhost:5000/from/api/env", + STAGEHAND_BASE_URL="http://localhost:5000/from/base/env", + ): + client = AsyncStagehand( + browserbase_api_key=browserbase_api_key, + browserbase_project_id=browserbase_project_id, + model_api_key=model_api_key, + _strict_response_validation=True, + ) + assert client.base_url == "http://localhost:5000/from/api/env/" + @pytest.mark.parametrize( "client", [ diff --git a/tests/test_deepcopy.py b/tests/test_deepcopy.py deleted file mode 100644 index 889b2d4e..00000000 --- a/tests/test_deepcopy.py +++ /dev/null @@ -1,58 +0,0 @@ -from stagehand._utils import deepcopy_minimal - - -def assert_different_identities(obj1: object, obj2: object) -> None: - assert obj1 == obj2 - assert id(obj1) != id(obj2) - - -def test_simple_dict() -> None: - obj1 = {"foo": "bar"} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - - -def test_nested_dict() -> None: - obj1 = {"foo": {"bar": True}} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1["foo"], obj2["foo"]) - - -def test_complex_nested_dict() -> None: - obj1 = {"foo": {"bar": [{"hello": "world"}]}} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1["foo"], obj2["foo"]) - assert_different_identities(obj1["foo"]["bar"], obj2["foo"]["bar"]) - assert_different_identities(obj1["foo"]["bar"][0], obj2["foo"]["bar"][0]) - - -def test_simple_list() -> None: - obj1 = ["a", "b", "c"] - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - - -def test_nested_list() -> None: - obj1 = ["a", [1, 2, 3]] - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert_different_identities(obj1[1], obj2[1]) - - -class MyObject: ... - - -def test_ignores_other_types() -> None: - # custom classes - my_obj = MyObject() - obj1 = {"foo": my_obj} - obj2 = deepcopy_minimal(obj1) - assert_different_identities(obj1, obj2) - assert obj1["foo"] is my_obj - - # tuples - obj3 = ("a", "b") - obj4 = deepcopy_minimal(obj3) - assert obj3 is obj4 diff --git a/tests/test_extract_files.py b/tests/test_extract_files.py index 0d751b46..79883d4f 100644 --- a/tests/test_extract_files.py +++ b/tests/test_extract_files.py @@ -4,7 +4,7 @@ import pytest -from stagehand._types import FileTypes +from stagehand._types import FileTypes, ArrayFormat from stagehand._utils import extract_files @@ -35,6 +35,12 @@ def test_multiple_files() -> None: assert query == {"documents": [{}, {}]} +def test_top_level_file_array() -> None: + query = {"files": [b"file one", b"file two"], "title": "hello"} + assert extract_files(query, paths=[["files", ""]]) == [("files[]", b"file one"), ("files[]", b"file two")] + assert query == {"title": "hello"} + + @pytest.mark.parametrize( "query,paths,expected", [ @@ -62,3 +68,24 @@ def test_ignores_incorrect_paths( expected: list[tuple[str, FileTypes]], ) -> None: assert extract_files(query, paths=paths) == expected + + +@pytest.mark.parametrize( + "array_format,expected_top_level,expected_nested", + [ + ("brackets", [("files[]", b"a"), ("files[]", b"b")], [("items[][file]", b"a"), ("items[][file]", b"b")]), + ("repeat", [("files", b"a"), ("files", b"b")], [("items[file]", b"a"), ("items[file]", b"b")]), + ("comma", [("files", b"a"), ("files", b"b")], [("items[file]", b"a"), ("items[file]", b"b")]), + ("indices", [("files[0]", b"a"), ("files[1]", b"b")], [("items[0][file]", b"a"), ("items[1][file]", b"b")]), + ], +) +def test_array_format_controls_file_field_names( + array_format: ArrayFormat, + expected_top_level: list[tuple[str, FileTypes]], + expected_nested: list[tuple[str, FileTypes]], +) -> None: + top_level = {"files": [b"a", b"b"]} + assert extract_files(top_level, paths=[["files", ""]], array_format=array_format) == expected_top_level + + nested = {"items": [{"file": b"a"}, {"file": b"b"}]} + assert extract_files(nested, paths=[["items", "", "file"]], array_format=array_format) == expected_nested diff --git a/tests/test_files.py b/tests/test_files.py index 683b85ee..5e31d7cd 100644 --- a/tests/test_files.py +++ b/tests/test_files.py @@ -4,7 +4,8 @@ import pytest from dirty_equals import IsDict, IsList, IsBytes, IsTuple -from stagehand._files import to_httpx_files, async_to_httpx_files +from stagehand._files import to_httpx_files, deepcopy_with_paths, async_to_httpx_files +from stagehand._utils import extract_files readme_path = Path(__file__).parent.parent.joinpath("README.md") @@ -49,3 +50,99 @@ def test_string_not_allowed() -> None: "file": "foo", # type: ignore } ) + + +def assert_different_identities(obj1: object, obj2: object) -> None: + assert obj1 == obj2 + assert obj1 is not obj2 + + +class TestDeepcopyWithPaths: + def test_copies_top_level_dict(self) -> None: + original = {"file": b"data", "other": "value"} + result = deepcopy_with_paths(original, [["file"]]) + assert_different_identities(result, original) + + def test_file_value_is_same_reference(self) -> None: + file_bytes = b"contents" + original = {"file": file_bytes} + result = deepcopy_with_paths(original, [["file"]]) + assert_different_identities(result, original) + assert result["file"] is file_bytes + + def test_list_popped_wholesale(self) -> None: + files = [b"f1", b"f2"] + original = {"files": files, "title": "t"} + result = deepcopy_with_paths(original, [["files", ""]]) + assert_different_identities(result, original) + result_files = result["files"] + assert isinstance(result_files, list) + assert_different_identities(result_files, files) + + def test_nested_array_path_copies_list_and_elements(self) -> None: + elem1 = {"file": b"f1", "extra": 1} + elem2 = {"file": b"f2", "extra": 2} + original = {"items": [elem1, elem2]} + result = deepcopy_with_paths(original, [["items", "", "file"]]) + assert_different_identities(result, original) + result_items = result["items"] + assert isinstance(result_items, list) + assert_different_identities(result_items, original["items"]) + assert_different_identities(result_items[0], elem1) + assert_different_identities(result_items[1], elem2) + + def test_empty_paths_returns_same_object(self) -> None: + original = {"foo": "bar"} + result = deepcopy_with_paths(original, []) + assert result is original + + def test_multiple_paths(self) -> None: + f1 = b"file1" + f2 = b"file2" + original = {"a": f1, "b": f2, "c": "unchanged"} + result = deepcopy_with_paths(original, [["a"], ["b"]]) + assert_different_identities(result, original) + assert result["a"] is f1 + assert result["b"] is f2 + assert result["c"] is original["c"] + + def test_extract_files_does_not_mutate_original_top_level(self) -> None: + file_bytes = b"contents" + original = {"file": file_bytes, "other": "value"} + + copied = deepcopy_with_paths(original, [["file"]]) + extracted = extract_files(copied, paths=[["file"]]) + + assert extracted == [("file", file_bytes)] + assert original == {"file": file_bytes, "other": "value"} + assert copied == {"other": "value"} + + def test_extract_files_does_not_mutate_original_nested_array_path(self) -> None: + file1 = b"f1" + file2 = b"f2" + original = { + "items": [ + {"file": file1, "extra": 1}, + {"file": file2, "extra": 2}, + ], + "title": "example", + } + + copied = deepcopy_with_paths(original, [["items", "", "file"]]) + extracted = extract_files(copied, paths=[["items", "", "file"]]) + + assert [entry for _, entry in extracted] == [file1, file2] + assert original == { + "items": [ + {"file": file1, "extra": 1}, + {"file": file2, "extra": 2}, + ], + "title": "example", + } + assert copied == { + "items": [ + {"extra": 1}, + {"extra": 2}, + ], + "title": "example", + } diff --git a/tests/test_local_server.py b/tests/test_local_server.py index 20f7ab5b..12af5df4 100644 --- a/tests/test_local_server.py +++ b/tests/test_local_server.py @@ -56,12 +56,10 @@ def kill(self) -> None: def _set_required_env(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("BROWSERBASE_API_KEY", "bb_key") - monkeypatch.setenv("BROWSERBASE_PROJECT_ID", "bb_project") def _set_browserbase_env(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("BROWSERBASE_API_KEY", "bb_key") - monkeypatch.setenv("BROWSERBASE_PROJECT_ID", "bb_project") def _install_fake_sea_runtime( @@ -230,7 +228,6 @@ def test_local_server_requires_browserbase_keys_for_browserbase_sessions( ) -> None: _set_required_env(monkeypatch) monkeypatch.delenv("BROWSERBASE_API_KEY", raising=False) - monkeypatch.delenv("BROWSERBASE_PROJECT_ID", raising=False) client = Stagehand( server="local", model_api_key="model_key", @@ -241,12 +238,33 @@ def test_local_server_requires_browserbase_keys_for_browserbase_sessions( client.sessions.start(model_name="openai/gpt-5-nano") +def test_local_server_allows_browserbase_sessions_without_project_id( + monkeypatch: pytest.MonkeyPatch, +) -> None: + _set_required_env(monkeypatch) + monkeypatch.delenv("BROWSERBASE_PROJECT_ID", raising=False) + client = Stagehand( + server="local", + model_api_key="model_key", + _local_stagehand_binary_path="/does/not/matter/in/test", + ) + client._sea_server = _DummySeaServer("http://127.0.0.1:43132") # type: ignore[attr-defined] + + def _post(*_args: object, **_kwargs: object) -> object: + raise RuntimeError("post called") + + client.sessions._post = _post # type: ignore[method-assign] + client.base_url = httpx.URL("http://127.0.0.1:43132") + + with pytest.raises(RuntimeError, match="post called"): + client.sessions.start(model_name="openai/gpt-5-nano") + + def test_local_server_allows_local_browser_without_browserbase_keys( monkeypatch: pytest.MonkeyPatch, ) -> None: _set_required_env(monkeypatch) monkeypatch.delenv("BROWSERBASE_API_KEY", raising=False) - monkeypatch.delenv("BROWSERBASE_PROJECT_ID", raising=False) client = Stagehand( server="local", model_api_key="model_key",