Skip to content

feat(sdk-python): add Browserbase client_options to launch and connect - #3119

Merged
miguelg719 merged 9 commits into
feat/browserbase-client-optionsfrom
feat/browserbase-client-options-python
Oct 7, 2026
Merged

miguelg719 merged 9 commits into
feat/browserbase-client-optionsfrom
feat/browserbase-client-options-python

Conversation

@miguelg719

@miguelg719 miguelg719 commented Oct 6, 2026 •

Copy link
Copy Markdown
Collaborator

why

Stacked on #3118, which adds clientOptions to the TypeScript SDK. This PR adds the same option to Python, so callers can tune the Browserbase client Stagehand builds for session management without injecting their own client.

what changed

  • client_options: BrowserbaseClientOptions on browserbase.launch() and browserbase.connect(). Fields: timeout (seconds), max_retries, default_headers, default_query, http_client. It's a public TypedDict backed by a strict Pydantic runtime model, so unknown keys such as api_key or base_url are rejected.
  • _OfficialBrowserbaseAPI builds every AsyncBrowserbase with these options. api_key and base_url stay top-level.
  • http_client: httpx.AsyncClient is the Python counterpart to TS fetch: custom transports, proxies, mocks, request/response hooks. The SDK's AsyncBrowserbase.close() closes its HTTP client, so for a caller-supplied client Stagehand builds AsyncBrowserbase around it without closing it. The caller owns the client and must keep it open until browser.close() returns. Clients Stagehand creates are still closed. httpx becomes a direct dependency because it now appears in a public type; it was already in the lock file through browserbase.
  • Removes the Python exemption feat(sdk): add Browserbase clientOptions to launch and connect #3118 added to the parity test, and adds BrowserbaseClientOptions to the check that keeps Python's input types and runtime models in sync.
  • Extends the docs section with a Python example and a minor changeset for stagehand-python.

test plan

  • pytest: 754 passed. New tests cover passing options through launch and connect, rejecting invalid options before any API call (including a sync httpx.Client), and building a real AsyncBrowserbase with the options applied.
  • A full launch-and-close lifecycle through a caller http_client with a mock transport: all four requests (extension upload, session create, release, extension delete) go through it, and it is still open afterwards. With the ownership check disabled, this test fails at session create.
  • ty check, ruff check and ruff format, uv lock --check, generate.py --check
  • rules/ast-grep parity tests with the Python exemption removed, and the docs tests
  • No live Browserbase session was launched

Mirror the TypeScript clientOptions: timeout, max_retries, default_headers, and
default_query configure the AsyncBrowserbase client used for session and
extension management. http_client is omitted because each call closes its
client. Removes the pending-Python parity exemption.
@changeset-bot

changeset-bot Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d92a4f6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@browserbasehq/stagehand-python Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 10 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.
Architecture diagram
sequenceDiagram
    participant User as Python Caller
    participant Browser as BrowserbaseBrowser
    participant Session as BrowserbaseSessionClient
    participant Official as OfficialBrowserbaseAPI
    participant BBClient as AsyncBrowserbase
    participant BBAPI as Browserbase API

    Note over User,BBAPI: Client Options Configuration Flow

    User->>Browser: launch(api_key, client_options, ...)
    Browser->>Browser: Validate browser_settings
    Browser->>Browser: NEW: Validate client_options via BrowserbaseClientOptions Pydantic model

    alt Valid client_options
        Browser->>Session: _create_browserbase_session_client(api_key, base_url, sdk_client_options)
        Session->>Official: Create _OfficialBrowserbaseAPI with normalized options dict
    else Invalid client_options (e.g. api_key, base_url, negative values)
        Browser-->>User: Raise ValidationError (no API call made)
    end

    Note over User,BBAPI: Launch Path

    Browser->>Session: create_session(options)
    Session->>Official: create_session(options, metadata, extension_id)
    Official->>Official: Build kwargs for session creation
    Official->>BBClient: AsyncBrowserbase(**client_options, api_key, base_url)
    BBClient->>BBClient: NEW: Apply timeout, max_retries, default_headers, default_query
    BBClient->>BBAPI: POST /v1/sessions with configured headers/timeout
    BBAPI-->>BBClient: Session created (id, connect_url)
    BBClient-->>Official: Return session data
    Official-->>Session: Return (session_id, connect_url)
    Session-->>Browser: session details
    Browser-->>User: ResolvedBrowserSource with CDP URL

    Note over User,BBAPI: Connect Path (similar flow)

    User->>Browser: connect(api_key, session_id, client_options)
    Browser->>Browser: Build and validate BrowserbaseConnectOptions
    Browser->>Session: _create_browserbase_session_client(api_key, base_url, client_options)
    Session->>Official: Create API wrapper with normalized options
    Session->>Official: connect_session(session_id)
    Official->>BBClient: AsyncBrowserbase(**client_options, api_key, base_url)
    BBClient->>BBAPI: GET /v1/sessions/{session_id}
    BBAPI-->>BBClient: Session details
    BBClient-->>Official: Retrieved session
    Official-->>Session: Return session info
    Session-->>Browser: Connection established
    Browser-->>User: StagehandBrowser instance
