Temporal + Browserbase Stagehand Integration
A best practices example showing how Temporal handles browser automation failures with automatic retries using atomic, idempotent activities.
What it does#
- Uses Stagehand to perform Google searches in a real browser
- Each individual task is encapsulated within a Temporal Activity following best practices for atomicity and idempotency
- If any individual task fails, Temporal will automatically retry it, resulting in reliable browser automation
- Clean, maintainable code following Temporal patterns
Temporal Best Practices Demonstrated#
Atomic Activities#
Each Temporal activity performs a single, well-defined task:
- initializeBrowser - Creates and initializes browser session
- navigateToSearchPage - Navigates to Google
- executeSearch - Types query and submits search
- extractSearchResults - Extracts and validates results
- cleanupBrowser - Closes browser session
- formatResults - Formats results for display
Why Atomic Activities?#
- Efficient retries: If extraction fails after search succeeds, only extraction is retried
- Better performance: No need to repeat successful steps
- Clearer debugging: Each activity's purpose is obvious
- Flexible retry policies: Different activities can have different retry strategies
Idempotent Design#
- Browser sessions are reused if already initialized
- Cleanup handles already-closed sessions gracefully
- Navigation always results in the same state
- Formatting produces consistent output