Skip to main content
Connect GPT to a browser with StagehandBrowserDriver. The OpenAI SDK runs the model’s tool loop, and Stagehand handles browser interactions such as navigation, screenshots, and form input.

Prerequisites

  • An OpenAI API key and access to a model that supports the browser toolset.
  • Google Chrome for the local example, or a Browserbase API key for a hosted session.
  • Node.js 22.18 or later and pnpm for TypeScript, or Python 3.11 or later and uv for Python.

Install the integration

Clone the OpenAI Toolset repository:
Install the OpenAI SDK and Stagehand from npm:
Import the driver from the stagehand-openai-sdk npm package.

Run a browser task

Export your OpenAI API key:
Replace the repository’s example.ts or example.py with the matching example below. Both launch headless Chrome, ask GPT to read the heading on example.com, and print the final answer.
example.ts
Run from the repository’s typescript directory:
Look for a response identifying the heading as Example Domain. Wrap the driver with createBrowserToolset in TypeScript or create_browser_toolset in Python, then pass the toolset to the Responses tool runner. Keep the same driver throughout the loop so tabs and page state remain available. The tool runner doesn’t close the browser. Use finally in both languages to close the driver when the task finishes or fails.

Use Browserbase

Export your Browserbase API key:
Replace the StagehandBrowserDriver.launch call with the matching factory below. Keep the tool loop and cleanup code from the previous example.
This creates a Browserbase session, so you don’t need local Chrome. The toolset doesn’t support downloads in Browserbase sessions.

Domain restrictions

Set allowedDomains and blockedDomains in TypeScript, or allowed_domains and blocked_domains in Python, to restrict HTTP and HTTPS requests and WebSocket handshakes. A domain entry includes its subdomains; blocked entries take precedence.
Domain restrictions don’t cover WebRTC or other non-HTTP traffic. Don’t treat these lists as complete network isolation.

Troubleshooting

For additional configuration and examples, see the OpenAI Toolset repository.