This documentation is automatically synced from the Ruby SDK GitHub repository.
What is Stagehand?
Stagehand is a browser automation framework used to control web browsers with natural language and code. By combining the power of AI with the precision of code, Stagehand makes web automation flexible, maintainable, and actually reliable.Why Stagehand?
Most existing browser automation tools either require you to write low-level code in a framework like Selenium, Playwright, or Puppeteer, or use high-level agents that can be unpredictable in production. By letting developers choose what to write in code vs. natural language (and bridging the gap between the two) Stagehand is the natural choice for browser automations in production.- Choose when to write code vs. natural language: use AI when you want to navigate unfamiliar pages, and use code when you know exactly what you want to do.
- Go from AI-driven to repeatable workflows: Stagehand lets you preview AI actions before running them, and also helps you easily cache repeatable actions to save time and tokens.
- Write once, run forever: Stagehand’s auto-caching combined with self-healing remembers previous actions, runs without LLM inference, and knows when to involve AI whenever the website changes and your automation breaks.
Installation
To use this gem, install via Bundler by adding the following to your application’sGemfile:
Usage
Running the Example
Set the required environment variables and run the example script:Streaming
We provide support for streaming responses using Server-Sent Events (SSE).Handling errors
When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass ofStagehand::Errors::APIError will be thrown:
| Cause | Error Type |
|---|---|
| HTTP 400 | BadRequestError |
| HTTP 401 | AuthenticationError |
| HTTP 403 | PermissionDeniedError |
| HTTP 404 | NotFoundError |
| HTTP 409 | ConflictError |
| HTTP 422 | UnprocessableEntityError |
| HTTP 429 | RateLimitError |
| HTTP >= 500 | InternalServerError |
| Other HTTP error | APIStatusError |
| Timeout | APITimeoutError |
| Network error | APIConnectionError |
Retries
Certain errors will be automatically retried 2 times by default, with a short exponential backoff. Connection errors (for example, due to a network connectivity problem), 408 Request Timeout, 409 Conflict, 429 Rate Limit, >=500 Internal errors, and timeouts will all be retried by default. You can use themax_retries option to configure or disable this:
Timeouts
By default, requests will time out after 60 seconds. You can use the timeout option to configure or disable this:Stagehand::Errors::APITimeoutError is raised.
Note that requests that time out are retried by default.
Advanced concepts
BaseModel
All parameter and response objects inherit fromStagehand::Internal::Type::BaseModel, which provides several conveniences, including:
-
All fields, including unknown ones, are accessible with
obj[:prop]syntax, and can be destructured withobj => {prop: prop}or pattern-matching syntax. - Structural equivalence for equality; if two API calls return the same values, comparing the responses with == will return true.
- Both instances and the classes themselves can be pretty-printed.
-
Helpers such as
#to_h,#deep_to_h,#to_json, and#to_yaml.
Making custom or undocumented requests
Undocumented properties
You can send undocumented parameters to any endpoint, and read undocumented response properties, like so: Note: theextra_ parameters of the same name overrides the documented parameters.
Undocumented request params
If you want to explicitly send an extra param, you can do so with theextra_query, extra_body, and extra_headers under the request_options: parameter when making a request, as seen in the examples above.
Undocumented endpoints
To make requests to undocumented endpoints while retaining the benefit of auth, retries, and so on, you can make requests usingclient.request, like so:
Concurrency & connection pooling
TheStagehand::Client instances are threadsafe, but are only are fork-safe when there are no in-flight HTTP requests.
Each instance of Stagehand::Client has its own HTTP connection pool with a default size of 99. As such, we recommend instantiating the client once per application in most settings.
When all available connections from the pool are checked out, requests wait for a new connection to become available, with queue time counting towards the request timeout.
Unless otherwise specified, other classes in the SDK do not have locks protecting their underlying data structure.
Sorbet
This library provides comprehensive RBI definitions, and has no dependency on sorbet-runtime. You can provide typesafe request parameters like so:Enums
Since this library does not depend onsorbet-runtime, it cannot provide T::Enum instances. Instead, we provide “tagged symbols” instead, which is always a primitive at runtime:
Versioning
This package follows SemVer conventions. As the library is in initial development and has a major version of0, APIs may change at any time.
This package considers improvements to the (non-runtime) *.rbi and *.rbs type definitions to be non-breaking changes.

