Skip to content

AISdkClient with Bedrock fails with 'modelApiKey is required' error #1372

@tkhduracell

Description

@tkhduracell

Description

When using AISdkClient with Amazon Bedrock models in BROWSERBASE environment, Stagehand throws a StagehandAPIError: modelApiKey is required error during initialization.

Error

StagehandAPIError: modelApiKey is required
    at <anonymous> (/usr/src/app/node_modules/@browserbasehq/stagehand/dist/index.js:38340:19)

Root Cause

When passing an llmClient to Stagehand, the code at lines 38695-38699 sets modelClientOptions to baseClientOptions without adding an apiKey:

if (opts.llmClient) {
  this.llmClient = opts.llmClient;
  this.modelClientOptions = baseClientOptions;  // No apiKey added
}

However, later at line 39129, when env: "BROWSERBASE" and disableAPI: false, the code tries to access this.modelClientOptions.apiKey:

const { sessionId: sessionId2, available } = yield this.apiClient.init({
  modelName: this.modelName,
  modelApiKey: this.modelClientOptions.apiKey,  // undefined for Bedrock
  // ...
});

Bedrock models use AWS credential chains (via credentialProvider) rather than a simple apiKey property, so this.modelClientOptions.apiKey is undefined.

Reproduction

import { createAmazonBedrock } from '@ai-sdk/amazon-bedrock';
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
import { AISdkClient, Stagehand } from '@browserbasehq/stagehand';

const bedrock = createAmazonBedrock({
  region: 'eu-north-1',
  credentialProvider: fromNodeProviderChain(),
});

const bedrockClient = new AISdkClient({
  model: bedrock('eu.amazon.nova-pro-v1:0'),
});

const stagehand = new Stagehand({
  env: 'BROWSERBASE',
  llmClient: bedrockClient,
});

await stagehand.init(); // Error: modelApiKey is required

Workaround

Setting disableAPI: true bypasses the API client that requires modelApiKey:

const stagehand = new Stagehand({
  env: 'BROWSERBASE',
  llmClient: bedrockClient,
  disableAPI: true,  // Bypasses the modelApiKey requirement
});

Suggested Fix

When llmClient is provided, Stagehand should either:

  1. Skip modelApiKey validation for custom LLM clients (similar to PR fix: allow custom LLM clients to skip modelApiKey validation in BrowserBase #966)
  2. Extract credentials from the AI SDK model if available
  3. Document that disableAPI: true is required for Bedrock/custom credential providers

Environment

  • Stagehand version: 3.0.1
  • Node version: 22.x
  • Using @ai-sdk/amazon-bedrock with Bedrock models
  • BROWSERBASE environment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions