Skip to content
Open
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
17 changes: 14 additions & 3 deletions stagehand/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
from typing import Any, Callable, Literal, Optional
from typing import Any, Callable, Literal, Optional, Union

from browserbase.types import SessionCreateParams as BrowserbaseSessionCreateParams
from pydantic import BaseModel, ConfigDict, Field
from pydantic import BaseModel, ConfigDict, Field, field_validator

from stagehand.schemas import AvailableModel

Expand Down Expand Up @@ -65,7 +65,7 @@ class StagehandConfig(BaseModel):
alias="domSettleTimeoutMs",
description="Timeout for DOM to settle (in ms)",
)
browserbase_session_create_params: Optional[BrowserbaseSessionCreateParams] = Field(
browserbase_session_create_params: Optional[Union[BrowserbaseSessionCreateParams, dict[str, Any]]] = Field(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The underlying type provided by browserbase.types should be changed in the upstream repo instead of weakening the type to dict[str, Any] here.

If thats not possible, at least do something like:

class BBSessionCreateParamsWithOptionalProjectId(BrowserbaseSessionCreateParams):
    project_id: str | None = None

None,
alias="browserbaseSessionCreateParams",
description="Browserbase session create params",
Expand Down Expand Up @@ -111,6 +111,17 @@ class StagehandConfig(BaseModel):
)

model_config = ConfigDict(populate_by_name=True)

@field_validator('browserbase_session_create_params', mode='before')
@classmethod
def validate_browserbase_params(cls, v, info):
"""Validate and convert browserbase session create params."""
if isinstance(v, dict) and 'project_id' not in v:
values = info.data
project_id = values.get('project_id') or values.get('projectId')
if project_id:
v = {**v, 'project_id': project_id}
return v

def with_overrides(self, **overrides) -> "StagehandConfig":
"""
Expand Down
Loading