Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ jobs:
runs-on: ${{ github.repository == 'stainless-sdks/stagehand-python' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
if: (github.event_name == 'push' || github.event.pull_request.head.repo.fork) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata')
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Install uv
uses: astral-sh/setup-uv@v5
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2
with:
version: '0.10.2'

Expand All @@ -43,10 +43,10 @@ jobs:
id-token: write
runs-on: ${{ github.repository == 'stainless-sdks/stagehand-python' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Install uv
uses: astral-sh/setup-uv@v5
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2
with:
version: '0.10.2'

Expand All @@ -61,7 +61,7 @@ jobs:
github.repository == 'stainless-sdks/stagehand-python' &&
!startsWith(github.ref, 'refs/heads/stl/')
id: github-oidc
uses: actions/github-script@v8
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
script: core.setOutput('github_token', await core.getIDToken());

Expand All @@ -81,10 +81,10 @@ jobs:
runs-on: ${{ github.repository == 'stainless-sdks/stagehand-python' && 'depot-ubuntu-24.04' || 'ubuntu-latest' }}
if: github.event_name == 'push' || github.event.pull_request.head.repo.fork
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Install uv
uses: astral-sh/setup-uv@v5
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2
with:
version: '0.10.2'

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ jobs:
contents: read

steps:
- uses: actions/checkout@v6
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Install uv
uses: astral-sh/setup-uv@v5
uses: astral-sh/setup-uv@d4b2f3b6ecc6e67c4457f6d3e41ec42d3d0fcb86 # v5.4.2
with:
version: "0.9.13"

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-doctor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
if: github.repository == 'browserbase/stagehand-python' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch' || startsWith(github.head_ref, 'release-please') || github.head_ref == 'next')

steps:
- uses: actions/checkout@v6
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

- name: Check release environment
run: |
Expand Down
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "3.20.0"
".": "3.21.0"
}
4 changes: 2 additions & 2 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
configured_endpoints: 8
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase/stagehand-6f6bfb81d092f30a5e2005328c97d61b9ea36132bb19e9e79e55294b9534ce20.yml
openapi_spec_hash: f3fc1e3688a38dc2c28f7178f7d534e5
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase/stagehand-eae8400fade7b2c8329c4148f56de92e147c34c0feecb420c015aab6544a9acc.yml
openapi_spec_hash: 0a9eff1ac1d464e89cbd9db64709b08a
config_hash: 1fb12ae9b478488bc1e56bfbdc210b01
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## 3.21.0 (2026-05-13)

Full Changelog: [v3.20.0...v3.21.0](https://github.com/browserbase/stagehand-python/compare/v3.20.0...v3.21.0)

### Features

* [feat]: add `ignoreSelectors` to `observe()` ([77be2c2](https://github.com/browserbase/stagehand-python/commit/77be2c2463c7b1d0ce6768eecf130adc3ef91149))
* **internal/types:** support eagerly validating pydantic iterators ([95a4cc8](https://github.com/browserbase/stagehand-python/commit/95a4cc8cd9228e21ea7eefa7138e498bf628e376))


### Bug Fixes

* **client:** add missing f-string prefix in file type error message ([fb40f50](https://github.com/browserbase/stagehand-python/commit/fb40f50c2fb3337be84a3f973b8a27a8f53fbfae))

## 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)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "stagehand"
version = "3.20.0"
version = "3.21.0"
description = "The official Python library for the stagehand API"
dynamic = ["readme"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion src/stagehand/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def async_to_httpx_files(files: RequestFiles | None) -> HttpxRequestFiles
elif is_sequence_t(files):
files = [(key, await _async_transform_file(file)) for key, file in files]
else:
raise TypeError("Unexpected file type input {type(files)}, expected mapping or sequence")
raise TypeError(f"Unexpected file type input {type(files)}, expected mapping or sequence")

return files

Expand Down
80 changes: 80 additions & 0 deletions src/stagehand/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
ClassVar,
Protocol,
Required,
Annotated,
ParamSpec,
TypeAlias,
TypedDict,
TypeGuard,
final,
Expand Down Expand Up @@ -79,7 +81,15 @@
from ._constants import RAW_RESPONSE_HEADER

if TYPE_CHECKING:
from pydantic import GetCoreSchemaHandler, ValidatorFunctionWrapHandler
from pydantic_core import CoreSchema, core_schema
from pydantic_core.core_schema import ModelField, ModelSchema, LiteralSchema, ModelFieldsSchema
else:
try:
from pydantic_core import CoreSchema, core_schema
except ImportError:
CoreSchema = None
core_schema = None

__all__ = ["BaseModel", "GenericModel"]

Expand Down Expand Up @@ -396,6 +406,76 @@ def model_dump_json(
)


class _EagerIterable(list[_T], Generic[_T]):
"""
Accepts any Iterable[T] input (including generators), consumes it
eagerly, and validates all items upfront.

Validation preserves the original container type where possible
(e.g. a set[T] stays a set[T]). Serialization (model_dump / JSON)
always emits a list — round-tripping through model_dump() will not
restore the original container type.
"""

@classmethod
def __get_pydantic_core_schema__(
cls,
source_type: Any,
handler: GetCoreSchemaHandler,
) -> CoreSchema:
(item_type,) = get_args(source_type) or (Any,)
item_schema: CoreSchema = handler.generate_schema(item_type)
list_of_items_schema: CoreSchema = core_schema.list_schema(item_schema)

return core_schema.no_info_wrap_validator_function(
cls._validate,
list_of_items_schema,
serialization=core_schema.plain_serializer_function_ser_schema(
cls._serialize,
info_arg=False,
),
)

@staticmethod
def _validate(v: Iterable[_T], handler: "ValidatorFunctionWrapHandler") -> Any:
original_type: type[Any] = type(v)

# Normalize to list so list_schema can validate each item
if isinstance(v, list):
items: list[_T] = v
else:
try:
items = list(v)
except TypeError as e:
raise TypeError("Value is not iterable") from e

# Validate items against the inner schema
validated: list[_T] = handler(items)

# Reconstruct original container type
if original_type is list:
return validated
# str(list) produces the list's repr, not a string built from items,
# so skip reconstruction for str and its subclasses.
if issubclass(original_type, str):
return validated
try:
return original_type(validated)
except (TypeError, ValueError):
# If the type cannot be reconstructed, just return the validated list
return validated

@staticmethod
def _serialize(v: Iterable[_T]) -> list[_T]:
"""Always serialize as a list so Pydantic's JSON encoder is happy."""
if isinstance(v, list):
return v
return list(v)


EagerIterable: TypeAlias = Annotated[Iterable[_T], _EagerIterable]


def _construct_field(value: object, field: FieldInfo, key: str) -> object:
if value is None:
return field_get_default(field)
Expand Down
2 changes: 1 addition & 1 deletion src/stagehand/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

__title__ = "stagehand"
__version__ = "3.20.0" # x-release-please-version
__version__ = "3.21.0" # x-release-please-version
4 changes: 4 additions & 0 deletions src/stagehand/types/session_observe_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Dict, Union, Optional
from typing_extensions import Literal, Required, Annotated, TypeAlias, TypedDict

from .._types import SequenceNotStr
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot May 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: Custom agent: Prevent direct modifications to stainless codegen'd code outside of src/stagehand/_custom

Directly modified a Stainless-generated SDK file outside src/stagehand/_custom by adding a new public field/import instead of using the allowed custom-code wrapper or hook pattern.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/stagehand/types/session_observe_params.py, line 8:

<comment>Directly modified a Stainless-generated SDK file outside src/stagehand/_custom by adding a new public field/import instead of using the allowed custom-code wrapper or hook pattern.</comment>

<file context>
@@ -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
</file context>
Fix with Cubic

from .._utils import PropertyInfo
from .model_config_param import ModelConfigParam

Expand Down Expand Up @@ -45,6 +46,9 @@ class OptionsVariablesUnionMember3(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 observation"""

model: OptionsModel
"""Model configuration object or model name string (e.g., 'openai/gpt-5-nano')"""

Expand Down
4 changes: 4 additions & 0 deletions tests/api_resources/test_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ def test_method_observe_with_all_params_overload_1(self, client: Stagehand) -> N
frame_id="frameId",
instruction="Find all clickable navigation links",
options={
"ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"],
"model": {
"model_name": "openai/gpt-5.4-mini",
"api_key": "sk-some-openai-api-key",
Expand Down Expand Up @@ -719,6 +720,7 @@ def test_method_observe_with_all_params_overload_2(self, client: Stagehand) -> N
frame_id="frameId",
instruction="Find all clickable navigation links",
options={
"ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"],
"model": {
"model_name": "openai/gpt-5.4-mini",
"api_key": "sk-some-openai-api-key",
Expand Down Expand Up @@ -1579,6 +1581,7 @@ async def test_method_observe_with_all_params_overload_1(self, async_client: Asy
frame_id="frameId",
instruction="Find all clickable navigation links",
options={
"ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"],
"model": {
"model_name": "openai/gpt-5.4-mini",
"api_key": "sk-some-openai-api-key",
Expand Down Expand Up @@ -1653,6 +1656,7 @@ async def test_method_observe_with_all_params_overload_2(self, async_client: Asy
frame_id="frameId",
instruction="Find all clickable navigation links",
options={
"ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"],
"model": {
"model_name": "openai/gpt-5.4-mini",
"api_key": "sk-some-openai-api-key",
Expand Down
60 changes: 57 additions & 3 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import json
from typing import TYPE_CHECKING, Any, Dict, List, Union, Optional, cast
from typing import TYPE_CHECKING, Any, Dict, List, Union, Iterable, Optional, cast
from datetime import datetime, timezone
from typing_extensions import Literal, Annotated, TypeAliasType
from collections import deque
from typing_extensions import Literal, Annotated, TypedDict, TypeAliasType

import pytest
import pydantic
from pydantic import Field

from stagehand._utils import PropertyInfo
from stagehand._compat import PYDANTIC_V1, parse_obj, model_dump, model_json
from stagehand._models import DISCRIMINATOR_CACHE, BaseModel, construct_type
from stagehand._models import DISCRIMINATOR_CACHE, BaseModel, EagerIterable, construct_type


class BasicModel(BaseModel):
Expand Down Expand Up @@ -961,3 +962,56 @@ def __getattr__(self, attr: str) -> Item: ...
assert model.a.prop == 1
assert isinstance(model.a, Item)
assert model.other == "foo"


# NOTE: Workaround for Pydantic Iterable behavior.
# Iterable fields are replaced with a ValidatorIterator and may be consumed
# during serialization, which can cause subsequent dumps to return empty data.
# See: https://github.com/pydantic/pydantic/issues/9541
@pytest.mark.parametrize(
"data, expected_validated",
[
([1, 2, 3], [1, 2, 3]),
((1, 2, 3), (1, 2, 3)),
(set([1, 2, 3]), set([1, 2, 3])),
(iter([1, 2, 3]), [1, 2, 3]),
([], []),
((x for x in [1, 2, 3]), [1, 2, 3]),
(map(lambda x: x, [1, 2, 3]), [1, 2, 3]),
(frozenset([1, 2, 3]), frozenset([1, 2, 3])),
(deque([1, 2, 3]), deque([1, 2, 3])),
],
ids=["list", "tuple", "set", "iterator", "empty", "generator", "map", "frozenset", "deque"],
)
@pytest.mark.skipif(PYDANTIC_V1, reason="this is only supported in pydantic v2")
def test_iterable_construction(data: Iterable[int], expected_validated: Iterable[int]) -> None:
class TypeWithIterable(TypedDict):
items: EagerIterable[int]

class Model(BaseModel):
data: TypeWithIterable

m = Model.model_validate({"data": {"items": data}})
assert m.data["items"] == expected_validated

# Verify repeated dumps don't lose data (the original bug)
assert m.model_dump()["data"]["items"] == list(expected_validated)
assert m.model_dump()["data"]["items"] == list(expected_validated)


@pytest.mark.skipif(PYDANTIC_V1, reason="this is only supported in pydantic v2")
def test_iterable_construction_str_falls_back_to_list() -> None:
# str is iterable (over chars), but str(list_of_chars) produces the list's repr
# rather than reconstructing a string from items. We special-case str to fall
# back to list instead of attempting reconstruction.
class TypeWithIterable(TypedDict):
items: EagerIterable[str]

class Model(BaseModel):
data: TypeWithIterable

m = Model.model_validate({"data": {"items": "hello"}})

# falls back to list of chars rather than calling str(["h", "e", "l", "l", "o"])
assert m.data["items"] == ["h", "e", "l", "l", "o"]
assert m.model_dump()["data"]["items"] == ["h", "e", "l", "l", "o"]
Loading