Loading

Turn on auto-fix | Re-trigger cubic

AsyncBrowserbase.close() closes its HTTP client, so Stagehand's per-call
'async with' would close a caller's client after the first request. When
http_client is supplied, build AsyncBrowserbase around it without closing it;
Stagehand-created clients are still closed. httpx becomes a direct dependency
since it now appears in a public type.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 10 files (changes from recent commits).

Tip: Review your code locally with the cubic CLI to iterate faster.

Turn on auto-fix | Re-trigger cubic

The embedded archive on main is stale (built 7df07ed4 vs embedded 9ddb0309),
which fails 'Python wheel smoke' on any Python-touching PR. Regenerated with
'go run ./internal/extensionpack' only; no source changes.

@seanmcguire12 seanmcguire12 left a comment

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.

maybe add a docs example using async with httpx.AsyncClient(...)? other than that LGTM

@miguelg719
miguelg719 merged commit 1220e0b into feat/browserbase-client-options Oct 7, 2026
42 of 43 checks passed
@miguelg719
miguelg719 deleted the feat/browserbase-client-options-python branch October 7, 2026 19:05
miguelg719 added a commit that referenced this pull request Oct 7, 2026
# why

Sibling to #2933. That PR lets callers inject a `Browserbase` SDK
instance. This PR takes the alternative approach: Stagehand keeps
building the Browserbase client, and callers pass the client's HTTP
settings instead.

- One `apiKey` authenticates both Browserbase and the Stagehand runtime.
An injected client could carry a different key.
- Stagehand controls the SDK version and how the client is built. A
hand-made or mismatched-version instance can't break calls like
`extensions.delete` that depend on SDK-specific options.
- Python can offer the same option, which #3119 does. The shared fields
(`timeout`, `max_retries`, `default_headers`, `default_query`) exist in
both SDKs.

The options are nested under `clientOptions` rather than flattened,
because `browserbase.launch()` passes unknown keys through to
`SessionCreateParams`, which already has a `timeout` field (the session
timeout in seconds).

# what changed

- `clientOptions?: BrowserbaseClientOptions` on `browserbase.launch()`
and `browserbase.connect()`. The type is `Pick<ClientOptions, "timeout"
| "maxRetries" | "defaultHeaders" | "defaultQuery" | "fetch">`,
validated by a strict schema. `apiKey` and `baseURL` are rejected inside
it. `BrowserbaseClientOptions` is exported.
- In the cross-language parity test,
`BrowserbaseConnectOptions.client_options` is temporarily exempt for
Python, until the stacked #3119 removes the exemption, and pending for
Go. Go talks to Browserbase over its own `net/http` client rather than
the official SDK.
- Adds docs (`configuration/browser.mdx`, TypeScript only for now) and a
minor changeset for `@browserbasehq/stagehand`.

