diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 46c4fb40..f5c86d9c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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' @@ -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' @@ -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()); @@ -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' diff --git a/.github/workflows/publish-pypi.yml b/.github/workflows/publish-pypi.yml index d55348a9..2f2e895b 100644 --- a/.github/workflows/publish-pypi.yml +++ b/.github/workflows/publish-pypi.yml @@ -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" diff --git a/.github/workflows/release-doctor.yml b/.github/workflows/release-doctor.yml index aba0c08f..e889f657 100644 --- a/.github/workflows/release-doctor.yml +++ b/.github/workflows/release-doctor.yml @@ -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: | diff --git a/.release-please-manifest.json b/.release-please-manifest.json index f80372ae..a397c094 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "3.20.0" + ".": "3.21.0" } diff --git a/.stats.yml b/.stats.yml index 391cde37..15099cab 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/stagehand-6f6bfb81d092f30a5e2005328c97d61b9ea36132bb19e9e79e55294b9534ce20.yml -openapi_spec_hash: f3fc1e3688a38dc2c28f7178f7d534e5 +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/browserbase/stagehand-c7910965e66e73ad8b65b6cc391d431094b2a6c6577c3e9d82feaa8138e74cff.yml +openapi_spec_hash: 37748bb69c22a9ce721d9b5a5861f964 config_hash: 1fb12ae9b478488bc1e56bfbdc210b01 diff --git a/CHANGELOG.md b/CHANGELOG.md index c5ab97a1..46ad8b34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,22 @@ # Changelog +## 3.21.0 (2026-05-27) + +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)) +* [STG-1756] forward Vertex model config ([09bdd54](https://github.com/browserbase/stagehand-python/commit/09bdd5484e09c3827bed41e8fca5dbefd6d9ada9)) +* Add `screenshot` option to Extract ([393ebbe](https://github.com/browserbase/stagehand-python/commit/393ebbee487278360fc844e1fb5034651f37cce4)) +* **internal/types:** support eagerly validating pydantic iterators ([95a4cc8](https://github.com/browserbase/stagehand-python/commit/95a4cc8cd9228e21ea7eefa7138e498bf628e376)) +* STG-1756 add Vertex auth params to Stagehand spec ([2bdeae8](https://github.com/browserbase/stagehand-python/commit/2bdeae8adfe292c4bffc8ff93444f5be4c255c2e)) + + +### 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) diff --git a/pyproject.toml b/pyproject.toml index d3411cde..7a595051 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/src/stagehand/_files.py b/src/stagehand/_files.py index 0fdce17b..76da9e08 100644 --- a/src/stagehand/_files.py +++ b/src/stagehand/_files.py @@ -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 diff --git a/src/stagehand/_models.py b/src/stagehand/_models.py index 47024dd0..9e331821 100644 --- a/src/stagehand/_models.py +++ b/src/stagehand/_models.py @@ -25,7 +25,9 @@ ClassVar, Protocol, Required, + Annotated, ParamSpec, + TypeAlias, TypedDict, TypeGuard, final, @@ -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"] @@ -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) diff --git a/src/stagehand/_version.py b/src/stagehand/_version.py index b5e0b8a6..6dcaaca6 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.20.0" # x-release-please-version +__version__ = "3.21.0" # x-release-please-version diff --git a/src/stagehand/types/__init__.py b/src/stagehand/types/__init__.py index f7278aa6..d01a70d0 100644 --- a/src/stagehand/types/__init__.py +++ b/src/stagehand/types/__init__.py @@ -4,7 +4,6 @@ from .action_param import ActionParam as ActionParam from .stream_event import StreamEvent as StreamEvent -from .model_config_param import ModelConfigParam as ModelConfigParam from .session_act_params import SessionActParams as SessionActParams from .session_act_response import SessionActResponse as SessionActResponse from .session_end_response import SessionEndResponse as SessionEndResponse diff --git a/src/stagehand/types/model_config_param.py b/src/stagehand/types/model_config_param.py deleted file mode 100644 index 3ed433f7..00000000 --- a/src/stagehand/types/model_config_param.py +++ /dev/null @@ -1,27 +0,0 @@ -# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. - -from __future__ import annotations - -from typing import Dict -from typing_extensions import Literal, Required, Annotated, TypedDict - -from .._utils import PropertyInfo - -__all__ = ["ModelConfigParam"] - - -class ModelConfigParam(TypedDict, total=False): - model_name: Required[Annotated[str, PropertyInfo(alias="modelName")]] - """Model name string with provider prefix (e.g., 'openai/gpt-5-nano')""" - - api_key: Annotated[str, PropertyInfo(alias="apiKey")] - """API key for the model provider""" - - base_url: Annotated[str, PropertyInfo(alias="baseURL")] - """Base URL for the model provider""" - - headers: Dict[str, str] - """Custom headers sent with every request to the model provider""" - - provider: Literal["openai", "anthropic", "google", "microsoft", "bedrock"] - """AI provider for the model (or provide a baseURL endpoint instead)""" diff --git a/src/stagehand/types/session_act_params.py b/src/stagehand/types/session_act_params.py index 5a206306..71bdfcdb 100644 --- a/src/stagehand/types/session_act_params.py +++ b/src/stagehand/types/session_act_params.py @@ -5,15 +5,21 @@ from typing import Dict, Union, Optional from typing_extensions import Literal, Required, Annotated, TypeAlias, TypedDict +from .._types import SequenceNotStr from .._utils import PropertyInfo from .action_param import ActionParam -from .model_config_param import ModelConfigParam __all__ = [ "SessionActParamsBase", "Input", "Options", "OptionsModel", + "OptionsModelVertexModelConfigObject", + "OptionsModelVertexModelConfigObjectAuth", + "OptionsModelVertexModelConfigObjectAuthCredentials", + "OptionsModelVertexModelConfigObjectProviderOptions", + "OptionsModelVertexModelConfigObjectProviderOptionsVertex", + "OptionsModelGenericModelConfigObject", "OptionsVariables", "OptionsVariablesUnionMember3", "SessionActParamsNonStreaming", @@ -36,7 +42,118 @@ class SessionActParamsBase(TypedDict, total=False): Input: TypeAlias = Union[str, ActionParam] -OptionsModel: TypeAlias = Union[ModelConfigParam, str] + +class OptionsModelVertexModelConfigObjectAuthCredentials(TypedDict, total=False): + """Google Cloud service account credentials""" + + client_email: Required[str] + + private_key: Required[str] + + auth_provider_x509_cert_url: str + + auth_uri: str + + client_id: str + + client_x509_cert_url: str + + private_key_id: str + + project_id: str + + token_uri: str + + type: Literal["service_account"] + + universe_domain: str + + +class OptionsModelVertexModelConfigObjectAuth(TypedDict, total=False): + """Vertex provider authentication configuration""" + + credentials: Required[OptionsModelVertexModelConfigObjectAuthCredentials] + """Google Cloud service account credentials""" + + type: Required[Literal["googleServiceAccount"]] + """Use inline Google Cloud service account credentials for provider authentication""" + + project_id: Annotated[str, PropertyInfo(alias="projectId")] + """Google Cloud project ID used by google-auth-library""" + + scopes: Union[str, SequenceNotStr[str]] + """Google auth scopes for the desired API request""" + + universe_domain: Annotated[str, PropertyInfo(alias="universeDomain")] + """Google Cloud universe domain""" + + +class OptionsModelVertexModelConfigObjectProviderOptionsVertex(TypedDict, total=False): + """Vertex AI provider-specific settings""" + + location: Required[str] + """Google Cloud location for Vertex AI models""" + + project: Required[str] + """Google Cloud project ID for Vertex AI models""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the Vertex AI provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the Vertex AI provider""" + + +class OptionsModelVertexModelConfigObjectProviderOptions(TypedDict, total=False): + """Vertex provider-specific model configuration""" + + vertex: Required[OptionsModelVertexModelConfigObjectProviderOptionsVertex] + """Vertex AI provider-specific settings""" + + +class OptionsModelVertexModelConfigObject(TypedDict, total=False): + auth: Required[OptionsModelVertexModelConfigObjectAuth] + """Vertex provider authentication configuration""" + + model_name: Required[Annotated[str, PropertyInfo(alias="modelName")]] + """Model name string with provider prefix (e.g., 'openai/gpt-5-nano')""" + + provider: Required[Literal["vertex"]] + """Vertex AI model provider""" + + provider_options: Required[ + Annotated[OptionsModelVertexModelConfigObjectProviderOptions, PropertyInfo(alias="providerOptions")] + ] + """Vertex provider-specific model configuration""" + + api_key: Annotated[str, PropertyInfo(alias="apiKey")] + """API key for the model provider""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the model provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the model provider""" + + +class OptionsModelGenericModelConfigObject(TypedDict, total=False): + model_name: Required[Annotated[str, PropertyInfo(alias="modelName")]] + """Model name string with provider prefix (e.g., 'openai/gpt-5-nano')""" + + api_key: Annotated[str, PropertyInfo(alias="apiKey")] + """API key for the model provider""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the model provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the model provider""" + + provider: Literal["openai", "anthropic", "google", "microsoft", "bedrock"] + """AI provider for the model (or provide a baseURL endpoint instead)""" + + +OptionsModel: TypeAlias = Union[OptionsModelVertexModelConfigObject, OptionsModelGenericModelConfigObject, str] class OptionsVariablesUnionMember3(TypedDict, total=False): diff --git a/src/stagehand/types/session_execute_params.py b/src/stagehand/types/session_execute_params.py index ab7ea532..5c045977 100644 --- a/src/stagehand/types/session_execute_params.py +++ b/src/stagehand/types/session_execute_params.py @@ -5,14 +5,26 @@ 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 __all__ = [ "SessionExecuteParamsBase", "AgentConfig", "AgentConfigExecutionModel", + "AgentConfigExecutionModelVertexModelConfigObject", + "AgentConfigExecutionModelVertexModelConfigObjectAuth", + "AgentConfigExecutionModelVertexModelConfigObjectAuthCredentials", + "AgentConfigExecutionModelVertexModelConfigObjectProviderOptions", + "AgentConfigExecutionModelVertexModelConfigObjectProviderOptionsVertex", + "AgentConfigExecutionModelGenericModelConfigObject", "AgentConfigModel", + "AgentConfigModelVertexModelConfigObject", + "AgentConfigModelVertexModelConfigObjectAuth", + "AgentConfigModelVertexModelConfigObjectAuthCredentials", + "AgentConfigModelVertexModelConfigObjectProviderOptions", + "AgentConfigModelVertexModelConfigObjectProviderOptionsVertex", + "AgentConfigModelGenericModelConfigObject", "ExecuteOptions", "ExecuteOptionsVariables", "ExecuteOptionsVariablesUnionMember3", @@ -36,9 +48,236 @@ class SessionExecuteParamsBase(TypedDict, total=False): """Whether to stream the response via SSE""" -AgentConfigExecutionModel: TypeAlias = Union[ModelConfigParam, str] +class AgentConfigExecutionModelVertexModelConfigObjectAuthCredentials(TypedDict, total=False): + """Google Cloud service account credentials""" -AgentConfigModel: TypeAlias = Union[ModelConfigParam, str] + client_email: Required[str] + + private_key: Required[str] + + auth_provider_x509_cert_url: str + + auth_uri: str + + client_id: str + + client_x509_cert_url: str + + private_key_id: str + + project_id: str + + token_uri: str + + type: Literal["service_account"] + + universe_domain: str + + +class AgentConfigExecutionModelVertexModelConfigObjectAuth(TypedDict, total=False): + """Vertex provider authentication configuration""" + + credentials: Required[AgentConfigExecutionModelVertexModelConfigObjectAuthCredentials] + """Google Cloud service account credentials""" + + type: Required[Literal["googleServiceAccount"]] + """Use inline Google Cloud service account credentials for provider authentication""" + + project_id: Annotated[str, PropertyInfo(alias="projectId")] + """Google Cloud project ID used by google-auth-library""" + + scopes: Union[str, SequenceNotStr[str]] + """Google auth scopes for the desired API request""" + + universe_domain: Annotated[str, PropertyInfo(alias="universeDomain")] + """Google Cloud universe domain""" + + +class AgentConfigExecutionModelVertexModelConfigObjectProviderOptionsVertex(TypedDict, total=False): + """Vertex AI provider-specific settings""" + + location: Required[str] + """Google Cloud location for Vertex AI models""" + + project: Required[str] + """Google Cloud project ID for Vertex AI models""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the Vertex AI provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the Vertex AI provider""" + + +class AgentConfigExecutionModelVertexModelConfigObjectProviderOptions(TypedDict, total=False): + """Vertex provider-specific model configuration""" + + vertex: Required[AgentConfigExecutionModelVertexModelConfigObjectProviderOptionsVertex] + """Vertex AI provider-specific settings""" + + +class AgentConfigExecutionModelVertexModelConfigObject(TypedDict, total=False): + auth: Required[AgentConfigExecutionModelVertexModelConfigObjectAuth] + """Vertex provider authentication configuration""" + + model_name: Required[Annotated[str, PropertyInfo(alias="modelName")]] + """Model name string with provider prefix (e.g., 'openai/gpt-5-nano')""" + + provider: Required[Literal["vertex"]] + """Vertex AI model provider""" + + provider_options: Required[ + Annotated[ + AgentConfigExecutionModelVertexModelConfigObjectProviderOptions, PropertyInfo(alias="providerOptions") + ] + ] + """Vertex provider-specific model configuration""" + + api_key: Annotated[str, PropertyInfo(alias="apiKey")] + """API key for the model provider""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the model provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the model provider""" + + +class AgentConfigExecutionModelGenericModelConfigObject(TypedDict, total=False): + model_name: Required[Annotated[str, PropertyInfo(alias="modelName")]] + """Model name string with provider prefix (e.g., 'openai/gpt-5-nano')""" + + api_key: Annotated[str, PropertyInfo(alias="apiKey")] + """API key for the model provider""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the model provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the model provider""" + + provider: Literal["openai", "anthropic", "google", "microsoft", "bedrock"] + """AI provider for the model (or provide a baseURL endpoint instead)""" + + +AgentConfigExecutionModel: TypeAlias = Union[ + AgentConfigExecutionModelVertexModelConfigObject, AgentConfigExecutionModelGenericModelConfigObject, str +] + + +class AgentConfigModelVertexModelConfigObjectAuthCredentials(TypedDict, total=False): + """Google Cloud service account credentials""" + + client_email: Required[str] + + private_key: Required[str] + + auth_provider_x509_cert_url: str + + auth_uri: str + + client_id: str + + client_x509_cert_url: str + + private_key_id: str + + project_id: str + + token_uri: str + + type: Literal["service_account"] + + universe_domain: str + + +class AgentConfigModelVertexModelConfigObjectAuth(TypedDict, total=False): + """Vertex provider authentication configuration""" + + credentials: Required[AgentConfigModelVertexModelConfigObjectAuthCredentials] + """Google Cloud service account credentials""" + + type: Required[Literal["googleServiceAccount"]] + """Use inline Google Cloud service account credentials for provider authentication""" + + project_id: Annotated[str, PropertyInfo(alias="projectId")] + """Google Cloud project ID used by google-auth-library""" + + scopes: Union[str, SequenceNotStr[str]] + """Google auth scopes for the desired API request""" + + universe_domain: Annotated[str, PropertyInfo(alias="universeDomain")] + """Google Cloud universe domain""" + + +class AgentConfigModelVertexModelConfigObjectProviderOptionsVertex(TypedDict, total=False): + """Vertex AI provider-specific settings""" + + location: Required[str] + """Google Cloud location for Vertex AI models""" + + project: Required[str] + """Google Cloud project ID for Vertex AI models""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the Vertex AI provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the Vertex AI provider""" + + +class AgentConfigModelVertexModelConfigObjectProviderOptions(TypedDict, total=False): + """Vertex provider-specific model configuration""" + + vertex: Required[AgentConfigModelVertexModelConfigObjectProviderOptionsVertex] + """Vertex AI provider-specific settings""" + + +class AgentConfigModelVertexModelConfigObject(TypedDict, total=False): + auth: Required[AgentConfigModelVertexModelConfigObjectAuth] + """Vertex provider authentication configuration""" + + model_name: Required[Annotated[str, PropertyInfo(alias="modelName")]] + """Model name string with provider prefix (e.g., 'openai/gpt-5-nano')""" + + provider: Required[Literal["vertex"]] + """Vertex AI model provider""" + + provider_options: Required[ + Annotated[AgentConfigModelVertexModelConfigObjectProviderOptions, PropertyInfo(alias="providerOptions")] + ] + """Vertex provider-specific model configuration""" + + api_key: Annotated[str, PropertyInfo(alias="apiKey")] + """API key for the model provider""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the model provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the model provider""" + + +class AgentConfigModelGenericModelConfigObject(TypedDict, total=False): + model_name: Required[Annotated[str, PropertyInfo(alias="modelName")]] + """Model name string with provider prefix (e.g., 'openai/gpt-5-nano')""" + + api_key: Annotated[str, PropertyInfo(alias="apiKey")] + """API key for the model provider""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the model provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the model provider""" + + provider: Literal["openai", "anthropic", "google", "microsoft", "bedrock"] + """AI provider for the model (or provide a baseURL endpoint instead)""" + + +AgentConfigModel: TypeAlias = Union[ + AgentConfigModelVertexModelConfigObject, AgentConfigModelGenericModelConfigObject, str +] class AgentConfig(TypedDict, total=False): diff --git a/src/stagehand/types/session_extract_params.py b/src/stagehand/types/session_extract_params.py index fd03aa67..4a0b2d9f 100644 --- a/src/stagehand/types/session_extract_params.py +++ b/src/stagehand/types/session_extract_params.py @@ -7,12 +7,17 @@ from .._types import SequenceNotStr from .._utils import PropertyInfo -from .model_config_param import ModelConfigParam __all__ = [ "SessionExtractParamsBase", "Options", "OptionsModel", + "OptionsModelVertexModelConfigObject", + "OptionsModelVertexModelConfigObjectAuth", + "OptionsModelVertexModelConfigObjectAuthCredentials", + "OptionsModelVertexModelConfigObjectProviderOptions", + "OptionsModelVertexModelConfigObjectProviderOptionsVertex", + "OptionsModelGenericModelConfigObject", "SessionExtractParamsNonStreaming", "SessionExtractParamsStreaming", ] @@ -34,7 +39,117 @@ class SessionExtractParamsBase(TypedDict, total=False): """Whether to stream the response via SSE""" -OptionsModel: TypeAlias = Union[ModelConfigParam, str] +class OptionsModelVertexModelConfigObjectAuthCredentials(TypedDict, total=False): + """Google Cloud service account credentials""" + + client_email: Required[str] + + private_key: Required[str] + + auth_provider_x509_cert_url: str + + auth_uri: str + + client_id: str + + client_x509_cert_url: str + + private_key_id: str + + project_id: str + + token_uri: str + + type: Literal["service_account"] + + universe_domain: str + + +class OptionsModelVertexModelConfigObjectAuth(TypedDict, total=False): + """Vertex provider authentication configuration""" + + credentials: Required[OptionsModelVertexModelConfigObjectAuthCredentials] + """Google Cloud service account credentials""" + + type: Required[Literal["googleServiceAccount"]] + """Use inline Google Cloud service account credentials for provider authentication""" + + project_id: Annotated[str, PropertyInfo(alias="projectId")] + """Google Cloud project ID used by google-auth-library""" + + scopes: Union[str, SequenceNotStr[str]] + """Google auth scopes for the desired API request""" + + universe_domain: Annotated[str, PropertyInfo(alias="universeDomain")] + """Google Cloud universe domain""" + + +class OptionsModelVertexModelConfigObjectProviderOptionsVertex(TypedDict, total=False): + """Vertex AI provider-specific settings""" + + location: Required[str] + """Google Cloud location for Vertex AI models""" + + project: Required[str] + """Google Cloud project ID for Vertex AI models""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the Vertex AI provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the Vertex AI provider""" + + +class OptionsModelVertexModelConfigObjectProviderOptions(TypedDict, total=False): + """Vertex provider-specific model configuration""" + + vertex: Required[OptionsModelVertexModelConfigObjectProviderOptionsVertex] + """Vertex AI provider-specific settings""" + + +class OptionsModelVertexModelConfigObject(TypedDict, total=False): + auth: Required[OptionsModelVertexModelConfigObjectAuth] + """Vertex provider authentication configuration""" + + model_name: Required[Annotated[str, PropertyInfo(alias="modelName")]] + """Model name string with provider prefix (e.g., 'openai/gpt-5-nano')""" + + provider: Required[Literal["vertex"]] + """Vertex AI model provider""" + + provider_options: Required[ + Annotated[OptionsModelVertexModelConfigObjectProviderOptions, PropertyInfo(alias="providerOptions")] + ] + """Vertex provider-specific model configuration""" + + api_key: Annotated[str, PropertyInfo(alias="apiKey")] + """API key for the model provider""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the model provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the model provider""" + + +class OptionsModelGenericModelConfigObject(TypedDict, total=False): + model_name: Required[Annotated[str, PropertyInfo(alias="modelName")]] + """Model name string with provider prefix (e.g., 'openai/gpt-5-nano')""" + + api_key: Annotated[str, PropertyInfo(alias="apiKey")] + """API key for the model provider""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the model provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the model provider""" + + provider: Literal["openai", "anthropic", "google", "microsoft", "bedrock"] + """AI provider for the model (or provide a baseURL endpoint instead)""" + + +OptionsModel: TypeAlias = Union[OptionsModelVertexModelConfigObject, OptionsModelGenericModelConfigObject, str] class Options(TypedDict, total=False): @@ -44,6 +159,12 @@ class Options(TypedDict, total=False): model: OptionsModel """Model configuration object or model name string (e.g., 'openai/gpt-5-nano')""" + screenshot: bool + """ + When true, include a screenshot of the current viewport in the extraction LLM + call. Defaults to false. + """ + selector: str """CSS selector to scope extraction to a specific element""" diff --git a/src/stagehand/types/session_observe_params.py b/src/stagehand/types/session_observe_params.py index 6c08105a..12550ac8 100644 --- a/src/stagehand/types/session_observe_params.py +++ b/src/stagehand/types/session_observe_params.py @@ -5,13 +5,19 @@ 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 __all__ = [ "SessionObserveParamsBase", "Options", "OptionsModel", + "OptionsModelVertexModelConfigObject", + "OptionsModelVertexModelConfigObjectAuth", + "OptionsModelVertexModelConfigObjectAuthCredentials", + "OptionsModelVertexModelConfigObjectProviderOptions", + "OptionsModelVertexModelConfigObjectProviderOptionsVertex", + "OptionsModelGenericModelConfigObject", "OptionsVariables", "OptionsVariablesUnionMember3", "SessionObserveParamsNonStreaming", @@ -32,7 +38,117 @@ class SessionObserveParamsBase(TypedDict, total=False): """Whether to stream the response via SSE""" -OptionsModel: TypeAlias = Union[ModelConfigParam, str] +class OptionsModelVertexModelConfigObjectAuthCredentials(TypedDict, total=False): + """Google Cloud service account credentials""" + + client_email: Required[str] + + private_key: Required[str] + + auth_provider_x509_cert_url: str + + auth_uri: str + + client_id: str + + client_x509_cert_url: str + + private_key_id: str + + project_id: str + + token_uri: str + + type: Literal["service_account"] + + universe_domain: str + + +class OptionsModelVertexModelConfigObjectAuth(TypedDict, total=False): + """Vertex provider authentication configuration""" + + credentials: Required[OptionsModelVertexModelConfigObjectAuthCredentials] + """Google Cloud service account credentials""" + + type: Required[Literal["googleServiceAccount"]] + """Use inline Google Cloud service account credentials for provider authentication""" + + project_id: Annotated[str, PropertyInfo(alias="projectId")] + """Google Cloud project ID used by google-auth-library""" + + scopes: Union[str, SequenceNotStr[str]] + """Google auth scopes for the desired API request""" + + universe_domain: Annotated[str, PropertyInfo(alias="universeDomain")] + """Google Cloud universe domain""" + + +class OptionsModelVertexModelConfigObjectProviderOptionsVertex(TypedDict, total=False): + """Vertex AI provider-specific settings""" + + location: Required[str] + """Google Cloud location for Vertex AI models""" + + project: Required[str] + """Google Cloud project ID for Vertex AI models""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the Vertex AI provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the Vertex AI provider""" + + +class OptionsModelVertexModelConfigObjectProviderOptions(TypedDict, total=False): + """Vertex provider-specific model configuration""" + + vertex: Required[OptionsModelVertexModelConfigObjectProviderOptionsVertex] + """Vertex AI provider-specific settings""" + + +class OptionsModelVertexModelConfigObject(TypedDict, total=False): + auth: Required[OptionsModelVertexModelConfigObjectAuth] + """Vertex provider authentication configuration""" + + model_name: Required[Annotated[str, PropertyInfo(alias="modelName")]] + """Model name string with provider prefix (e.g., 'openai/gpt-5-nano')""" + + provider: Required[Literal["vertex"]] + """Vertex AI model provider""" + + provider_options: Required[ + Annotated[OptionsModelVertexModelConfigObjectProviderOptions, PropertyInfo(alias="providerOptions")] + ] + """Vertex provider-specific model configuration""" + + api_key: Annotated[str, PropertyInfo(alias="apiKey")] + """API key for the model provider""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the model provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the model provider""" + + +class OptionsModelGenericModelConfigObject(TypedDict, total=False): + model_name: Required[Annotated[str, PropertyInfo(alias="modelName")]] + """Model name string with provider prefix (e.g., 'openai/gpt-5-nano')""" + + api_key: Annotated[str, PropertyInfo(alias="apiKey")] + """API key for the model provider""" + + base_url: Annotated[str, PropertyInfo(alias="baseURL")] + """Base URL for the model provider""" + + headers: Dict[str, str] + """Custom headers sent with every request to the model provider""" + + provider: Literal["openai", "anthropic", "google", "microsoft", "bedrock"] + """AI provider for the model (or provide a baseURL endpoint instead)""" + + +OptionsModel: TypeAlias = Union[OptionsModelVertexModelConfigObject, OptionsModelGenericModelConfigObject, str] class OptionsVariablesUnionMember3(TypedDict, total=False): @@ -45,6 +161,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')""" diff --git a/tests/api_resources/test_sessions.py b/tests/api_resources/test_sessions.py index 1929c8f5..9b9eac2e 100644 --- a/tests/api_resources/test_sessions.py +++ b/tests/api_resources/test_sessions.py @@ -44,11 +44,38 @@ def test_method_act_with_all_params_overload_1(self, client: Stagehand) -> None: frame_id="frameId", options={ "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "timeout": 30000, "variables": { @@ -121,11 +148,38 @@ def test_method_act_with_all_params_overload_2(self, client: Stagehand) -> None: frame_id="frameId", options={ "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "timeout": 30000, "variables": { @@ -250,19 +304,73 @@ def test_method_execute_with_all_params_overload_1(self, client: Stagehand) -> N agent_config={ "cua": True, "execution_model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "mode": "cua", "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "provider": "openai", "system_prompt": "systemPrompt", @@ -349,19 +457,73 @@ def test_method_execute_with_all_params_overload_2(self, client: Stagehand) -> N agent_config={ "cua": True, "execution_model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "mode": "cua", "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "provider": "openai", "system_prompt": "systemPrompt", @@ -447,12 +609,40 @@ def test_method_extract_with_all_params_overload_1(self, client: Stagehand) -> N options={ "ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"], "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, + "screenshot": False, "selector": "#main-content", "timeout": 30000, }, @@ -516,12 +706,40 @@ def test_method_extract_with_all_params_overload_2(self, client: Stagehand) -> N options={ "ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"], "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, + "screenshot": False, "selector": "#main-content", "timeout": 30000, }, @@ -645,12 +863,40 @@ 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": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "selector": "nav", "timeout": 30000, @@ -719,12 +965,40 @@ 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": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "selector": "nav", "timeout": 30000, @@ -978,11 +1252,38 @@ async def test_method_act_with_all_params_overload_1(self, async_client: AsyncSt frame_id="frameId", options={ "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "timeout": 30000, "variables": { @@ -1055,11 +1356,38 @@ async def test_method_act_with_all_params_overload_2(self, async_client: AsyncSt frame_id="frameId", options={ "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "timeout": 30000, "variables": { @@ -1184,19 +1512,73 @@ async def test_method_execute_with_all_params_overload_1(self, async_client: Asy agent_config={ "cua": True, "execution_model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "mode": "cua", "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "provider": "openai", "system_prompt": "systemPrompt", @@ -1283,19 +1665,73 @@ async def test_method_execute_with_all_params_overload_2(self, async_client: Asy agent_config={ "cua": True, "execution_model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "mode": "cua", "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "provider": "openai", "system_prompt": "systemPrompt", @@ -1381,12 +1817,40 @@ async def test_method_extract_with_all_params_overload_1(self, async_client: Asy options={ "ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"], "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, + "screenshot": False, "selector": "#main-content", "timeout": 30000, }, @@ -1450,12 +1914,40 @@ async def test_method_extract_with_all_params_overload_2(self, async_client: Asy options={ "ignore_selectors": ["nav", ".cookie-banner", "#sidebar-ads"], "model": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, + "screenshot": False, "selector": "#main-content", "timeout": 30000, }, @@ -1579,12 +2071,40 @@ 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": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "selector": "nav", "timeout": 30000, @@ -1653,12 +2173,40 @@ 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": { + "auth": { + "credentials": { + "client_email": "client_email", + "private_key": "private_key", + "auth_provider_x509_cert_url": "https://example.com", + "auth_uri": "https://example.com", + "client_id": "client_id", + "client_x509_cert_url": "https://example.com", + "private_key_id": "private_key_id", + "project_id": "project_id", + "token_uri": "https://example.com", + "type": "service_account", + "universe_domain": "universe_domain", + }, + "type": "googleServiceAccount", + "project_id": "projectId", + "scopes": "string", + "universe_domain": "universeDomain", + }, "model_name": "openai/gpt-5.4-mini", + "provider": "vertex", + "provider_options": { + "vertex": { + "location": "us-central1", + "project": "my-gcp-project", + "base_url": "https://example.com", + "headers": {"foo": "string"}, + } + }, "api_key": "sk-some-openai-api-key", "base_url": "https://api.openai.com/v1", "headers": {"foo": "string"}, - "provider": "openai", }, "selector": "nav", "timeout": 30000, diff --git a/tests/test_models.py b/tests/test_models.py index d23f1c44..341ac4a0 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,7 +1,8 @@ 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 @@ -9,7 +10,7 @@ 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): @@ -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"]