# test plan

- [x] `tsc --noEmit`, oxfmt, oxlint; sdk-ts unit tests and
`rules/ast-grep` parity tests (31 files, 407 tests)
- [x] One test builds a real `@browserbasehq/sdk` client with a spy
`fetch` and checks that the request URL, `defaultQuery`,
`defaultHeaders` and API key are applied
- [x] Published-tarball consumer type fixture uses
`BrowserbaseClientOptions`
- [ ] No live Browserbase session was launched
miguelg719 added a commit that referenced this pull request Oct 7, 2026
)

# why

Stacked on #3119 (Python), which is stacked on #3118 (TypeScript). Those
PRs add Browserbase client options to `browserbase.launch()` /
`browserbase.connect()` in TS and Python. This PR adds the Go
equivalent, so the three SDKs are at parity again and the parity test no
longer needs a Go exemption.

# what changed

- New `BrowserbaseClientOptions` struct, set as `ClientOptions
*BrowserbaseClientOptions` on both `BrowserbaseLaunchOptions` and
`BrowserbaseConnectOptions`:
- `Timeout time.Duration`: applies to each attempt; 0 keeps the 60s
default.
  - `MaxRetries *int`: nil keeps the default of 2.
- `DefaultHeaders map[string]string`, `DefaultQuery map[string]string`.
  - `HTTPClient *http.Client`: owned by the caller and never modified.
- Go talks to Browserbase over its own `net/http` client, so the
internal options it already had (HTTP client, max retries) are now
exposed, plus headers and query.
- The timeout is applied per attempt through the request context, both
for the default client and for a client the caller passes in, and it
also covers reading the response body. The default client no longer sets
`http.Client.Timeout` itself. If an attempt times out, only requests
that are safe to resend (retrieve, release, delete) are retried, each
with a new deadline. Creates fail immediately.
- Negative `Timeout` or `MaxRetries`, invalid header names, and header
values containing control characters are rejected before any request is
sent.
- `ClientOptions` is never sent in the session-create body; a test
checks the body contents.
- Removes `pendingGoBrowserFields` from the cross-language parity test.
- Docs: the client-options section is a TypeScript / Python / Go tab
group again.
- Adds a minor changeset for `@browserbasehq/stagehand-go`. This PR is
the only change in the stack that releases Go.

# header precedence

Same rule as TS and Python: `DefaultHeaders` override Stagehand's own
headers on conflict, including `X-BB-API-Key` and `User-Agent`. Both
Browserbase SDKs apply caller default headers last; this was checked in
their source (`@browserbasehq/sdk` `index.js` `defaultHeaders`, and the
Python `_base_client`). Go still rejects invalid header names or values
before sending.

# test plan

- [x] `go vet`; gofmt (prints nothing); `go test -race` on every package
except examples; generator `--check`; `scripts/check-examples.sh`
- [x] New `browserbase_client_options_test.go` uses `httptest` servers
to cover:
- options reaching requests, and later changes to the caller's maps
having no effect;
- default headers and query on POST and GET, with caller defaults
overriding Stagehand's headers (mutation-checked);
  - `MaxRetries` set to 0 and 1, and the default;
- the per-attempt timeout, including a retry getting a new deadline and
the deadline covering the response body;
  - a caller `HTTPClient` being used and left unmodified;
- invalid options failing with zero requests reaching the server, on
both launch and connect;
  - full launch and connect through the real factory.
- [x] Mutation-checked: with the per-attempt deadline removed, the
timeout test fails in about 5s with a clear message.
- [x] `rules/ast-grep` parity tests (Go exemption removed), docs tests,
the TS suite, oxfmt, oxlint, `check-changesets`
- [ ] Plain `go test ./...` fails only because
`packages/sdk-go/examples` doesn't build as a single package; that was
already the case. CI and the justfile exclude `/examples`.
- [ ] No live Browserbase session was launched
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants